views:

96

answers:

2

First of all hello, this is my first time here. I'm young coder (15) and this is my second attempt to make a game (actually whole reusable game engine AND framework, not sure how to call it, maybe GDK?), while making it much more organized this time.

IMPORTANT NOTE: I'm using XNA framework, so the game engine is not purely low level DirectX management! I'm trying to expand XNA with predefined classes that will be easy to insert into any game and manage later on!

I got stuck at finding a way to put an Entity into WorldManager's list, later retrieve the entity for direct changes, and then put it back to the list (so it's used with Update method).

My ideas (and questions) were:

  1. Best possible solution would be to somehow make the entity linked with the list, so as I update the lone entity object it updates in the list too. Unfortunately I don't know how to do it, and this could be the best answer.

  2. What I also thought about was to edit the entity I created first, and then use find to look it up in the list and update. The problem is that I'm unsure of how the find method searches. What if any change can make method think it's a whole new different object? (How does find actually find?)

  3. What I did was to add IDs to Entities at the moment they're added to the list, so I can put them back in their place after editing. The problem is that I've got remove method too, and I'm unsure would it change the indexes, because if it would then I'd have to make re-update IDs every time I remove an entity. I find that quite inefficient. So would List<>.Remove change the indexes? (I guess it would)

These are actually 3 questions in one, and I'd appreciate answer on any of these, of course answers to topmost questions will be more appreciated!

A piece of code to get idea how does it all work:

public class WorldManager
{
    List<Entity> Entities = new List<Entity>();
    List<Entity> ToAdd_Entities = new List<Entity>();
    List<Entity> ToRemove_Entities = new List<Entity>();
...
    public void Update()
    {
        for(int count = 0; count < ToAdd_Entities.Count; count++)
        {
            Entities.Add(ToAdd_Entities[count]);
            int index = Entities.IndexOf(ToAdd_Entities[count]);
            Entities[index].ID = index;

            ToAdd_Entities.Remove(ToAdd_Entities[count]);
        }

        for (int count = 0; count < ToRemove_Entities.Count; count++)
        {
            Entities.Remove(ToRemove_Entities[count]);
            ToAdd_Entities.Remove(ToAdd_Entities[count]);

            //index update is required here too, I just didn't implement it yet.
        }
    ...
   }
}

How would I access it:

List<Entity> bots = WorldManager.GetEntitiesByName("bot");
bots[1].Location = new Vector2(x,y);
WorldManager.UpdateEntities(bots);

I don't use that piece of code, that's just a plain example of my idea.

Please forgive me for my confusing English (if you find it confusing), I'm a Serbian.

Thanks in advance.

A: 

I suggest to not go with the "one big list where every object is in"-approach. Rather focus on input, graphics and other basic stuff in the engine, then leave the rest to game specific classes.

Imagine you've got a 2D World with a lot of trees in it, they're all mostly static. Now you've got 5k of them sitting there in your Entities List doing exactly nothing, instead of putting them there, you should rather put them in a List somewhere in your Map class, this way they are easier to access(you don't have to go through the whole list to find them) and design gets a lot cleaner too. This way you don't have to code a whole class of generic handler code, which in the end still doesn't fit to every possible object you could throw into your World.

So the 3 tips from my side are: - Put only non game specific code in the engine(Graphics, Sound, Input) - From the engine only call 2 methods in your game, like e.g. updateGame() and renderGame() which then take care of the rest - And last but not least: Keep the components of your game separated from another, only let them interact where it's really needed.

Also important, build some games, then look which code they really share and from this code form your engine.

If you want an example of how this could be done(though I don't guaranty that it's perfect) you can take a look at my small Engine here. It's Java and not really good commented, but it should give you a basic idea on how to structure your engine. If you want to see a game I've build with the engine take a look at the downloads section.

Ivo Wetzel
That's nice way of thinking, but I was wrong I wanted to tell I'm making whole framework. I actually need straightforward answers to my question. Why? Because my game is actually gonna be simple top-down 2D game, so it won't have any problems with maintaining all the entities in one list. That list works like "Event handler" it calls update globally on every single entity.
Johnny
I took a closer look to your engine, got partially how it works, but I'll get it when I read it more thoroughly. It's still not the answer I was looking for, but I'd give vote up for your effort and even an example, unfortunately I don't have enough rep.I've recently read something about component based entity management, and I'd appreciate if you could find me any examples of such frameworks.
Johnny
A: 

Well I've got answer on my topmost solution! Got it from another question of mine, and thought I should place solution here too.

The answer is that as long as I added it to the list, it's already linked, because Entity is a class and it's objects are "reference types", so when I assign them to some other object, or a list in this case, they're linked to it. Any changes I make to that object is automatically updated on other side!

(Someone correct me if I'm wrong. I'll check this out as soon as I put my framework/engine to test, and let you know is this "the" solution.)

Johnny