Skip to content

Virtual functions

inkoalawetrust edited this page Apr 30, 2023 · 8 revisions

All NPCs in the library share a set of common virtual functions. Which allow them to define custom behaviors on a per-class basis. For example, defining custom attack decision code, without having to create your own chase function from scratch, or glue the code on top of existing code.

UserVariableDefaults()

Parameters:

  • None

Return type(s):

  • None

Function:

Used to set the actors' default user variable values, for variables that did not have a custom value passed to them in the editor. GZDoom already has a //$UserDefaultValue editor key. However only works for actors spawned by the map. While this function allows for values to also be set for actors spawned by ACS or the console.

Example:

Say your NPC has a User_SpecialAttackChance variable, allowing mappers to define how likely the actor is to do a special attack. The default chance should be 24 out of 255 for every time it attacks. //$UserDefaultValue can set the default value for NPCs placed on the map. But the default value will be kept to 0 for NPCs spawned through ACS, or the console.

	//$UserDefaultValue 24
	Int User_SpecialAttackChance; //How often does the NPC do its' AOE attack ?

This is where UserVariableDefaults() helps:

	Override Void UserVariableDefaults()
	{
		If (User_SpecialAttackChance == 0) User_SpecialAttackChance = 24; //Default AOE chance.
	}

CanAttack()

Parameters:

  • None

Return type(s):

  • True or false.

Function:

Returns whether or not the NPC can currently attack, by default it just checks if the actor has a missile or melee state, is not dead, and is not in a SECF_NOATTACK sector. More or less vanilla behavior.

ShouldAttack()

Parameters:

  • NoStateJump: If true, the actor should not jump to any state even if the function returns true. This needs to manually be obeyed by overrides to this virtual.

Return type(s):

  • Returns true if the actor should attack, and also makes them actually enter the intended attack state if NoStateJump is off.

Function:

This virtual is the attack decision code of KAI NPCs. By default, it is to be used by chase functions to determine what attack the NPC should perform, if any, and make them actually go to that state. Overriding it allows for custom attack decision code on a per-actor basis, without needing to write new chase functions or other hacks.

Default behavior:

By default, this virtual is a mostly direct ZScript rip of A_Chase's attack decision code. So naturally, it behaves the same as well.

OnWander()

Parameters:

  • None

Return type(s):

  • None

Function:

Allows executing code every time KAI_Wander() is called.

GetAttackLocation()

Parameters:

  • None

Return type(s):

  • Returns a Vector3 coordinate to the location at which the NPC is going to attack.

Function:

Used to determine where the NPC is going to attack at.

Default behavior:

If the NPC has a target, it returns their position, otherwise it returns a null vector[^1].

IsActorHostile()

Note: This is a ClearScope function. So that it can be called from KAI_LOFRaycast as well.

Parameters:

  • Other: The actor to check if it is hostile.

Return type(s):

  • Returns true if Other is hostile, false otherwise

Function:

Checks if the Other actor is an enemy to the calling KAI NPC or not. Can be used to give NPCs different conditions for if another actor is an enemy or not. Like for making an enemy NPC that doesn't always consider other NPCs to not be hostile like IsHostile() does.

Default behavior:

Just calls IsHostile() by default.

AssessThreatLevel()

TBD: Maybe try making this into its' own page to include the Baron of Hell example ? Or put more info on using the threat level system on the feature page.

Parameters:

  • Other: The actor whose threat level should be assessed.
  • CheckPlayers: Should players be returned as THREAT_UNSTOPPABLE if they have buddha or god mode. Or be automatically returned as THREAT_DANGEROUS otherwise ? Default is false.

Return type(s):

  • Returns a threat level (Integer).

Function:

[^1]: A vector with a value of (Double.NaN,Double.NaN,Double.NaN), or -2147483647 on each member. Which is returned as an empty Vector3 by IsEmptyVector3.

Clone this wiki locally