Skip to main content
gamedev.net
🔒 Locked

Retreiving data from a dynamic map array?

Started by _damN_ 9 replies 1k views
Original Post
_damN_
_damN_
Hi there!! Basically i have a map array called int g_pTileMap[m_iWidth * m_iHeight]. The map size is a 16x16 grid of tiles. The tiles are 64x64. The elements contain integers and each integer corresponds to a tile ID. So 1 would be grass and 2 would be dirt etc. My probem is this... Im struggling to retreive data from the array. I wanto be able to change the value in an element but i dont know how to access it. I have a function called InsertTile(int xTile, int yTile, int iTileID) that i wanto use to change a specific elements ID. Any help would be appreciated! Thanks!
SiCrane
SiCrane
If I understand what you're asking, you would use something like g_pTileNap[xTile * m_iHeight + yTile] = TileID.
Zahlman
Zahlman
You could just use boost::multi_array instead...
_damN_
_damN_
Hi SiCrane. That doesnt seem to work... :-//////
Zakwayda
Zakwayda
Quote:
That doesnt seem to work... :-//////
We'll need some more information in order to be able to help. Perhaps you could post the relevant code, along with a description of the problem (be sure to include any error messages that you're getting).
_damN_
_damN_
Hey there... apologies... This is what my function looks like. Basically xTile and yTile are the x and y positions in the array of the tile i wanto modify and then iTileID is the new tile i wanto change it to. So all it does basically is add a different tile ID to the array.

I dont get any error messages. All that happens is that the ID doesnt change and all the tiles in the map still look the same.

inline void TileMap::InsertTile(int xTile, int yTile,int iTileID)
{
// Assign the tiles ID to the member variable.
m_iTileID = iTileID;

m_pTileMap[xTile * m_iHeight + yTile] = iTileID;

}

if it was a 2D array i would have accessed the element like:

m_pTileMap[x][y] = iTileID;

So what could the equivilant be?
Any idea what im doing wrong?

Sharsnik
Sharsnik
If you're willing to do some confusing construction and deconstruction you can ease up the acessing of the array.

int** g_pTileMap;pTileMap = new int[m_iWidth];for (int i = 0; i < m_iWidth; i++){    pTileMap = new int[m_iHeight];    for (int j = 0; j < m_iHeight; j++)    {        pTileMap[j] = 0; //0 initilization (not needed)    }}


I personally like this method since you can easily access it with the classic 2D array notation.

Just be careful that you deconstruct it in reverse order in your deconstructor.

for (int i = 0; i < m_iWidth; i++){    delete pTileMap;}delete pTileMap;
Zakwayda
Zakwayda
Quote:
If you're willing to do some confusing construction and deconstruction you can ease up the acessing of the array.

int** g_pTileMap;pTileMap = new int[m_iWidth];for (int i = 0; i < m_iWidth; i++){    pTileMap = new int[m_iHeight];    for (int j = 0; j < m_iHeight; j++)    {        pTileMap[j] = 0; //0 initilization (not needed)    }}


I personally like this method since you can easily access it with the classic 2D array notation.

Just be careful that you deconstruct it in reverse order in your deconstructor.

for (int i = 0; i < m_iWidth; i++){    delete pTileMap;}delete pTileMap;
There are some problems with this method, such as:

1. You have to manage the memory manually, which is a bit cumbersome and error-prone. In modern C++ we generally try to let RAII containers handle this sort of work whenever possible, which generally leads to code that's more clean, concise, and robust (and that has a better chance of being exception-safe).

2. The array may end up being stored non-contiguously in memory. In this particular case it probably won't matter, but in general there can be advantages to using contiguous storage (such as improved cache coherency).

3. This work has already been done for you in the form of boost::multi_array. multi_array supports element access using 'bracket' notation (e.g. [j]), and, like other RAII containers, will handle the memory management details for you (multi_array will also ensure that the data is stored in a single contiguous block).

@The OP: I think you'll need to supply more information. Based on what you've posted though, I think a good first step would be to step through the code using the debugger and try to determine where and why the tile map values are deviating from the expected values.
_damN_
_damN_
Yeah. Ive decided than to change the arrays to 2D. I must say... Its alot slower though but it doesnt matter because its only for an editor.
Imgelling
Imgelling
m_pTileMap[xTile * m_iHeight + yTile] = iTileID;

Shouldn't that be

m_pTileMap[yTile * m_iWidth + xTile] = iTileID;

my blog contains ramblings and what I am up to programming wise.
SiCrane
SiCrane
Both ways should work. The difference is if you organize your array in terms of rows or columns.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.