Advertisement

How should game objects be stored?

Started by October 26, 2004 05:48 PM
4 comments, last by slyterence 20 years, 3 months ago
I'm currenlty trying to design a 2d rpg/adventure game (a very zelda on the nes style). I'm trying to get the ideas straight in my head/on paper to begin with. What I think I will do is load the level information from a file into a data structure. The data structure will be an array of objects. Each object will represent a square/tile on the map. These squares will be objects which contain information about what tile graphic to display, can the player walk on the square, and a list of "game objects" on the tile. by "game objects" I mean items, characters etc Now I was wondering is this the right way to hold the data about the game, should the squares hold a list of all items on the square? If so how can I create a list that contains differnt types of object? Although they are all game objects they will be differnt types. If thats not the correct way to store the game data any suggestions on how to do it? What I want to be able to do is create an engine, so that I can come along and add specific game objects which exhibt certian behaviour. But how can game engines know how to store the game objects, if the game objects themselves have not been defined? OR is it just a fact that the engine always has to be modified to suit these objects in each differnt game
Quote:
Original post by siliconsurfer
Now I was wondering is this the right way to hold the data about the game, should the squares hold a list of all items on the square? If so how can I create a list that contains differnt types of object? Although they are all game objects they will be differnt types.


it sounds like a good start. are you using C or C++ or something else? If it's an Object Oriented language like C++ or Java you can make the array be an array of base class objects and just store the objects that way:

class BaseObject{};class OneObject: public BaseObject{};class AnotherObject: public BaseObject{};int main(){    BaseObject **myObjects = new BaseObject*[ numObjects ];    memset( myObjects, 0, sizeof( BaseObject* ) * numObjects );  //initialize the list to NULL    myObjects[0] = new OneObject();  //put in a OneObject    myObjects[1] = new AnotherObject();  //put in a AnotherObject    //run your game    //then when you're done, release all the memory    for ( int i = 0; i < numOjbects; ++i )    {        if (myObjects)            delete myObjects;    }    delete[] myObjects;}


i'd suggest using a stl vector or list instead of an array just for convenience. you'll also probably need to research and learn Object Oriented design concepts if you want to go this way.

-me
Advertisement
make a base class for your objects, and have each real object in the game be derived from that. Then, for each tile or region or whatever in your game, store a linked list of these base classes.
I'm not sure how familiar you are with object-oriented programming, but if you want to store different types of objects in an array, list, or map, you could create a base class called GameObject.
Then derive all your game object classes, such as Tile, Character, Item, etc. from GameObject. You could then insert any of those classes into a vector or list of GameObjects.

It might be tricky figuring out what all those GameObjects are when it's time to remove them. Some kind of type member variable of an enumeration type would work for that.

enum GameObjectType{  Character,  Item,  Tile}


It would be better though, to separate the different types of game object into different data structures. You could have an array of tiles, a list containing items, and a map containing Characters. That's probably the way I would do it.
Cheers guys, that clears things up for me a bit.

When we are talking about creating a base class and then derving other classes from that, I take it we are talkinng about inhertence.
So I guess a list can store a list of inherted objects?

If not I guess code monkey's option would be the simplist. And use a differnt list of each type of object in the game, my only problem is deciding in all the differnt types now!

Eh one other question. Does anyone know any good articles for linked lists, specifically a generic one for storing these sort of objects? Or better yet anyone written a linked list and want to share there code?
If not I guess I better hit the books!
Yeah. I'm going to assume you're using C++ :). std::list<> is the answer to all your prayers.
We scratch our eternal itchA twentieth century bitchWe are grateful forOur Iron Lung

This topic is closed to new replies.

Advertisement