For developers: Reading attachments
This page is aimed at server developers. It shows how another script detects whether an entity is an ELS attachment, which vehicle it belongs to and which attachments a vehicle carries. A typical example is a vehicle lift: it should only lift the vehicle; the attachments are attached to it and move along.
State bags
Section titled “State bags”| State bag | Entity | Meaning |
|---|---|---|
isElsPlusAttachment | the attachment | true: this entity is an ELS attachment |
elsAttachments | the carrier vehicle | List of attachments, each entry with netId |
You find the root vehicle via GetEntityAttachedTo: attachments are attached to the vehicle; with nested attachments you walk up the chain until the entity is no longer an attachment.
Example code
Section titled “Example code”-- Is the entity an ELS attachment?local function isElsAttachment(ent) return ent ~= 0 and DoesEntityExist(ent) and Entity(ent).state.isElsPlusAttachment == trueend
-- Find the root vehicle (walks up the attachment chain in case an attachment is attached to another attachment)local function elsRootVehicle(ent) while isElsAttachment(ent) do local parent = GetEntityAttachedTo(ent) if parent == 0 then break end ent = parent end return entend
-- All attachments of a vehicle as entitieslocal function elsAttachmentEntities(vehicle) local list = {} for _, a in ipairs(Entity(vehicle).state.elsAttachments or {}) do if a.netId and NetworkDoesNetworkIdExist(a.netId) then local e = NetworkGetEntityFromNetworkId(a.netId) if e ~= 0 then list[#list + 1] = e end end end return listendExample: vehicle lift
Section titled “Example: vehicle lift”local veh = elsRootVehicle(targetEntity)if isElsAttachment(targetEntity) then -- the target was an attachment: lift the vehicle underneath insteadend-- attachments do not need to be moved individually, they are attached to the vehicle and move along- Check
DoesEntityExistandNetworkDoesNetworkIdExist: an attachment may only just be spawning or, far away, not be streamed yet. - The state bags are meant for reading only. You change, place or delete attachments in the ELS editor, not via state bags.
- More state bags and exports: Server dev / extending.