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'
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.
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, 300So, 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: