TokenPool
Hands out a limited number of tokens, e.g. the "attack token" AI pattern: at most maxHolders enemies attack at once, and after one finishes nobody else starts for cooldownSeconds, so attacks come one after another rather than all at once.
Holders are identified by an id (a GameEntity's id, or any string or integer), since = can't compare objects. Call update(dt) once per frame.
Properties
maxHolders(integer) — How many ids can hold a token at oncecooldownSeconds(float) — After any token is released, nobody can take one for this long
Example
m.attackTokens = new BGE.TokenPool(1, 1.2)
' In an enemy's onUpdate:
if inRange and m.attackTokens.request(m.id)
m.startAttack()
end if
' When its attack is over, or it dies:
m.attackTokens.release(m.id)Constructor
new TokenPool(
maxHolders?: integer,
cooldownSeconds?: float,
): TokenPoolParameters
maxHolders(integer, optional, default: 1)cooldownSeconds(float, optional, default: 0)
Instance Methods
update(dt: float): void
Counts down the cooldown and the idle time. Call once per frame.
Parameters
dt(float) — seconds
Returns
void
canRequest(id: string | integer): boolean
True if id already holds a token, or could take one now.
Parameters
id(string | integer)
Returns
boolean
request(id: string | integer): boolean
Takes a token for id if it can (see canRequest()). Holding one already counts.
Parameters
id(string | integer)
Returns
boolean— true ifidnow holds a token
release(id: string | integer): boolean
Gives back id's token and starts the cooldown. Safe to call when it holds none.
Parameters
id(string | integer)
Returns
boolean— true if it held one
isHolding(id: string | integer): boolean
Parameters
id(string | integer)
Returns
boolean
holderCount(): integer
Returns
integer
cooldownRemaining(): float
Seconds left before anyone can take a token after the last release; 0 when none.
Returns
float
idleSeconds(): float
Seconds since anyone last took a token, counted only while nobody holds one (0 while one is held). E.g. let an enemy out of view attack once the player has had a long break.
Returns
float
clear(): void
Drops every holder and resets the cooldown and idle time.
Returns
void