Concepts/Entities_are_handles
Players, vehicles, objects, pickups, spheres, blips, NPCs and traffic are handle objects. They are tiny references to the real entity; they stay valid as long as the entity exists. You read and write properties with . and call methods with ():
local p = FindPlayer(0); // handle for player id 0, or null
if (p)
{
p.Health = 100.0; // set a property
local where = p.Pos; // get a property (a Vector)
p.SetWeapon(WEP_UZI, 200); // call a method
}- Looking up gives
nullwhen it does not exist. Always check (if (p)orp == null) before using a handle you obtained by id. - Reading a property on a handle whose entity is gone returns
null/0/false; writing is ignored. Unknown property names read asnulland writes to them are ignored (no error), so a typo fails silently. - Ids are reused. Do not store a player id and assume it is still the same person later - clear your tables in
onPlayerPart. - Handles compare by identity of the underlying id:
a.ID == b.IDis the safest test.
Categories: Core concepts