Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've previously made a game using a system like this (psuedo code):

ConstructGameObjects()
{
   //these are all globals
   player = new Player;
   enemies = new list;
}

updategame()
{
    player.update();
    for (enemy: enemies) enemy.update();
    if(time to create enemy) createEnemy();
}

createEnemy()
{
    Enemy newEn = new Enemy(player);
    enemies.add(newEn);
}

render(Canvas c)
{
    player.render(c);
   //etc. etc.
}

Obviously this became ridicously large and annoying to deal with when the game got to contain around just 30 different things to update.

So after reading various articles, I decided to go with a generic Entity object (that has components). Any entity in the world would be added to a entity list, which would update, render, etc. If I wanted to have new enemies be created during the game, then at the beginning of the game I would add something like EnemyGenerator which would have it's own creation logic built in, and would add enemies to the entity list as needed. The problem I ran into was, how would I get access to the player object now? Before, there was a global I could call from anywhere. How should I go about solving this? I have a few ideas but I'd like to see what other people say.

share|improve this question
Well, if you cant manage a global to be local, then maybe you won't get much result making this entity component based system lol – Gustavo Maciel Jan 30 '12 at 3:29
In an entity system, every entity should have an unique identifier. (Ideally, your entity factory would return the ID of the object when/whenever an entity is created.) All you'd have to do is store the id of your player in a variable. – sarahm Jan 30 '12 at 7:56
1  
You could make a class that keeps explicit references to entities you may need to do specific things on. Still have them update, render, etc. "automatically" within your entity system, but this class would be special references you may need to do special things with. – Scott W Jan 30 '12 at 11:29

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.