AddStateBagChangeHandler
Docslocal retval = AddStateBagChangeHandler(keyFilter, bagFilter, handler)Description
Adds a handler for changes to a state bag.
The function called expects to match the following signature:
function StateBagChangeHandler(bagName: string, key: string, value: any, reserved: number, replicated: boolean); bagName: The internal bag ID for the state bag which changed. This is usually player:Source, entity:NetID
or localEntity:Handle.
key: The changed key.
value: The new value stored at key. The old value is still stored in the state bag at the time this callback executes.
reserved: Currently unused.
- replicated: Whether the set is meant to be replicated.
If bagName refers to an entity, use GET_ENTITY_FROM_STATE_BAG_NAME to get the entity handle
If bagName refers to a player, use GET_PLAYER_FROM_STATE_BAG_NAME to get the player handle
Parameters
| Name | Type | Description |
|---|---|---|
keyFilter | char* | The key to check for, or null for no filter. |
bagFilter | char* | The bag ID to check for such as `entity:65535`, or null for no filter. |
handler | func | The handler function. |
Returns
intReturns an integer value.
Examples
Official
AddStateBagChangeHandler("blockTasks", null, async (bagName, key, value /* boolean */) => {
let entity = GetEntityFromStateBagName(bagName);
// Whoops, we don't have a valid entity!
if (entity === 0) return;
// We don't want to freeze the entity position if the entity collision hasn't loaded yet
while (!HasCollisionLoadedAroundEntity(entity)) {
// The entity went out of our scope before the collision loaded
if (!DoesEntityExist(entity)) return;
await Delay(250);
}
SetEntityInvincible(entity, value)
FreezeEntityPosition(entity, value)
TaskSetBlockingOfNonTemporaryEvents(entity, value)
})