Skip to content

Tutorials Component Game Object

Takumii edited this page Feb 21, 2014 · 3 revisions

HomeTutorialsComponent ▸ Game Object

Game object

What is a game object

A game object is an object in a world. It can be anything, an animal, a car, a building, ...

Game objects define a world and give a meaning to it. A world without game objects is an empty world.

A game object is nothing but only an id. It needs to have components to be something in particular.

How to create a game object

A game object can be created easily :

GameObject myGameObject = new GameObject();
myGameObject.Id = 1; // Assign an id to the game object

Once a game object is instantiated, you must provide an id if you plan to use it in a world.

How to add a game object in a world

To add a game object in a world, you have to use the Add method of the world instance :

World myWorld = new World();
GameObject myFirstObject = new GameObject() { Id = 1 };
GameObject mySecondObject = new GameObject() { Id = 2 };

myWorld.Add(myFirstObject);
myWorld.Add(mySecondObject);

When a game object is added to a world via the Add method, it isn't really active in the world. The game object is in a pending queue. You must call the Process method to notify the world of a change and add it definitely.

myWorld.Process(); // Process pending game object for add/remove

You aren't obligated to call Process each time you call Add. You can call Add to add all your objects and once everything is added you call Process only one time.

When you call Process, all components that are attached to a pending game object are also dispatched.

How to remove a game object from a world

To remove a game object from a world, you have to use the Remove method :

// Instance from the example of 'How to add a game object'
// myWorld is an instance of the World class
myWorld.Remove(myFirstObject);
myWorld.Remove(mySecondObject);

When a game object is removed from a world, it isn't really immediately removed. The game object is in a pending queue. You must call Process to notify the world of a change to remove it definitely.

myWorld.Process(); // Process pending game object for add/remove

When you call Process, all components that are attached to a pending game object are also dispatched.

How game objects are managed in a world

Each world has a GameObjectManager property. This property contains an instance of GameObjectManager class. This class is used to store all game objects of the world.

Within a GameObjectManager, each game object should have a unique id.

How to retrieve a game object

You can find a game object by its id :

// myWorld is an instance of the World class
GameObject myObject = myWorld.GameObjectManager.Get(1)

// or

GameObject myObject = myWorld.GameObjectManager[1];

How to retrieve the number of game object in a world

You can easily find the total number of game object in a world :

int objectCount = myWorld.GameObjectManager.Count;

How to implement a custom game object manager

In addition to the GameObjectManager instance provided by the world instance, you can add your own game object manager. This is useful if you need to regroup your game objects in a specific manner.

To create a custom game object manager, you have to implement the IGameObjectManager interface.

This interface has two methods :

// Called when a game object is added
void Added(GameObject gameObject);

// Called when a game object is removed
void Removed(GameObject gameObject);

Those two methods are called only when the Process method of world class has been called.

How to add a custom game object manager

Once you have implemented the IGameObjectManager interface, you have to add your game object manager to the world :

MyCustomManager myCustomManager = new MyCustomManager();
myWorld.AddManager(myCustomManager);

Now each time a game object is added or removed, the custom manager will be notified.

How to remove a custom game object manager

You can remove a custom game object manager from a world by calling RemoveManager :

myWorld.RemoveManage(myCustomManager);

// or

myWorld.RemoveManager(typeof(MyCustomManager));

// or

myWorld.RemoveManager<MyCustomManager>();