{"hash":"0x5BA35AAF","ns":"CFX","name":"ADD_STATE_BAG_CHANGE_HANDLER","slug":"ADD_STATE_BAG_CHANGE_HANDLER","official":true,"named":true,"luaName":"AddStateBagChangeHandler","game":"cfx","apiset":"shared","returnType":"int","params":[{"name":"keyFilter","type":"char*","description":"The key to check for, or null for no filter."},{"name":"bagFilter","type":"char*","description":"The bag ID to check for such as `entity:65535`, or null for no filter."},{"name":"handler","type":"func","description":"The handler function."}],"returnsDescription":"A cookie to remove the change handler.","summary":"Adds a handler for changes to a state bag.","descriptions":[{"source":"cfx","format":"md","body":"Adds a handler for changes to a state bag.\n\nThe function called expects to match the following signature:\n\n```ts\nfunction StateBagChangeHandler(bagName: string, key: string, value: any, reserved: number, replicated: boolean);\n```\n\n*   **bagName**: The internal bag ID for the state bag which changed. This is usually `player:Source`, `entity:NetID`\n    or `localEntity:Handle`.\n*   **key**: The changed key.\n*   **value**: The new value stored at key. The old value is still stored in the state bag at the time this callback executes.\n*   **reserved**: Currently unused.\n*   **replicated**: Whether the set is meant to be replicated.\n\nAt this time, the change handler can't opt to reject changes.\n\nIf bagName refers to an entity, use [GET_ENTITY_FROM_STATE_BAG_NAME](#\\_0x4BDF1867) to get the entity handle\nIf bagName refers to a player, use [GET_PLAYER_FROM_STATE_BAG_NAME](#\\_0xA56135E0) to get the player handle"}],"examples":[{"lang":"js","code":"AddStateBagChangeHandler(\"blockTasks\", null, async (bagName, key, value /* boolean */) => {\n    let entity = GetEntityFromStateBagName(bagName);\n    // Whoops, we don't have a valid entity!\n    if (entity === 0) return;\n    // We don't want to freeze the entity position if the entity collision hasn't loaded yet\n    while (!HasCollisionLoadedAroundEntity(entity)) {\n        // The entity went out of our scope before the collision loaded\n        if (!DoesEntityExist(entity)) return;\n        await Delay(250);\n    }\n    SetEntityInvincible(entity, value)\n    FreezeEntityPosition(entity, value)\n    TaskSetBlockingOfNonTemporaryEvents(entity, value)\n})","source":"cfx"},{"lang":"lua","code":"AddStateBagChangeHandler(\"blockTasks\", nil, function(bagName, key, value) \n    local entity = GetEntityFromStateBagName(bagName)\n    -- Whoops, we don't have a valid entity!\n    if entity == 0 then return end\n    -- We don't want to freeze the entity position if the entity collision hasn't loaded yet\n    while not HasCollisionLoadedAroundEntity(entity) do\n        -- The entity went out of our scope before the collision loaded\n        if not DoesEntityExist(entity) then return end\n        Wait(250)\n    end\n    SetEntityInvincible(entity, value)\n    FreezeEntityPosition(entity, value)\n    TaskSetBlockingOfNonTemporaryEvents(entity, value)\nend)","source":"cfx"},{"lang":"cs","code":"AddStateBagChangeHandler(\"blockTasks\", null,\n    new Action<string, string, object, int, bool>\n    (async (bagName, key, value, res, rep) =>\n    {\n        bool val = (bool)value;\n\n        var entity = GetEntityFromStateBagName(bagName);\n        //-- Whoops, we don't have a valid entity!\n        if (entity == 0) return;\n        //-- We don't want to freeze the entity position if the entity collision hasn't loaded yet\n        while (!HasCollisionLoadedAroundEntity(entity))\n        {\n            //--The entity went out of our scope before the collision loaded\n            if (!DoesEntityExist(entity)) return;\n\n            await Delay(250);\n        }\n        SetEntityInvincible(entity, val);\n        FreezeEntityPosition(entity, val);\n        TaskSetBlockingOfNonTemporaryEvents(entity, val);\n    }));","source":"cfx"}],"aliases":[],"enums":[],"related":["0xD36BE661"],"sources":["cfx"],"doc":3,"url":"https://natives.borderlinerp.com/natives/CFX/ADD_STATE_BAG_CHANGE_HANDLER","markdownUrl":"https://natives.borderlinerp.com/natives/CFX/ADD_STATE_BAG_CHANGE_HANDLER.md","snippets":{"lua":"local retval = AddStateBagChangeHandler(keyFilter, bagFilter, handler)","luaInvoke":"local retval = Citizen.InvokeNative(0x5ba35aaf, keyFilter, bagFilter, handler, Citizen.ResultAsInteger())","js":"const retval = AddStateBagChangeHandler(keyFilter, bagFilter, handler);","jsInvoke":"const retval = Citizen.invokeNative('0x5ba35aaf', keyFilter, bagFilter, handler, Citizen.returnResultAnyway(), Citizen.resultAsInteger());","csharp":"int retval = Function.Call<int>((Hash)0x5ba35aaf, keyFilter, bagFilter, handler);","notes":[]}}