View Full Version : struct arrays
DrKane
2002.07.13, 10:04 PM
I've been working on my rpg engine for some time now and until now I have been using a 2 dimensional array to represent my world. It's an array a typedef enum called TileState, which has verious names such as grass, wall, etc... I just created a tile struct and I now want to make my 2d array into an array of tileStructs. I have an InitMap() function which declares every item in my 2d array;
tileArray[kMaxH][kMaxV] = {empty, empty, monster} etc...
Even after I changed my array declaration from: TileState tileArray[kMaxH][kMaxV] to tileStruct tileArray[kMaxH][kMaxV], no errors at all show up when I debug. As expected it doesn't work correctly, but that's not even the problem. If I put tileStructs that I've created rather than the typedef enum values, into the array decleration, it doesn't work.
Mars_999
2002.07.13, 11:04 PM
Are you saying your Array has enum types in it? If so an enum can't be changed its like a const variable. I guess I am somewhat confused on what your asking? Why are you using enum anyway if you don't mind me asking? =) Only thing I use enum for is in classes for const in the declaration part of the class.
Zwilnik
2002.07.14, 11:26 AM
by the sounds of it, you want to put Structs (different ones) into an array to show what is at a particular co-ordinate of the map ?
The only problem is, if the compiler doesn't give you an error message, it's probably only putting the address of the tile struct in, not the data from it.
If you've got one TileStruct defined (it looks like you do) an array of which contains all the info on your tile types, then putting enums that relate to the TileStruct array would still be the way to go. eg.
typedef struct
{
short tile_type;
short is_a_monster;
etc...
} TileStruct
TileStruct TileStructArray[MaxTileTypes]; // as it's a struct you'll have to fill it manually
TileStructArray[Plain_Tile].tile_type = 0;
etc.
then
short MapArray[Map_Width][Map_Height] = {Plain_Tile, Plain_Tile, Monster_Tile .... }
and use the value from MapArray to get the data you want out of TileStructArray...
if you were working on the GameBoy and absolutely needed the speed and wanted to save a tiny amount of memory (not worth it on the Mac), you could put a pointer to each TileStructArray element into MapArray instead and save a little, but it's not really worth it for a Mac game.
DrKane
2002.07.14, 10:10 PM
So, is there any way to create an array of structs?
DrKane
2002.07.14, 10:22 PM
So, is there any way to create an array of structs?
OneSadCookie
2002.07.14, 10:26 PM
I don't understand half of what's been said in this thread, so hopefully this is relevant:
typedef struct Foo
{
int x;
float y;
}
Foo;
Foo myArray[3] =
{
{ 3, 3.14f },
{ 2, 2.71f },
{ 9, 9.81f }
};
DrKane
2002.07.14, 10:36 PM
Ohhh... this is what I've been trying to do:
typedef struct
{
short h,v;
float pos;
}tileStruct;
// different terrain tiles
tileStruct grassTile;
tileStruct wallTile;
//the world array
tileStruct tileArray[kMaxArrayH][kMaxArrayV] = {grassTile, grassTile, grassTile},
{wallTile, grassTile, grassTile },
{wallTile, grassTile, grassTile }};
Is there any way I could set this up to make it work, or is there anything I could do to bassically get the same results?
Mars_999
2002.07.14, 10:36 PM
Originally posted by DrKane
So, is there any way to create an array of structs?
This should work for you?
#include <iostream>
struct FOO
{
int a;
};
const int arraysize = 10;
int main(int argc, char *argv[])
{
FOO array[arraysize][arraysize];
for(int a = 0; a < arraysize; a++)
{
array[a][a].a = 0;
}
for(int a = 0; a < arraysize; a++)
{
cout << array[a][a].a << endl;
}
return 0;
}
HTH!!
OneSadCookie
2002.07.14, 11:50 PM
Although Mars999's code is only going to initialize the elements of the 2D array that are on the diagonal, his idea is right -- best to declare the 2D array empty in one place, and write a function to initialize it field-by-field which you call at the beginning of main.
If you want a hacky solution, though, I can't resist posting this:
typedef struct Foo
{
int x;
float y;
}
Foo;
#define GRASS { 3, 3.14 }
#define CLAY { 2, 2.71 }
#define STONE { 9, 9.81 }
Foo myArray[2][2] =
{
{ GRASS, CLAY },
{ STONE, GRASS }
};
codemattic
2002.07.15, 12:08 AM
Originally posted by Mars_999
FOO array[arraysize][arraysize];
for(int a = 0; a < arraysize; a++)
{
array[a][a].a = 0;
}
for(int a = 0; a < arraysize; a++)
{
cout << array[a][a].a << endl;
}
In the code you posted you need to nest another 'for' loop for it to do anything meaningful. And you dont really have to zero it out since the compiler will do that for you.
for(int a = 0; a < arraysize; a++)
for(int b = 0; b < arraysize; b++)
{
array[b][a].a = 0;
}
for(int a = 0; a < arraysize; a++)
{
for(int b = 0; b < arraysize; b++)
{
cout << array[b][a].a << " ";
}
cout << endl;
}
Mars_999
2002.07.15, 06:04 PM
Originally posted by codemattic
In the code you posted you need to nest another 'for' loop for it to do anything meaningful. And you dont really have to zero it out since the compiler will do that for you.
No I dont' think you should have to do to my array size is 10 on both x and y. So you can use a to zero out everything in all elements. You would need b if you had lets say array[10][2]; Now a isn't going to work. =) You will get a memory error somewhere?
Jeff Binder
2002.07.15, 07:03 PM
No, the version with array[a][a] will only zero elements where both indices are the same. Thus it will only hit the diagonal of the array.
OneSadCookie
2002.07.15, 07:03 PM
You're only initializing the elements where the i & j indices match -- (0, 0), (1, 1), (2, 2), &c. You'll never initialize (0, 1), (1, 0), (0, 2), (1, 2), &c, so you do need the second, nested, for loop.
Mars_999
2002.07.15, 07:47 PM
Oops my bad, yes its a linear equation 1/1!! Thinking of something else! =)
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.