[Lua/OAL] Script loses entity ownership after using DetachEntity

Attaching an entity with AttachEntityToEntity, then later using DetachEntity will cause the resource to lose ownership of the entity when using “use_experimental_fxv2_oal”.

Using DoesEntityBelongToThisScript without oal will return 1 each time.
With oal, it will return true the first two times.

CreateThread(function()
    local model = `hei_prop_heist_thermite`

    RequestModel(model)
    Wait(0)

    local entity = CreateObject(model, 0.0, 0.0, 0.0, true, true, false)
    SetModelAsNoLongerNeeded(model)
    print(DoesEntityExist(entity), DoesEntityBelongToThisScript(entity, true))

    AttachEntityToEntity(entity, PlayerPedId(), GetPedBoneIndex(PlayerPedId(), 28422), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
    print(DoesEntityExist(entity), DoesEntityBelongToThisScript(entity, true))

    Wait(500)

    DetachEntity(entity, true, true)
    print(DoesEntityExist(entity), DoesEntityBelongToThisScript(entity, true))
    DeleteEntity(entity)
end)

DeleteEntity fails since the script does not own the entity.

Could it be DetachEntity had a new argument added at some point that (if not set to 0, as with uninitialized memory) will release the entity as well?

2699 added a field (p15) to ATTACH_ENTITY_TO_ENTITY that toggles a flag (stored in a fwAttachmentEntityExtension field) to mark the entity as no longer needed on detach. Thus, for reasons state above, you will see inconsistent behaviour.

The native implementation can be referenced by 0x8A3D8937

2 Likes