Create an Entity – Arma Reforger
Lou Montana (talk | contribs) m (Fix category) |
Lou Montana (talk | contribs) (Change from CallQueue().CallLater to EOnFrame) |
||
Line 49: | Line 49: | ||
class TAG_PrintPlayerPositionEntity : GenericEntity | class TAG_PrintPlayerPositionEntity : GenericEntity | ||
{ | { | ||
protected float m_fWaitingTime = float.MAX; // trigger Print on start | |||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
protected void PrintPlayerPosition() | protected void PrintPlayerPosition() | ||
Line 64: | Line 66: | ||
Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | ||
} | |||
//------------------------------------------------------------------------------------------------ | |||
override void EOnFrame(IEntity owner, float timeSlice) | |||
{ | |||
m_fWaitingTime += timeSlice; | |||
if (m_fWaitingTime < m_iCycleDuration) | |||
return; | |||
m_fWaitingTime = 0; | |||
PrintPlayerPosition(); | |||
} | } | ||
Line 69: | Line 82: | ||
void TAG_PrintPlayerPositionEntity(IEntitySource src, IEntity parent) | void TAG_PrintPlayerPositionEntity(IEntitySource src, IEntity parent) | ||
{ | { | ||
SetFlags(EntityFlags.ACTIVE | EntityFlags.NO_LINK, false); | |||
SetEventMask(EntityEvent.FRAME); | |||
} | } | ||
}; | }; | ||
Line 85: | Line 98: | ||
class TAG_PrintPlayerPositionEntity : GenericEntity | class TAG_PrintPlayerPositionEntity : GenericEntity | ||
{ | { | ||
// other properties | |||
protected static TAG_PrintPlayerPositionEntity s_Instance; | protected static TAG_PrintPlayerPositionEntity s_Instance; | ||
// | // other methods | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 120: | Line 131: | ||
protected bool m_bPrintPlayerPosition; | protected bool m_bPrintPlayerPosition; | ||
[Attribute(defvalue: "1", desc: "Print | [Attribute(defvalue: "1", desc: "Print a message when player is null")] | ||
protected bool | protected bool m_bPrintWhenPlayerIsNull; | ||
[Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 1")] | [Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 1")] | ||
Line 128: | Line 139: | ||
</enforce> | </enforce> | ||
The following code contains | The following code contains code with the implemented attributes: | ||
<enforce> | <enforce> | ||
Line 136: | Line 147: | ||
protected bool m_bPrintPlayerPosition; | protected bool m_bPrintPlayerPosition; | ||
[Attribute(defvalue: "1", desc: "Print | [Attribute(defvalue: "1", desc: "Print a message when player is null")] | ||
protected bool | protected bool m_bPrintWhenPlayerIsNull; | ||
[Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 | [Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 1")] | ||
protected int m_iCycleDuration; | protected int m_iCycleDuration; | ||
// other properties | |||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 154: | Line 165: | ||
if (!player) | if (!player) | ||
{ | { | ||
if ( | if (m_bPrintWhenPlayerIsNull) | ||
Print("Player entity position: no player", LogLevel.NORMAL); | Print("Player entity position: no player", LogLevel.NORMAL); | ||
return; | return; | ||
Line 161: | Line 172: | ||
Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | ||
} | } | ||
// other methods | |||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 193: | Line 206: | ||
protected bool m_bPrintPlayerPosition; | protected bool m_bPrintPlayerPosition; | ||
[Attribute(defvalue: "1", desc: "Print | [Attribute(defvalue: "1", desc: "Print a message when player is null")] | ||
protected bool | protected bool m_bPrintWhenPlayerIsNull; | ||
[Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 1")] | [Attribute(defvalue: "1", uiwidget: UIWidgets.Slider, desc: "Print cycle period (in seconds)", params: "1 30 1")] | ||
protected int m_iCycleDuration; | protected int m_iCycleDuration; | ||
protected float m_fWaitingTime = float.MAX; // trigger Print on start | |||
protected static TAG_PrintPlayerPositionEntity s_Instance; | protected static TAG_PrintPlayerPositionEntity s_Instance; | ||
Line 211: | Line 226: | ||
if (!player) | if (!player) | ||
{ | { | ||
if ( | if (m_bPrintWhenPlayerIsNull) | ||
Print("Player entity position: no player", LogLevel.NORMAL); | Print("Player entity position: no player", LogLevel.NORMAL); | ||
return; | return; | ||
Line 217: | Line 232: | ||
Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | Print("Player entity position: " + player.GetOrigin(), LogLevel.NORMAL); | ||
} | |||
//------------------------------------------------------------------------------------------------ | |||
override void EOnFrame(IEntity owner, float timeSlice) | |||
{ | |||
m_fWaitingTime += timeSlice; | |||
if (m_fWaitingTime < m_iCycleDuration) | |||
return; | |||
m_fWaitingTime = 0; | |||
PrintPlayerPosition(); | |||
} | } | ||
Line 237: | Line 263: | ||
s_Instance = this; | s_Instance = this; | ||
SetFlags(EntityFlags.ACTIVE | EntityFlags.NO_LINK, false); | |||
SetEventMask( EntityEvent.FRAME); | |||
} | } | ||
}; | }; |
Revision as of 18:03, 14 November 2022
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.
Entity Class
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:
The class is decorated using EntityEditorProps; the category is where the Entity will be found in World Editor's Create tab.
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.
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.
For that we will use the static keyword to keep a single reference:
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:
The following code contains code with the implemented attributes:
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!
Final Code
The final file content can be found here: