<span class="p">[</span><span class="nc">Attribute</span><span class="p">(</span>defvalue<span class="p">:</span> <span class="s">"1"</span><span class="p">,</span> desc<span class="p">:</span> <span class="s">"Print a message when player is null"</span><span class="p">)]</span>
<span class="om">Print</span><span class="p">(</span><span class="s">"Only one instance of TAG_PrintPlayerPositionEntity is allowed in the world!"</span><span class="p">,</span> <span class="nc">[./enfusion://ScriptEditor/scripts/Core/generated/Debug/LogLevel.c%3B13 LogLevel]</span><span class="p">.</span><span class="c">WARNING</span><span class="p">);</span>
A World Editor entity is a scripted entity that can be placed from the World Editor's Create tab.
In this example, we will create an Entity that once placed in the world, will print the player's position with a certain frequency.
Declaration
Entity
Create a new file and name it as your entity - here, we will go with TAG_PrintPlayerPositionEntity so the file should be TAG_PrintPlayerPositionEntity.c.
ⓘ
By convention all Entities classnames must end with the Entity suffix.
⚠
An entity script file must be created in the Game module (scripts/Game), otherwise it will not be listed in the Entities list!
An Entity requires an Entity Class declaration. This allows it to be visible in World Editor.
The name must be exactly the Entity name suffixed by Class, here TAG_PrintPlayerPositionClass.
An Entity Class is usually placed just above the Entity definition as such:
[EntityEditorProps(category:"Tutorial Entities", description:"Prints player's position regularly")]classTAG_PrintPlayerPositionEntityClass:GenericEntityClass{}classTAG_PrintPlayerPositionEntity:GenericEntity{}
The class is decorated using EntityEditorProps; the category is where the Entity will be found in World Editor's Create tab - see below.
EntityEditorProps
category
the "Create" tab's category in which the Entity can be found
description
unused (for now)
color
the bounding box's unselected line colour - useful only when visible is set to true
visible
have the bounding box always visible - drawn in color
configRoot
unused
icon
unused: direct path to a png file, e.g WBData/EntityEditorProps/entityEditor.png
style
can be "none", "box", "sphere", "cylinder", "capsule", "pyramid", "diamond"
sizeMin
bounding box's lower dimensions
sizeMax
bounding box's higher dimensions
color2
the bounding box's surface colour
dynamicBox
enables the entity visualiser using custom dimensions (provided by _WB_GetBoundBox)
Filling
The Entity is now visible in World Editor, the next step is to make it do something.
Add Code
Let's use the IEntity's constructor to call code.
classTAG_PrintPlayerPositionEntity:GenericEntity{protectedfloatm_fWaitingTime=float.MAX;// trigger Print on startprotectedintm_iCycleDuration= 10;// in seconds//------------------------------------------------------------------------------------------------protectedvoidPrintPlayerPosition(){PlayerController playerController =GetGame().GetPlayerController();if(!playerController)return;IEntity player = playerController.GetControlledEntity();if(!player){Print("Player entity position: no player",LogLevel.NORMAL);return;}Print("Player entity position: "+ player.GetOrigin(),LogLevel.NORMAL);}//------------------------------------------------------------------------------------------------overridevoidEOnFrame(IEntity owner,float timeSlice){m_fWaitingTime+= timeSlice;if(m_fWaitingTime<m_iCycleDuration)return;m_fWaitingTime= 0;PrintPlayerPosition();}//------------------------------------------------------------------------------------------------voidTAG_PrintPlayerPositionEntity(IEntitySource src,IEntity parent){SetFlags(EntityFlags.ACTIVE | EntityFlags.NO_LINK,false);SetEventMask(EntityEvent.FRAME);}}
Make It Unique
Let's assume we do not want the Print to be displayed multiple times in the case someone placed multiple Entities in the world.
ⓘ
A singleton is an entity that can only be instanciated once. See Singleton pattern.
For that we will use the static keyword to keep a single reference:
classTAG_PrintPlayerPositionEntity:GenericEntity{// other propertiesprotectedstaticTAG_PrintPlayerPositionEntitys_Instance;// other methods//------------------------------------------------------------------------------------------------voidTAG_PrintPlayerPositionEntity(IEntitySource src,IEntity parent){if(s_Instance){Print("Only one instance of TAG_PrintPlayerPositionEntity is allowed in the world!",LogLevel.WARNING);deletethis;return;}s_Instance=this;// rest of the init code}}
Add Properties
Now, we can declare properties with the Attribute in order to be able to adjust some settings from the World Editor interface. The following code only contains the added attributes:
classTAG_PrintPlayerPositionEntity:GenericEntity{[Attribute(defvalue:"1", desc:"Print player position")]protectedboolm_bPrintPlayerPosition;[Attribute(defvalue:"1", desc:"Print a message when player is null")]protectedboolm_bPrintWhenPlayerIsNull;[Attribute(defvalue:"1", uiwidget:UIWidgets.Slider, desc:"Print cycle period (in seconds)", params:"1 30 1")]protectedintm_iCycleDuration;}
The following code contains code with the implemented attributes:
classTAG_PrintPlayerPositionEntity:GenericEntity{[Attribute(defvalue:"1", desc:"Print player position")]protectedboolm_bPrintPlayerPosition;[Attribute(defvalue:"1", desc:"Print a message when player is null")]protectedboolm_bPrintWhenPlayerIsNull;[Attribute(defvalue:"1", uiwidget:UIWidgets.Slider, desc:"Print cycle period (in seconds)", params:"1 30 1")]protectedintm_iCycleDuration;// other properties//------------------------------------------------------------------------------------------------protectedvoidPrintPlayerPosition(){PlayerController playerController =GetGame().GetPlayerController();if(!playerController)return;IEntity player = playerController.GetControlledEntity();if(!player){if(m_bPrintWhenPlayerIsNull)Print("Player entity position: no player",LogLevel.NORMAL);return;}Print("Player entity position: "+ player.GetOrigin(),LogLevel.NORMAL);}// other methods//------------------------------------------------------------------------------------------------voidTAG_PrintPlayerPositionEntity(IEntitySource src,IEntity parent){if(!m_bPrintPlayerPosition){deletethis;return;}// rest of the init code}}
Now all there is to do is to place one TAG_PrintPlayerPositionEntity entity in the world and see the player's position printed in logs!
ⓘ
In order for the entity to appear in World Editor, scripts must be compiled and reloaded via⇧ Shift + F7.
Final Code
The final file content can be found here:
[EntityEditorProps(category:"Tutorial Entities", description:"Prints player's position regularly", color:"0 255 0 0")]classTAG_PrintPlayerPositionEntityClass:GenericEntityClass{}classTAG_PrintPlayerPositionEntity:GenericEntity{[Attribute(defvalue:"1", desc:"Print player position")]protectedboolm_bPrintPlayerPosition;[Attribute(defvalue:"1", desc:"Print a message when player is null")]protectedboolm_bPrintWhenPlayerIsNull;[Attribute(defvalue:"1", uiwidget:UIWidgets.Slider, desc:"Print cycle period (in seconds)", params:"1 30 1")]protectedintm_iCycleDuration;protectedfloatm_fWaitingTime=float.MAX;// trigger Print on startprotectedstaticTAG_PrintPlayerPositionEntitys_Instance;//------------------------------------------------------------------------------------------------protectedvoidPrintPlayerPosition(){PlayerController playerController =GetGame().GetPlayerController();if(!playerController)return;IEntity player = playerController.GetControlledEntity();if(!player){if(m_bPrintWhenPlayerIsNull)Print("Player entity position: no player",LogLevel.NORMAL);return;}Print("Player entity position: "+ player.GetOrigin(),LogLevel.NORMAL);}//------------------------------------------------------------------------------------------------overridevoidEOnFrame(IEntity owner,float timeSlice){m_fWaitingTime+= timeSlice;if(m_fWaitingTime<m_iCycleDuration)return;m_fWaitingTime= 0;PrintPlayerPosition();}//------------------------------------------------------------------------------------------------voidTAG_PrintPlayerPositionEntity(IEntitySource src,IEntity parent){if(s_Instance){Print("Only one instance of TAG_PrintPlayerPositionEntity is allowed in the world!",LogLevel.WARNING);deletethis;return;}if(!m_bPrintPlayerPosition){deletethis;return;}s_Instance=this;SetFlags(EntityFlags.ACTIVE | EntityFlags.NO_LINK,false);SetEventMask(EntityEvent.FRAME);}}