---
description: >-
  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.
---

# tsg ai totems and kill tokens

##

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-f4c2f05a03be42c3a00073971f8cf68b0a08fab2%2Fimage%20(44).png?alt=media" alt=""><figcaption><p>An image of the prefab. The blue cubes are the Totem objects. Each of the two brains below them handles Totems and Kill Tokens, respectively. The lower brain acts as an area monitor that deletes excess Kill Tokens if they are generated and not put into play, the glass is just there to funnel them and make it so they don't fall past that zone. The turret is used to generate the Kill Tokens.</p></figcaption></figure>

## Current File

[tsg ai totem and kill token kit v1](https://www.halowaypoint.com/halo-infinite/ugc/prefabs/5de45543-2314-470b-aba8-64ef9165f795)

### 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](../map-mode-setup/tsg-init)

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-42c8a2dd14908ba0ad75439fc1edd1edd91ff102%2Fimage%20(28).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-1547755fc98ffe6034e5db4ec3e84b1a955d2665%2Fimage%20(29).png?alt=media" alt=""><figcaption></figcaption></figure>

### Totems

#### Variable Declaration

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

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-8ec161a73726fd84eb98c3b41148e09e045f8cbb%2Fimage%20(30).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Poll Totems and Check Totem Owner Status

* For each Totem, check if `totemOwner` is a valid object
* If false, that AI is no longer alive/in the simulation
  * Remove Totem from `assignedTotems`
  * Add Totem to `cooldownTotems`
  * Call `ownerDied` to process the death of the current Totem's owner

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-4f0903dd35af67ae43d472b110e7c407463747c2%2Fimage%20(31).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Update Totem Ownership

* Clean up `totemOwners` list
  * For each AI Unit, remove from `totemOwners`
  * For each assigned Totem, add `totemOwner` to `totemOwners`
* For each AI Unit not in `totemOwners`...
  * Assign the first unassigned Totem that is not in `cooldownTotems` to the current unit
  * Assign the current unit as it's Totem's `totemOwner`
  * Add the current Totem to `assignedTotems`

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-675023f8e7e341c2446d7f245316b725cac5fa32%2Fimage%20(32).png?alt=media" alt=""><figcaption></figcaption></figure>

#### State Monitoring

* Call `checkTotemOwnership`
* For each assigned Totem, set Totem location to offset from it's owner
  * Adjust offset with Vector 3 node

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-c86a876379e02d1f63c1a51e44e2b5fce6f4635f%2Fimage%20(33).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Handle Totem Losing Ownership

* Call `processOwnerDeath`
* Wait N Seconds to keep Totem data available
* Delete and spawn Totem to reset Totem data
* Remove Totem from `cooldownTotems` so that it can be used again

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-242bac99faf601aeb46872adae951c6a9e120963%2Fimage%20(34).png?alt=media" alt=""><figcaption></figcaption></figure>

### Kill Tokens

#### Variable Declaration

* `killTokens` - Object List Variable, Global
  * Objects that are created to drop from dead units are added to this list when they are created.
* `activeKillTokens` - Object List Variable, Global
  * Kill 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, Object
  * The value that is adjusted to represent how many Kill Tokens a player has acquired.
* `killTokenValue` - Number Variable, Object
  * The 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

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-001bd8fc25d16cfd953a2cc17135367ac398849e%2Fimage%20(35).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Totem Configuration

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

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-62bf631b674e872f6a4e1f4ae9a8642230816f08%2Fimage%20(36).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Distribute Kill Tokens on Owner Death

* For `killTokenValue` iterations...
  * Add the input Totem's position, plus offset, to `v3Queue`
  * Wait 0.1 Seconds
  * Call `gunball` to generate a Kill Token

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-5ab4b64bfdb7c2c51ccccd5c2c80955d641eca71%2Fimage%20(37).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Handle Kill Token Reset (Recursive Loop)

* Check to see if Totem is in `activeKillTokens` or if time has run out
  * If true, remove the Totem from `activeKillTokens` and delete it
  * If false, call `resetTokenLoop` with the input number value minus 1

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-cc0e92b4e36a406ceb55582e54de8efab659036e%2Fimage%20(38).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 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 increment `playerInventory` for current Player, and call `tokenPU`\* event

\* screenshot says 'motePU' instead of 'tokenPU'

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-d3277b38bebc1aaeef8fcd8e6b239a2e00739668%2Fimage%20(39).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Handle Players Dropping Kill Tokens on Death

* Call `tokenDrop` when a Player is killed
* For `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 `gunball`
* Set current player's `playerInventory` to 0

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-15dcd411650c71fb87295fa9960bca6c7607073e%2Fimage%20(40).png?alt=media" alt=""><figcaption></figcaption></figure>

### 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

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-904168e5a82d436a6b625d0e06624934eb94db3c%2Fimage%20(41).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Generate Tokens

* Preload Gunball Machine at `gameplayInit`
  * Give turret a weapon (Generic Ball or Skull)
* On `gunball`
  * For 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 `sendIt` and pass the dropped weapon

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-2e00d73d2ef3a2964f4462e437a0936da20301ef%2Fimage%20(42).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Put Generated Kill Tokens into Play

* Add current Token to `activeTokens`
* Set current Token to random rotation
* Set current Token position to Index 1 of `v3Queue`
* Update `v3Queue` to remove that entry
* Set current Token velocity to random (in ranges) upward and sideways velocity to produce fountain effect
* Call `resetTokenLoop` for current Token

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-c031e36990cb98f7d5f7ead093fdd4e29f595233%2Fimage%20(43).png?alt=media" alt=""><figcaption></figcaption></figure>

## Contributors

Captain Punch
