#### Archived

This topic is now archived and is closed to further replies.

# RPG game question

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

## Recommended Posts

I am wondering how to handle the map/objects in a RPG? I have a 2d array with all the tiles for drawing the map, thats good, but how do i handle objects on tiles? Would I have to create another array that says what objects are on what tiles? And what if i want more than one object per tile? Then would I have to create a third array for path finding? It seems wasteful to have so many arrays. Is there a more efficient way of handling this?

##### Share on other sites
I would have one array of a tile class. The tile class would conatin a pointer to the tile image and details, as well as a linked list of objects.

##### Share on other sites
You could also run a check through the objects, and mathematically calcuate which tile they are on. Quite easy maths wise, you could simply divide the amount of tiles, by the size of the land and you can find the cordinates of each tile. Then check to see which tile the object is in.

RPGamer

##### Share on other sites
So would this mean one array that would hold the information for every object as well as the graphics for every object in the world?

##### Share on other sites
This is how I would do it, but it might not be the easiest way if you dont already know what your doing.

have an array of objects that represent items in the game

struct sObject
{
char name[30];
int type;
etc...
};

sObject ObjectArray[numobjects];

then have an a simple liked list class
class cObjectNode
{
cObjectNode m_pNextNode; //next node in liked list
int ObjectIndex; //index number in object list this node points to
int GetObjectNumber();
}

Now you have an array of tiles for your map spaces
struct sTile
{
SpriteClass sprite; //sprite for drawing ground at this location
cObjectNode *ObjectNode; //pointer to a liked list of nodes that point to the objects index number in the objec table, or null of no objects are present
}

sTile Map[NumTilesOnMap];

Now what makes this easy, is...
in your loop, when your going through your map array drawing the ground tiles, you check to see if Map[n].ObjectNode if not NULL. If its not, you travel through the liked list that Map[n].ObjectNode points to. When you read each node, you draw the object that Map[n].ObjectNode->ObjectIndex points to.
If you object array has a function for drawint the object, say its called Draw, then you would do this
ObjectArray[Map[n].ObjectNode->GetObjectNumber()].Draw();

I wish I was more articulate at explaining what I am trying to explain, but oh well. Perhaps this helps. Read it after a few nights without sleep, and it just might make perfect sense

class cMapTile
{

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002142
×