tsg ai totems and kill tokens
Track information about AI units and reference it after death. Example code spawns tokens per enemy type at death location that players can pick up.

Current File
tsg ai totem and kill token kit v1
Boilerplate
This is a subset of the boilerplate code provided by the tsg init prefab.
If this is being used alongside an install of tsg init, this code should be deleted.
tsg init is explained in detail here: tsg init


Totems
Variable Declaration
mobTotems- Object List Variable, GlobalObjects with label User: Mike are added to this list at declaration.
totemOwners- Object List Variable, GlobalHolds the list of entities that have totems assigned to them.
assignedTotems- Object List Variable, GlobalHolds the list of totems that are currently assigned to entities.
cooldownTotems- Object List Variable, GlobalHolds the list of totems that are on cooldown, which prevents them from being assigned.
totemOwner- Object Variable, ObjectStores a reference to the entity a totem is assigned to.
Is scoped to the totem owned by that entity.

Poll Totems and Check Totem Owner Status
For each Totem, check if
totemOwneris a valid objectIf false, that AI is no longer alive/in the simulation
Remove Totem from
assignedTotemsAdd Totem to
cooldownTotemsCall
ownerDiedto process the death of the current Totem's owner

Update Totem Ownership
Clean up
totemOwnerslistFor each AI Unit, remove from
totemOwnersFor each assigned Totem, add
totemOwnertototemOwners
For each AI Unit not in
totemOwners...Assign the first unassigned Totem that is not in
cooldownTotemsto the current unitAssign the current unit as it's Totem's
totemOwnerAdd the current Totem to
assignedTotems

State Monitoring
Call
checkTotemOwnershipFor each assigned Totem, set Totem location to offset from it's owner
Adjust offset with Vector 3 node

Handle Totem Losing Ownership
Call
processOwnerDeathWait N Seconds to keep Totem data available
Delete and spawn Totem to reset Totem data
Remove Totem from
cooldownTotemsso that it can be used again

Kill Tokens
Variable Declaration
killTokens- Object List Variable, GlobalObjects that are created to drop from dead units are added to this list when they are created.
activeKillTokens- Object List Variable, GlobalKill Tokens that are in play and haven't been picked up are added to this list.
This list is not relevant to Gunball Machine Tokens.
playerInventory- Number Variable, ObjectThe value that is adjusted to represent how many Kill Tokens a player has acquired.
killTokenValue- Number Variable, ObjectThe number of Kill Tokens that an unit is configured to drop.
This value defaults to 1 and can be augmented during Totem config or any time between then and the unit's death

Totem Configuration
Toggle event via adjusting hard-coded Branch node
In this example, the Totem owner is examined and the value of
killTokenValueis set based on their Character Type using a switch case

Distribute Kill Tokens on Owner Death
For
killTokenValueiterations...Add the input Totem's position, plus offset, to
v3QueueWait 0.1 Seconds
Call
gunballto generate a Kill Token

Handle Kill Token Reset (Recursive Loop)
Check to see if Totem is in
activeKillTokensor if time has run outIf true, remove the Totem from
activeKillTokensand delete itIf false, call
resetTokenLoopwith the input number value minus 1

Poll Active Kill Tokens and Player Locations to Handle Token Pick Up
For each active Kill Token...
For each Player (all players)
Get current Player distance from current Kill Token
Check if distance < 8 units
If true, remove Kill Token from
activeKillTokens, delete it, and incrementplayerInventoryfor current Player, and calltokenPU* event
* screenshot says 'motePU' instead of 'tokenPU'

Handle Players Dropping Kill Tokens on Death
Call
tokenDropwhen a Player is killedFor
playerInventory* X (where X is a 0-1 scalar to modify the magnitude) iterations...Add the player's location, plus offset, to
v3Queue
Wait 0.1 seconds
Call
gunballSet current player's
playerInventoryto 0

Gunball Machine
Clean Up Excess Kill Tokens and Prevent Token Pick Up as Weapon
Sometimes the Gunball Machine makes extra Tokens
When tokens fall into the boundary, they are excess that wont get assigned, delete them
Tokens shouldn't be picked up as weapons
On Token pickup, force dropping that Token

Generate Tokens
Preload Gunball Machine at
gameplayInitGive turret a weapon (Generic Ball or Skull)
On
gunballFor input Iterations...
Wait 0.1 seconds
Give turret a weapon
Wait 0 seconds
Force turret to drop weapon
On Weapon Dropped
Check if dropping unit is Gunball Machine (turret)
If true, add the dropped weapon to killTokens
Wait 0.1 seconds
Call
sendItand pass the dropped weapon

Put Generated Kill Tokens into Play
Add current Token to
activeTokensSet current Token to random rotation
Set current Token position to Index 1 of
v3QueueUpdate
v3Queueto remove that entrySet current Token velocity to random (in ranges) upward and sideways velocity to produce fountain effect
Call
resetTokenLoopfor current Token

Contributors
Captain Punch
Last updated