Skip to content

GameObject

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

GameObject

GameObject is an interface represents game object - abstract container with some scripts. Each game object uses single responsibility principle. For example GameObject called character should contains scripts for character creation and etc. Basic implementation of GameObject called BasicGameObject where every object has it's own unique id.

Game object creation

//Create new game object
GameObject myObj = game.addGameObject();
//Get object id
Long myObjId = myObj.getId();
//Removes object from game and calls dispose() on it
game.removeGameObject(myObj);

Attaching scripts

Every script in each game object is unique. It means, that game object may have only one script instance of some type.

//Attach new script of type EchoSystem or get already attached script
EchoSystem echoSystem = myObj.getGameScript(EchoSystem.class);

GameItem gameItem = diContainer.getBean("simpleGameItem", GameItem.class);
//Attach new script of type GameItemSystem and use gameItem object in GameItemSystem class constructor
GameItemSystem itemSystem = myObj.getGameScript(GameItemSystem.class, gameItem);

//Check if script of given type is attached
boolean hasItemSystem = myObj.hasGameScript(GameItemSystem.class);
//Get all scripts attached to this object
Set<GameScript> allScripts = myObj.getGameScripts();
//Detaches script from game object
myObj.removeGameScript(GameItemSystem.class);

Updating GameObject

GameObject has method called update, which invokes update on each GameScript attached to this object. This method will be called automatically on each GameObject after calling update in Game.

Disposing objects

Every GameObject has dispose() method. It contains logic for game object disposing. In BasicGameObject it removes all game scripts for given object. dispose() will be called automatically on game.removeGameObject(object)

//Dispose object manually
myObj.dispose();
//Remove object from game and dispose() it
game.removeGameObject(myObj);
Clone this wiki locally