struct arrays

DrKane
Unregistered
 
Post: #1
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.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 335
Joined: 2002.04
Post: #3
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.
Quote this message in a reply
DrKane
Unregistered
 
Post: #4
So, is there any way to create an array of structs?
Quote this message in a reply
DrKane
Unregistered
 
Post: #5
So, is there any way to create an array of structs?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
I don't understand half of what's been said in this thread, so hopefully this is relevant:

Code:
typedef struct Foo
{
    int x;
    float y;
}
Foo;

Foo myArray[3] =
{
    { 3, 3.14f },
    { 2, 2.71f },
    { 9, 9.81f }
};
Quote this message in a reply
DrKane
Unregistered
 
Post: #7
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?
Quote this message in a reply
Mars_999
Unregistered
 
Post: #8
Quote:Originally posted by DrKane
So, is there any way to create an array of structs?

This should work for you?

Code:
#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!!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
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:

Code:
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 }
};
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #10
Quote:Originally posted by Mars_999

Code:
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.

Code:
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;
    }
Quote this message in a reply
Mars_999
Unregistered
 
Post: #11
Quote: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?
Quote this message in a reply
Jeff Binder
Unregistered
 
Post: #12
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
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.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #14
Oops my bad, yes its a linear equation 1/1!! Thinking of something else! =)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Malloc() Struct with NSString Inside? Graphic Ace 3 4,792 Jan 26, 2010 05:32 PM
Last Post: cmiller
  Struct based functions Jones 9 5,639 May 9, 2006 02:37 PM
Last Post: Zekaric