Skip to content

Step 2: Creating the System

ContronThePanda edited this page Oct 16, 2020 · 4 revisions

In this step, we will make a system. More information on systems can be found in Creating Systems. For this tutorial, we will create a class called TutorialSystem. The class header should look like this:

@RegisterSystem(RegisterMode.AUTHORITY)
public class TutorialSystem extends BaseComponentSystem

Extending BaseComponentSystem and having the @RegisterSystem annotation are both necessary for the class to be recognized as a system. The RegisterMode.AUTHORITY parameter means that this system is registered for server-side logic, which is the case for most systems that handle game logic.

SayComponent logic

Entities with the SayComponent should print out a message to the in-game console when they are activated with the E key. In order to implement this logic, we first need access to the console. This can be accomplished with this declaration in the class:

@In
private Console console;

The @In annotation automatically fills in the field when the system is activated, ensuring that it holds a reference to the game's console. Now, we can use it in an event handler to print the message to the console:

@ReceiveEvent
public void onActivateSay(ActivateEvent event, EntityRef entity, SayComponent sayComponent) {
    console.addMessage(sayComponent.message); 
}

The @ReceiveEvent annotation marks the method as an event handler. The first parameter, ActivateEvent event, indicates that this method receives ActivateEvents, so it will be triggered when the entity is activated. The second parameter is the EntityRef for the entity which is receiving the event; in this case, this is the entity being activated. The final parameter is the SayComponent on the entity. This parameter both filters the entities which can activate this handler to only those with the component and automatically retrieves the component. Inside the method, the code is relatively simple. The addMessage method prints out a string to the console, and the message used is the variable from the SayComponent.

DisplayComponent Logic

Entities with a DisplayComponent should display a floating number on top of them when activated. Activating it should toggle the display on or off. Here is the code for this component:

@ReceiveEvent
public void onActivateDisplay(ActivateEvent event, EntityRef entity, DisplayComponent dispComponent) {
    if(entity.hasComponent(FloatingTextComponent.class)) {
        entity.removeComponent(FloatingTextComponent.class);
    } else {
        FloatingTextComponent text = new FloatingTextComponent();
        text.text = dispComponent.num + "";
        text.isOverlay = true;
        entity.addOrSaveComponent(text);
    }
}

The parameters are essentially the same as for the SayComponent, except that the type of the third component has been changed to DisplayComponent. The code, however, is more complex. In order to add a floating number, the method adds and removes a FloatingTextComponent from the entity. The if-else block checks if the component is already present. If it is, it removes it. If it isn't, then it creates one, sets its variables, and adds it to the entity. This has the effect of toggling the display when activated. The text uses the value set in the DisplayComponent.

Previous: Step 1: Creating some Components

Next: Step 3: Creating Entities: Preliminary Work