Sign in to follow this  

dynamic variables

This topic is 3860 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, I'm not really sure what to call what I'm trying to achieve, so I'm just using dynamic variables. Let’s say I have 3 arrays: map1[100][10] = {{0, 0, 1, 6, 18, 2... map2[130][10] = {{1, 5, 15, 15, 2, 4... map3[180][10] = {{6, 8, 2, 0, 0, 0, 1... Each array stores the data for the tile based map for a level. Now in my code, I often need to take data from the current level map, however as the map changes with each level, doing this is proving difficult for me. In action script (my main language), I would do this.. level = 1; map = ["map"+level]; That would create a new variable called map, and make it equal to map 1 because level equals 1. When the level changed, I could just increase level, and change what array map is equal to. I'm looking for a way to achieve this in C++ but have reached a dead end. I can't really progress with the development of my game without being able to do this somehow, so I would really appreciate it of someone could let me know how this is done in C++. Thanks Ali

Share this post


Link to post
Share on other sites
You could each map (or the pointer to each map) in an array (or vector), and have the index number of the map in the array correspond to the level. That way, you have a variable (int i), and each time you increase a level, just increase i by 1.

Share this post


Link to post
Share on other sites
1.
If your level data wasnt created with constants you could do

Map[3][180][10];

Of course you have to have 180 for each level (whatever that is X or Y etc)

2.
If your map is declared globally you can do this, but the last dim must remain 10:

int map1[100][10] = {0, 0, 1, 6, 18, 2...
int map2[130][10] = {1, 5, 15, 15, 2, 4...
int map3[180][10] = {6, 8, 2, 0, 0, 0, 1...

int * map[3]={map1,map2,map3};

(Might need to be this, I never remember till it fails to compile:
int * map[3]={map1[0],map2[0],map3[0]};
}

Here is how to access your map (10 must be static)

int GetMapCell(int Level,int Col,int Row)
{
return map[Level]+(Col*10)+Row;
}

None of these are safe however, how is your program going to know how many cols per level. If you are on level 1 and you try to read 178,6 its gonna go wrong!

Share this post


Link to post
Share on other sites
Thanks for the replies.

I didn’t think of using a 3D array, that’s quite a simple solution. I suppose my levels don't have to be different sizes, I'll just make them all the same size to save myself some hassle.

Thanks again guys, I can stop worrying about this now :)

Ali

Share this post


Link to post
Share on other sites
Sign in to follow this