Sign in to follow this  

Pre alloc game objects

This topic is 2547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. First off, my english isn't that good so my questions/explanations may seem a little confusing :)
I have a hard time deciding how I should pre allocate game objects. Its easy if I use the C style approach of having the same struct for every object and just allocate an array of it for each layer of objects. But I find it harder to do this in OOP (C#) with lots of different (derived) classes. Do you create a seperate array for every kind of object (making a generic array class) ? Or is it sensible to make an array class that reserves an array of the base class where I can add sprites of different types and then call a GetObject method that returns an object by a given type and makes a downcast by the caller:


class ObjectArray
{
public Object[] objects;
public int capacity;
public int size;

public ObjectArray(int capacity)
{
this.capacity = capacity;
this.size = 0;
this.objects = new Object[capacity];
}

public void AddObject(Object spr)
{
if (size == capacity)
return;
objects[size++] = spr;
}

public Object GetObject(byte type)
{
for (int i = 0; i < size; i++)
{
if (objects[i].type == type)
return objects[i];
}
return null;
}
}


void pseudoMethod()
{
AnEnemy enm = (AnEnemy)enemyObjects.GetObject(ObjectTypes.AN_ENEMY);
enm.state = ObjectStates.ALIVE;
enm.CustomInitMethod();
}



Share this post


Link to post
Share on other sites
Good question. In C++ I might be tempted to make an array of chunks of memory that are the size of the largest object type but in C# I'm not sure if that's possible.

Like you say, there are a couple different ways to do it, but whatever the right way to do it is based on your needs.

It might not even matter which way is the right way... I'd just pick a way and go that route. if you find there is a problem later on you can fix it, but without knowing the details of all the types of objects you are making and their lifetimes, it's really hard to guess what is best (:

Share this post


Link to post
Share on other sites
Thanks for the reply.
I've read a little about object pools in C++, but I thought I would take the easier route and allocate all needed instances of each class on init and use a state variable to decide if I can reuse them or not (and whether to draw them or not). Guess I'll go for the solution with the ObjectArray so that I can call update etc. on the whole array (enemyarray, itemarray..).

I really should stop thinking and start doing :)

Share this post


Link to post
Share on other sites
Maybe use a linked list instead of an array.

edit:
Actually, re-reading the question. I think this is just a matter of tunnel vision :)
It sounds like you are thinking to just preallocate an array of structs.
Instead you allocate an array of pointers to structs (or object instances).
It could be a void* array.

A linked list would work too and would probably be better in the long run anyway.
I just wanted to point out that it's not so different as it seems.

Share this post


Link to post
Share on other sites
I use the base class container method. For me, it's usually an Actor class, then I extend specific monsters, heroes, etc, add them to the Actor container and call an update,or whatever,from an abstract method in the base class.

Share this post


Link to post
Share on other sites

This topic is 2547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this