Page 1 of 1

struct question

PostPosted: Wed Sep 06, 2006 10:47 pm
by fauldran
using typedef and struct is all new to me at this point. Here is what I have done.

In Global Code
Code: Select all
typedef struct stElement
{
   int var1;
   int var2;
} Element;

Element e1;
e1.var1 = 5;
e1.var2 = 10;


For some reason the e1 variable is not global. When I try to access the "fields" on it from an actors script it has a value of 0. If I set it in the actors script (instead of in global code) then it works.

Am I doing something wrong or not doing something?

PostPosted: Thu Sep 07, 2006 12:32 pm
by makslane
The Global Code editor don't will assign the values or execute functions by itself. You need set the variable values in some Script Editor action.
So, the code 'e1.var1 = 5; ' in the Global Code has no effect. A good place to initialize the game variables is in the 'Create Actor' event of the view actor.

PostPosted: Thu Sep 07, 2006 12:44 pm
by fauldran
ahhh, I see. Thanks.

Something very cool.

PostPosted: Fri Sep 08, 2006 12:07 am
by Cleve_Blakemore
This has been tested and works for me (inside GE IDE and PC export) and it's a pretty cool thing to know for C programmers who are at entry or beginner level.

You can initialize an array of structs through a static declaration of data in one go in the globals. This is just a quick example of what I mean:

Code: Select all
struct POINT
{
 int x;
 int y;
}

struct POINT points[] =
{
  {0, 21},
  {35, 78},
  {8, 219}
}


... this is a very simple example, but I have done it with much larger and more complex structs with no problems. Great for specifying big ranges of data in-code rather than with an external file. I have also done this with single char * string arrays, but have not tried using the "string" variable type inside a struct yet.

Almost anything that would work in standard C code, will also work in C-script, with only a few limitations here and there. This opens up a pretty big world of code and you could write some extremely complex programs with this kind of infrastructure. I'm working on an iso game using a lot of data and object oriented pointers to functions right now in GE.