Create a Component – Arma Reforger
A World Editor Component is a code element that can be placed as a child (well, as a component) of an entity from the World Editor's Add Component button.
In this example, we will create a Component that teleports humans if they get too close.
Declaration
Component
Create a new file and name it as your component - here, we will go with TAG_TeleportFieldComponent so the file should be TAG_TeleportFieldComponent.c.
Component Class
Like an Entity, a Component requires a Component Class declaration. This allows it to be visible in World Editor. The name must be exactly the Component name suffixed by Class, here TAG_TeleportFieldComponentClass. A Component Class is usually placed just above the Component definition as such:
The class is decorated using ComponentEditorProps; the category is where the Component will be found using e.g the Add Component button - see below.
ComponentEditorProps
- category
- the "Create" tab's category in which the Component 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
- insertable
- configRoot
- unused
- icon
- set the component's icon in World Editor's UI - direct path to a png file, e.g icon: "WBData/ComponentEditorProps/componentEditor.png"
Filling
The Component is now visible in World Editor's "Add Component" UI, the next step is to make it do something.
Add Code
Let's use the Component's OnPostInit() method to call code.
This code does multiple things:
- in OnPostInit() the nearby characters array is initialised and the "on frame" event mask is set, allowing EOnFrame to be executed every frame
- in EOnFrame() we query entities by sphere (by straight line distance from point to point) and fill the nearby characters array through the QueryEntitiesCallbackMethod() callback method
- still in EOnFrame() we move all detected entities 5 metres back using in IEntity.SetOrigin()
Add Properties
Now, we can declare properties with the Attribute decorator in order to be able to adjust some settings from the World Editor interface. The following code only contains the added attributes:
These attributes's implementation can be a good exercise. As you may have noticed some additional attributes made their way to the code.
The goal is to draw a line from the object to the entity in order to warn them they will be teleported if they keep on getting closer. You can try to figure out how to do it properly then take a peek at Final Code to see one possible solution.
Now all there is to do is to attach a TAG_TeleportFieldComponent component to a world entity and see its effects!
Final Code
The final file content can be found here: