🔒 Locked
Retreiving data from a dynamic map array?
Advertisement
Original Post
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!
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?
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?
If you're willing to do some confusing construction and deconstruction you can ease up the acessing of the array.
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.
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;
Quote:There are some problems with this method, such as:
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;
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement