RedM Natives

GET_ENTITY_FROM_STATE_BAG_NAME

Returns the entity handle for the specified state bag name.

Hash0x4BDF1867
Runtime nameGetEntityFromStateBagName
NamespaceCFX
SideClient + server
NameOfficial
Docs

Signature

Entity GET_ENTITY_FROM_STATE_BAG_NAME(char* bagName);

Parameters

NameTypeDescription
bagNamechar*

An internal state bag ID from the argument to a state bag change handler.

Returns Entity — The entity handle or 0 if the state bag name did not refer to an entity, or the entity does not exist.

Description

Cfx.re native reference

Returns the entity handle for the specified state bag name. For use with ADD_STATE_BAG_CHANGE_HANDLER.

Usage

Lua
local retval = GetEntityFromStateBagName(bagName)

-- always works, by hash
local retval = Citizen.InvokeNative(0x4bdf1867, bagName, Citizen.ResultAsInteger())
JavaScript and C#
JavaScript
const retval = GetEntityFromStateBagName(bagName);

// by hash
const retval = Citizen.invokeNative('0x4bdf1867', bagName, Citizen.returnResultAnyway(), Citizen.resultAsInteger());
C#
int retval = Function.Call<int>((Hash)0x4bdf1867, bagName);

Examples

JS · Cfx.re native reference
AddStateBagChangeHandler("blockTasks", null, async (bagName, key, value /* boolean */) => {
    let entity = GetEntityFromStateBagName(bagName);
    // Whoops, we were 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)
})
LUA · Cfx.re native reference
AddStateBagChangeHandler("blockTasks", nil, function(bagName, key, value) 
    local entity = GetEntityFromStateBagName(bagName)
    -- Whoops, we don't have a valid entity!
    if entity == 0 then return end
    -- We don't want to freeze the entity position if the entity collision hasn't loaded yet
    while not HasCollisionLoadedAroundEntity(entity) do
        -- The entity went out of our scope before the collision loaded
        if not DoesEntityExist(entity) then return end
        Wait(250)
    end
    SetEntityInvincible(entity, value)
    FreezeEntityPosition(entity, value)
    TaskSetBlockingOfNonTemporaryEvents(entity, value)
end)

Sources

Cfx.re native reference

Something wrong or missing? Contribute a verified note — corrections ship through pull requests.