Skip to content

GameScript

Danila Rassokhin edited this page Jan 27, 2023 · 1 revision

GameScript

GameObject is a container for GameScript. But what is GameScript? Game script - module implementing some logic. It can be character system, inventory system or anything else.

Methods

GameScript has some methods, which are called by parent GameObject:

  • start()
    • Will be called automatically after game start (Game.start())
  • update(delta)
    • Will be called automatically on every update from Game, delta is a delta time between last and current update calls
  • stop()
    • Will be called automatically before game stopped (Game.stop())
  • dispose()
    • Will be called automatically after Game.dispose() call

Creating game scripts

Every game script class need to implement GameScript interface and must be annotated with @IsGameScript

//Simple Echo script
@IsGameScript
public class EchoSystem implements GameScript {

  //Parent GameObject this script will be attached to
  private GameObject parent;

  @Override
  public GameObject gameObject() {
    return parent;
  }

  @Override
  //Will be injected with parent game object while script instance creation
  public void setGameObject(GameObject parent) {
    this.parent = parent;
  }

  @Override
  public void update(long delta) {
    say(delta);
  }

  //This will be called on global game update
  private void say(Object message) {
    System.out.println(message);
  }
}

Script dependencies

Some script may depend on other scripts. You can specify dependencies in @RequiredGameScript annotation. This annotation already includes @IsGameScript, so you don't need to specify it implicitly. Required scripts will be attached to game object after this script creation.

//Simple Echo script
//Requires GameItemSystem script
@RequiredGameScript(GameItemSystem.class)
public class EchoSystem implements GameScript {

  //Parent GameObject this script will be attached to
  private GameObject parent;

  @Override
  public GameObject gameObject() {
    return parent;
  }

  @Override
  //Will be injected with parent game object while script instance creation
  public void setGameObject(GameObject parent) {
    this.parent = parent;
  }

  @Override
  public void update(long delta) {
    say(delta);
  }

  //This will be called on global game update
  private void say(Object message) {
    System.out.println(message);
  }
}

Script injection

To get dependencies from parent game object you can use @FromParent annotation on script fields. Fields will be injected with game scripts after this script creation. If there is no scripts exist of given type they will be created and attached to parent game object.

//Simple Echo script
//Requires GameItemSystem script
@RequiredGameScript(GameItemSystem.class)
public class EchoSystem implements GameScript {

  //Will inject GameItemSystem from parent GameObject
  @FromParent
  private GameItemSystem gameItemSystem;

  //Parent GameObject this script will be attached to
  private GameObject parent;

  @Override
  public GameObject gameObject() {
    return parent;
  }

  @Override
  //Will be injected with parent game object while script instance creation
  public void setGameObject(GameObject parent) {
    this.parent = parent;
  }

  @Override
  public void update(long delta) {
    say(delta);
  }

  //This will be called on global game update
  private void say(Object message) {
    System.out.println(message);
  }
}

So, if you need dependent script to use it in other script, you can use @FromParent without @RequiredGameScripts. If you need dependencies which won't be used in this script, you can use only @RequiredGameScripts.

Clone this wiki locally