Skip to content

@EngineObject Singletons

Zach Goethel edited this page Nov 4, 2020 · 2 revisions

Engine objects are instances of core objects which are created, updated, and managed by the game engine. There are a few ways to access the instances, and custom engine objects can be created during game development.

Each instance is globally shared everywhere in one game context (in the same thread).

Creating new Engine Objects

Classes annotated with @RegisterObject will automatically be created when the game boots. The engine will create an instance of the following class automatically (with no additional calls in the main game class):

@RegisterObject
public class MyEngineObject
{
    // Values in this class will be kept in the automatically created instance.
    
    // Since this is a singleton object, any class which accesses this engine
    // object will have access to the same instance and same values.
    public String globalValue = "All classes have access to read and write to this.";
}

Initializable

If the registered class also implements the Initializable interface, the engine will automatically call initialize() when the game boots.

Initialization will be called after the graphical context and basic functions have been initialized (so this interface can be used to set up menus and levels):

@RegisterObject
public class MyEngineObject implements Initializable
{
    @Override
    public void initialize()
    {
        // Set up things here; called only once while the game is booting
    }
}

Updatable

Similarly, classes implementing Updatable will be updated each engine frame. Updates are performed after the screen buffers are cleared and before the window buffers are swapped. This means objects can be rendered from this method, but there is little guarantee that rendering will execute in the expected order.

@RegisterObject
public class MyEngineObject implements Initializable, Updatable
{
    @Override
    public void initialize()
    {
        // Set up things here; called only once when the game is booting
    }

    @Override
    public void update()
    {
        // Update/render things here; called once every frame
    }
}

Accessing Engine Objects

Instances of engine objects (including basic engine utilities, any classes annotated with @RegisterObject, and the game instance itself) can be accessed through several channels.

  • Create a class field annotated with @EngineObject in one of the following locations:
    • Any class annotated with @RegisterObject
    • The main game class (which is a game object itself)
    • Any class which extends EngineAware (an abstract class)
  • Manually access it via EngineObjectsImpl.get<MyEngineObject>().get(0)
    • This is not recommended as it is implementation
    • This is an inline function which only works in Kotlin

Code Examples for the @EngineObject Annotation

@RegisterObject
public class MyEngineObject
{
    // This field's value will automatically be set to the instance of
    // the main game class (or whatever class the type indicates)
    @EngineObject
    private GameMainClass gameInstance;

    // . . .
}
public class GameMainClass implements CheckGame
{
    // This field's value will automatically be set to the instance of
    // MyEngineObject (or whatever class the type indicates)
    @EngineObject
    private MyEngineObject engineObject;

    // . . .
}
public class MyEngineAwareObject extends EngineAware
{
    // This field's value will automatically be set for any instances created
    // of this type (any time a MyEngineAwareObject is created)
    @EngineObject
    private MyEngineObject engineObject;

    // . . .
}