struct question

Non-platform specific questions.

struct question

Postby fauldran » Wed Sep 06, 2006 10:47 pm

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?
Fauldran
Image
fauldran
 
Posts: 43
Joined: Tue Dec 20, 2005 10:52 am
Score: 0 Give a positive score

Postby makslane » Thu Sep 07, 2006 12:32 pm

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.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby fauldran » Thu Sep 07, 2006 12:44 pm

ahhh, I see. Thanks.
Fauldran
Image
fauldran
 
Posts: 43
Joined: Tue Dec 20, 2005 10:52 am
Score: 0 Give a positive score

Something very cool.

Postby Cleve_Blakemore » Fri Sep 08, 2006 12:07 am

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.
Cleve_Blakemore
 
Posts: 20
Joined: Sat Aug 05, 2006 7:26 am
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest