Managing game objects/components
Posted
by
Xeon06
on Game Development
See other posts from Game Development
or by Xeon06
Published on 2011-03-01T23:21:04Z
Indexed on
2011/03/01
23:32 UTC
Read the original article
Hit count: 178
engine
|component-based
Good day everyone,
By far the biggest problem that has always dawned on my when programming games is how to structure my code. It just becomes an incredible mess after a while. The reason for that is because I have no idea how different classes should interact with each other. Let's have an example.
Say I have a class Player
, a class PlayerInput
and a class Map
. The player class contains information as to the location of the player, whereas the player input class handles changing that location, but by first making sure it's within a walkable area from the map class. How to structure this?
My usual approach is to pass those components as parameters in the constructors of the parameters that need them, like so:
var map = new Map(); var player = new Player(); var input = new PlayerInput(player, map);
The problem with that is that it quickly gets messy, when you add new components you have to go through your constructors and update them, and it doesn't work well if you have mirroring references:
var physics = new Physics(input); //Oops, doesn't work
var input = new Input(physics);
So, how do you guys usually manage this?
Thanks.
© Game Development or respective owner