# Pointer to array.

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

## Recommended Posts

I'm implementing a map paging system like such as and:

...|...|...
...|...|...
--+--+--
...|...|...
...|...|...
--+--+--
...|...|...
...|...|...

there is a macro array (3 by 3) and 9 micro arrays.

each macro array cell contains a micro array. great.

when the character moves outside the central bounding rectangle, i want to shift the pointers in the macro array so 6 out of the 9 micro arrays will not have to be reloaded.

how do i store the micro arrays in the macro array to facilitate this?

##### Share on other sites
is that not a 3 dimensional array?

[3][6][3] for your example

##### Share on other sites
Would this be a workable solution?

- Make the 3x3 macro array an array of pointers to your 'micro' object, rather than an array of micro objects themselves
- Create howerver many micro objects you need in a micro[x][y] array
- Set the first 9 pointers to the first nine micro objects, so
[currentX][currentY] is the micro cell the top left pointer points to
- Then do something like

void moveLeft(int currentX, int currentY){   if( currentX != 0 )   //Not at the far left       for(int i = 0; i < 3; i++){   //Run through the macro array X co-ords           for(int j = 0; j < 3; j++){  //Run through the macro array Y co-ords                macro[j] = micro[currentX + i - 1][currentY];                 //Every cell points to the micro array cell left of where it was           }       }        }}

with similar functions for up, right, down.

##### Share on other sites
Quote:
 Original post by PrintFDebuggerWould this be a workable solution?- Make the 3x3 macro array an array of pointers to your 'micro' object, rather than an array of micro objects themselves- Create howerver many micro objects you need in a micro[x][y] array- Set the first 9 pointers to the first nine micro objects, so [currentX][currentY] is the micro cell the top left pointer points to - Then do something like*** Source Snippet Removed ***with similar functions for up, right, down.

similar to what i came up with. it looks like using objects is the best solution. i was hoping i could bypass them entirely as they contain no information except the array

##### Share on other sites
could i have a four dimensional array, and then just do the same operation as above?

##### Share on other sites
Quote:
 Original post by overeasycould i have a four dimensional array, and then just do the same operation as above?

my concern is that when i move a two dimensional sub-array in the macro array the entire array will be copied in memory. can someone confirm this?

##### Share on other sites
the whole array will be in memory already anyway...

if you copy elements from

big[4][4]

to

small[4]

only the values you copy from one array to another will be copied in memory.

Using c style arrays you will have to iterate through the elements of the array that you want to use to copy the data from one array to the other, you cant simply do small[4] = big[4][], and a pointer to big[4] will not achieve what you hope.

##### Share on other sites
Your best bet would be to use an appropriate indexing scheme into a one-dimensional map array. You'll have one coordinate for the room, and another coordinate for the individual cell. You don't have to copy anything around, and it can easily be factored into a single function that you just use everywhere:
const int ROOM_WIDTH = 3;const int ROOM_HEIGHT = 3;const int WORLD_WIDTH = 3;const int WORLD_HEIGHT = 3;const int MAP_WIDTH = ROOM_WIDTH * WORLD_WIDTH;const int MAP_HEIGHT = ROOM_HEIGHT * WORLD_HEIGHT;Foo mapArray[MAP_WIDTH * MAP_HEIGHT]; // or perhaps an std::vectorint getMapArrayIndex(Point room, Point cell) {   // todo: check coordinates are in range   return (room.Y * ROOM_HEIGHT * MAP_WIDTH) + (room.X * ROOM_WIDTH) +          (cell.Y * MAP_WIDTH) + cell.X;}

##### Share on other sites
Quote:
 Original post by ZipsterYour best bet would be to use an appropriate indexing scheme into a one-dimensional map array. You'll have one coordinate for the room, and another coordinate for the individual cell. You don't have to copy anything around, and it can easily be factored into a single function that you just use everywhere:const int ROOM_WIDTH = 3;const int ROOM_HEIGHT = 3;const int WORLD_WIDTH = 3;const int WORLD_HEIGHT = 3;const int MAP_WIDTH = ROOM_WIDTH * WORLD_WIDTH;const int MAP_HEIGHT = ROOM_HEIGHT * WORLD_HEIGHT;Foo mapArray[MAP_WIDTH * MAP_HEIGHT]; // or perhaps an std::vectorint getMapArrayIndex(Point room, Point cell) { // todo: check coordinates are in range return (room.Y * ROOM_HEIGHT * MAP_WIDTH) + (room.X * ROOM_WIDTH) + (cell.Y * MAP_WIDTH) + cell.X;}

it's a paging system. the contents of it (the portion of map pre-loaded) changes. i think i'm going to use objects to be safe. doesn't hurt.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013434
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!