Difference between revisions of "Lua:Entity typing"

From Fortress Forever Wiki
Jump to navigationJump to search
m
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Infobox manual/Header}}
 
{{Infobox manual/Header}}
 
{{Infobox_mapping}}
 
{{Infobox_mapping}}
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.
+
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 accessed as blocks of raw data. Because altering these objects incorrectly can cause the game to crash, It's important that the Lua system correctly knows what type of class it is dealing with.
  
 
==FF Object Types==
 
==FF Object Types==
===BaseEntity===
+
===CBaseEntity===
This is the basic type from which all other entities are derived. Consequently there's not much you can do with it. When naming a variable of this type, it's best to use the word "entity" to prevent confusion.
+
This is the basic type from which all other entities are derived. Consequently there's not a lot you can do with it--see [[:Category:Entity_functions]]. When naming an object of this type, it's best to use the word "entity" to prevent confusion.
  
 
The following commands will return a CBaseEntity:
 
The following commands will return a CBaseEntity:
 
*[[Lua:GetEntity|GetEntity]]( int ) Every entity in the game has a unique ID number. Use the [[Lua:GetId|GetId]]() function to access this number for a particular entity.
 
*[[Lua:GetEntity|GetEntity]]( int ) Every entity in the game has a unique ID number. Use the [[Lua:GetId|GetId]]() function to access this number for a particular entity.
 
*[[Lua:GetEntityByName|GetEntityByName]]( String )
 
*[[Lua:GetEntityByName|GetEntityByName]]( String )
 +
*String [[Lua:GetClassName|GetClassName]]() Returns the entity type as a string.
 
===InfoScript===
 
===InfoScript===
 
An InfoScript is created by using the [[Lua:info_ff_script | info_ff_script]]:new() command in lua, and placing an info_ff_script in a map. This is usually a game item such as a flag, ball, or supply pack,
 
An InfoScript is created by using the [[Lua:info_ff_script | info_ff_script]]:new() command in lua, and placing an info_ff_script in a map. This is usually a game item such as a flag, ball, or supply pack,
Line 33: Line 34:
  
 
The following commands will return a CFFTeam:
 
The following commands will return a CFFTeam:
*[[Lua:GetPlayer|GetTeam]]( teamId ) This returns a CFFTeam object when given a team number. See [[Lua:Flags#Team]].
+
*[[Lua:GetTeam|GetTeam]]( teamId ) This returns a CFFTeam object when given a team number. See [[Lua:Flags#Team]].
 
===CFFBuildableObject===
 
===CFFBuildableObject===
===CFFDispenser===
+
Objects that the player can place or build. Includes dispensers, sentry guns, mancannons, and detpacks. See [[:Category:Buildable_functions]].
===CFFSentryGun===
 
 
===CFFGrenadeBase===
 
===CFFGrenadeBase===
 +
Refers to thrown grenades
 +
*[[Lua:GetGrenade|GetGrenade]]() TODO: what parameters does this take?
 
===CTakeDamageInfo===
 
===CTakeDamageInfo===
 +
Damageinfo is an object that's passed along whenever a player is damaged or dies. By intercepting this object, you can detect events in the game, or change the outcome of those events. Damageinfo can be accessed from within the callbacks player_ondamage and player_killed. See [[:Category:Damageinfo_functions]].
  
 
==Entity Checking==
 
==Entity Checking==
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.
+
Often in a lua script an entity will be passed to the script as a CBaseEntity. To gain access to more specific functions, we must ''cast'' the variable to a more specific type. But before casting to a more specific type of object, it's usually necessary to verify that the object exists, and that it's the kind of object we expect. Failing to do so can cause the script to fail and the game to crash.
  
Before casting to a more specific type of object, it's usually necessary to verify that the object exists, and that it's 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.
+
If these functions return ''true'', it is safe to cast to that type of object.
 
+
{| border="1" cellspacing="0"
These are pretty self-explanatory.
+
! LUA Command !! Description
*[[Lua:IsPlayer|IsPlayer]]( ent_id )
+
|-
*[[Lua:IsDispenser|IsDispenser]]( ent_id )  
+
| [[Lua:IsPlayer|IsPlayer]]( ent_id ) || used to see if a passed in entity is a player to before actions are performed on said player.
*[[Lua:IsSentrygun|IsSentrygun]]( ent_id )
+
|-
*[[Lua:IsDetpack|IsDetpack]]( ent_id )
+
| [[Lua:IsEntity|IsEntity]]( ent_id ) ||
*[[Lua:IsGrenade|IsGrenade]]( ent_id )
+
|-
*[[Lua:IsTurret|IsTurret]]( ent_id ) Meaning a respawn turret.
+
| [[Lua:IsInfoScript|IsInfoScript]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsDispenser|IsDispenser]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsSentrygun|IsSentrygun]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsDetpack|IsDetpack]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsGrenade|IsGrenade]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsProjectile|IsProjectile]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsBuildable|IsBuildable]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsJumpPad|IsJumpPad]]( ent_id ) ||
 +
|-
 +
| [[Lua:IsTurret|IsTurret]]( ent_id ) || Refers to respawn turrets.
 +
|}
  
 
==Casting Commands==
 
==Casting Commands==
 
A casting function accepts a CBaseEntity and returns the requested type of object, which you can assign to a new variable.
 
A casting function accepts a CBaseEntity and returns the requested type of object, which you can assign to a new variable.
*[[Lua:CastToBeam|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.
+
{| border="1" cellspacing="0"
*[[Lua:CastToPlayer|CastToPlayer]]( ent_id ) used to cast the passed in entity to a player, often used for touch commands.
+
! LUA Command !! Description
*[[Lua:CastToInfoScript|CastToInfoScript]]( ent_id ) This may be a flag, resupply pack, or the like.
+
|-
*[[Lua:CastToTriggerScript|CastToTriggerScript]]( ent_id ) This may be any kind of brush trigger that's referred to in an active lua script.
+
| [[Lua:CastToBeam|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.
*[[Lua:CastToTriggerClip|CastToTriggerClip]]( ent_id ) Refers to a trigger_ff_clip
+
|-
*[[Lua:CastToGrenade|CastToGrenade]]( ent_id )  
+
| [[Lua:CastToPlayer|CastToPlayer]]( ent_id ) || used to cast the passed in entity to a player, often used for touch commands.
*[[Lua:CastToDispenser|CastToDispenser]]( ent_id )  
+
|-
*[[Lua:CastToSentrygun|CastToSentrygun]]( ent_id )  
+
| [[Lua:CastToInfoScript|CastToInfoScript]]( ent_id ) || This may be a flag, resupply pack, or the like.
*[[Lua:CastToDetpack|CastToDetpack]]( ent_id )
+
|-
 +
| [[Lua:CastToTriggerScript|CastToTriggerScript]]( ent_id ) || This may be any kind of brush trigger that's referred to in an active lua script.
 +
|-
 +
| [[Lua:CastToTriggerClip|CastToTriggerClip]]( ent_id ) || Refers to a [[Lua:trigger_ff_clip|trigger_ff_clip]].
 +
|-
 +
| [[Lua:CastToGrenade|CastToGrenade]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToDispenser|CastToDispenser]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToSentrygun|CastToSentrygun]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToDetpack|CastToDetpack]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToBuildable|CastToBuildable]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToProjectile|CastToProjectile]]( ent_id ) ||
 +
|-
 +
| [[Lua:CastToJumpPad|CastToJumpPad]]( ent_id ) ||
 +
|}
  
 
==Example==
 
==Example==

Latest revision as of 12:55, 5 April 2015


Mapping for FF
The Basics

Setting up Hammer
Getting Started With Lua
Releasing a map

FF-specific Entities

Lua location system

Map Templates
FF Lua Documentation

Entity Typing
Entity Collections

Commands
Callbacks

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 accessed as blocks of raw data. Because altering these objects incorrectly can cause the game to crash, It's important that the Lua system correctly knows what type of class it is dealing with.

FF Object Types

CBaseEntity

This is the basic type from which all other entities are derived. Consequently there's not a lot you can do with it--see Category:Entity_functions. When naming an object of this type, it's best to use the word "entity" to prevent confusion.

The following commands will return a CBaseEntity:

  • GetEntity( int ) Every entity in the game has a unique ID number. Use the GetId() function to access this number for a particular entity.
  • GetEntityByName( String )
  • String GetClassName() Returns the entity type as a string.

InfoScript

An InfoScript is created by using the info_ff_script:new() command in lua, and placing an info_ff_script in a map. This is usually a game item such as a flag, ball, or supply pack,

The following commands will return an InfoScript:

TriggerScript

This is similar to InfoScript, but it's brush-based. Cap points and nobuild zones are Trigger scripts.

The following commands will return a TriggerScript:

CFFPlayer

This refers to any client on the server. See Category:Player_functions For a list of what you can do with CFFPlayer. The following commands will return a CFFPlayer:

  • GetPlayer( entity ) --This would appear to do the same thing as CastToPlayer (see below). Use that instead.
  • GetPlayerByID( int )

CFFTeam

This refers to one of six teams--"Unassigned" and Spectators, in addition to Blue, Red, Yellow, and Green. See Category:Team_functions For a list of what you can do with CFFTeam.

The following commands will return a CFFTeam:

CFFBuildableObject

Objects that the player can place or build. Includes dispensers, sentry guns, mancannons, and detpacks. See Category:Buildable_functions.

CFFGrenadeBase

Refers to thrown grenades

  • GetGrenade() TODO: what parameters does this take?

CTakeDamageInfo

Damageinfo is an object that's passed along whenever a player is damaged or dies. By intercepting this object, you can detect events in the game, or change the outcome of those events. Damageinfo can be accessed from within the callbacks player_ondamage and player_killed. See Category:Damageinfo_functions.

Entity Checking

Often in a lua script an entity will be passed to the script as a CBaseEntity. To gain access to more specific functions, we must cast the variable to a more specific type. But before casting to a more specific type of object, it's usually necessary to verify that the object exists, and that it's 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.

LUA Command Description
IsPlayer( ent_id ) used to see if a passed in entity is a player to before actions are performed on said player.
IsEntity( ent_id )
IsInfoScript( ent_id )
IsDispenser( ent_id )
IsSentrygun( ent_id )
IsDetpack( ent_id )
IsGrenade( ent_id )
IsProjectile( ent_id )
IsBuildable( ent_id )
IsJumpPad( ent_id )
IsTurret( ent_id ) Refers to respawn turrets.

Casting Commands

A casting function accepts a CBaseEntity and returns the requested type of object, which you can assign to a new variable.

LUA Command Description
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 )
CastToBuildable( ent_id )
CastToProjectile( ent_id )
CastToJumpPad( 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