Page 1 of 1

Arrays of structs

PostPosted: Wed Mar 05, 2014 11:47 pm
by CrackedP0t
For a game I'm working on, I'd like to have a 2d array of structs.
It would look something like this:
(In the global code)
Code: Select all
struct attack {
  int attackx;
  int attacky;
  int wait;
  int end;
};
struct attack attacks[5][5];
attacks[0][0].attackx = 200;
attacks[0][1].attackx = 300;

And then I'd need to access it in the actor code, something like this:
Code: Select all
textNumber = attacks[0][0]->attackx;

But I can't figure it out!
:x

Re: Arrays of structs

PostPosted: Wed Mar 05, 2014 11:57 pm
by lcl
You'd want to use typedef struct here.

Code: Select all
typedef struct attackStructAttributes {
  int attackx;
  int attacky;
  int wait;
  int end;
}attack;

attack attacks[5][5];
attacks[0][0].attackx = 200; //setting values like this doesn't work in Global Code
attacks[0][1].attackx = 300;

And for accessing the variables you don't need -> because these are not pointers. So:
Code: Select all
textNumber = attacks[0][0].attackx;

But you should note that you can't set values to variables in Global Code like that. You can only give start values to variables when defining them, but for setting the values later you need to use a function or to manually set them in some script editor event.

Re: Arrays of structs

PostPosted: Thu Mar 06, 2014 12:06 am
by CrackedP0t
Thanks a lot!
What's the difference between typedef and normal struct?

Re: Arrays of structs

PostPosted: Thu Mar 06, 2014 8:46 am
by lcl
CrackedP0t wrote:What's the difference between typedef and normal struct?

That's a very good question, and I didn't know the answer before doing some research.
But here's what I found out.

Using typedef struct allows you to define new structs without having to write the keyword struct.
I'll use my code as an example:
Code: Select all
typedef struct attackStructAttributes {
  int attackx;
  int attacky;
  int wait;
  int end;
}attack;

struct attackStructAttributes attacks2[5][5]; //this would work just as well
attack attacks[5][5]; //but with the use of typedef struct, we have now defined 'attack' as a keyword for an attackStructAttributes type of struct

So, what typedef does is to allow you to write less by defining a new keyword for the certain kind of struct type.
You can think it like this:

This is what you write:
Code: Select all
attack attacks[5][5];

This is what computer 'sees':
Code: Select all
struct attackStructAttirubtes attacks[5][5];


So, basically after using typedef writing 'attack' = writing 'struct attackStructAttributes' :D

It's a very good thing you asked that question, because I wasn't aware of the actual differences of these two ways of defining structs before and you kind of forced me to learn. :mrgreen:

But now that I know more about the normal struct definitions, here's how your original code would also work without the use of typedef..

Code: Select all
struct attack {
  int attackx;
  int attacky;
  int wait;
  int end;
};
struct attack attacks[5][5]; //this is correct

//these two lines aren't correct in Global Code if you're not writing inside a function
attacks[0][0].attackx = 200;
attacks[0][1].attackx = 300;

//so, if you did this, it would be correct:
void setValues()
{
    attacks[0][0].attackx = 200;
    attacks[0][1].attackx = 300;
}

The function would then be usable in any script editor event:
Code: Select all
setValues(); //and this would set the attackx's to 200 and 300


Or you can move the setting of the values to some script editor event:
Code: Select all
attacks[0][0].attackx = 200; //notice the use of '.' instead of '->'
attacks[0][1].attackx = 300;

sprintf(text, "%i, %i", attacks[0][0].attackx, attacks[0][1].attackx);

And now the text actor would print this:
200, 300

So, your mistakes in the original case were just trying to set values to those structs in Global Code and using the pointer operator '->' when you weren't dealing with pointers to structs but with the actual structs themselves.

I hope this helps you as much as it helped me: :lol: :D