# CFX::GET_ENTITY_FROM_STATE_BAG_NAME

- **Hash:** `0x4BDF1867`
- **Namespace:** CFX
- **Runtime name (Lua/JS):** `GetEntityFromStateBagName`
- **Side:** shared (Cfx runtime native)
- **Name status:** official
- **Documentation:** described with examples
- **URL:** https://natives.borderlinerp.com/natives/CFX/GET_ENTITY_FROM_STATE_BAG_NAME

## Signature

```c
Entity GET_ENTITY_FROM_STATE_BAG_NAME(char* bagName);
```

## Parameters

- `bagName` (`char*`) — 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

*Source: Cfx.re native reference*

Returns the entity handle for the specified state bag name. For use with [ADD_STATE_BAG_CHANGE_HANDLER](#\_0x5BA35AAF).

## Usage

### Lua

```lua
local retval = GetEntityFromStateBagName(bagName)

-- always available, by hash
local retval = Citizen.InvokeNative(0x4bdf1867, bagName, Citizen.ResultAsInteger())
```

### JavaScript

```js
const retval = GetEntityFromStateBagName(bagName);

// by hash
const retval = Citizen.invokeNative('0x4bdf1867', bagName, Citizen.returnResultAnyway(), Citizen.resultAsInteger());
```

### C#

```csharp
int retval = Function.Call<int>((Hash)0x4bdf1867, bagName);
```

## Examples

*Source: Cfx.re native reference*

```js
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)
})
```

*Source: Cfx.re native reference*

```lua
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.*
