Lua:Entity typing
Lua is a dynamically typed language--this means that a variable can hold any type of data at any time. C objects, such as those used in Fortress Forever, are stored as blocks of raw data. Lua can only access this data through functions that are made available by the API.
Often in a lua script an entity will be passed to the script as a CBaseEntity. This is a basic type of object used in the SDK, from which other types are derived. To gain access to more specific functions, we must cast the variable to a more specific type.
Entity Checking
Before casting to a more specific type of object, it's usually necessary to verify that the object exists, and is the kind of object we expect. Failing to do so can cause the script to fail and the game to crash. If these functions return true, it is safe to cast to that type of object.
These are pretty self-explanatory.
- IsPlayer( ent_id )
- IsDispenser( ent_id )
- IsSentrygun( ent_id )
- IsDetpack( ent_id )
- IsGrenade( ent_id )
- IsTurret( ent_id ) Meaning a respawn turret.
Casting Commands
A casting function returns the requested type of object, which you can assign to a new variable.
- CastToBeam( ent_id ) tries to cast the entity to a beam (to see if whatever triggered the event was a laser beam, a la SD2). If it fails, it returns null.
- CastToPlayer( ent_id ) used to cast the passed in entity to a player, often used for touch commands.
- CastToInfoScript( ent_id ) This may be a flag, resupply pack, or the like.
- CastToTriggerScript( ent_id ) This may be any kind of brush trigger that's referred to in an active lua script.
- CastToTriggerClip( ent_id ) Refers to a trigger_ff_clip
- CastToGrenade( ent_id )
- CastToDispenser( ent_id )
- CastToSentrygun( ent_id )
- CastToDetpack( ent_id )
Example
--This callback runs when something explodes inside the trigger name detpack_trigger. function detpack_trigger:onexplode( trigger_entity ) --We want to know if this mysterious exploding CBaseEntity is a detpack. If not we can skip the rest. if IsDetpack( trigger_entity ) then local detpack = CastToDetpack( trigger_entity ) --now that we have a detpack object, we can ask it what team it belongs to. if detpack:GetTeamId() == attackers then --This triggers a logic_relay in the map, which opens the hole and can trigger any other effect. OutputEvent( self.prefix .. "_detpack_relay", "Trigger" ) end end --This line tells all grenades, etc. that it's still OK to explode. return EVENT_ALLOWED end