Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: mode7 on Sun 03/07/2011 12:28:47

Title: Properly import structs [Solved]
Post by: mode7 on Sun 03/07/2011 12:28:47
Ok guys, its one of these retarded questions again I just can't seem to figure out
I've got a script header with this struct:


struct Engine {
 bool ShowLoadingScreen;
 int LoadScreenGraphic;
 int LoadScreenColorBG;
 int LoadScreenColorHigh;
 int LoadScreenColorLow;
};
Engine UTE;
import Engine UTE;

When I run this, I get an error "Variable "UTE" is already defined"
if I leave out the import I can access the variable from local scripts also but value changes seem to stay local - so I can't pass a value on to a module script. So how can I make the struct to become global?

OFFTOPIC:

I dont want to open another topic on this as its probably rather simple.
Structs can contain functions right, so I can call them like this

MyStruct.Myfunction (parameters);

right?

So how do I declare them?
Probably I can just write
function xxx (parameter);
in the struct, but how do I declare the function in the main script??

Do I just write
Struct.function (parameter) {
 stuff
}

Or how would I pull it off?

Title: Re: Properly import structs
Post by: Wyz on Sun 03/07/2011 13:10:36
You need to have the variable declaration in the code file instead of the header file. The import should remain there. Also export the variable from the code file.

As for functions it's pretty simple:


// in the header
struct MyStruct
{
 int someValue;

 import void SomeFunction();
}

// in the code file
void MyStruct::SomeFunction()
{
 this.someValue = 495;
}


Good luck. :)
Title: Re: Properly import structs
Post by: monkey0506 on Sun 03/07/2011 16:33:22
When you're working with structs something to keep in mind is that you're defining a new type. So if you want an instance of that type to be global, then you also have to make the type global. To do that the struct definition has to go in the header, before the import of the instance. Otherwise you can put the struct in the script file.

So, what you need to do to make a global struct instance is this:

// Script.ash

struct Engine { // define the type in the header to make it global
 bool ShowLoadingScreen;
 int LoadScreenGraphic;
 int LoadScreenColorBG;
 int LoadScreenColorHigh;
 int LoadScreenColorLow;
};

import Engine UTE; // import the instance globally

// Script.asc

Engine UTE;
export UTE;


It's important to remember that any variables, pointers, or struct instances have to be exported before they can be imported, but not functions. Functions can just be directly imported.

Also, as Wyz showed, for struct member functions you can import them directly into the struct. You should also check out extender methods which work for custom structs, and can be quite useful for various situations (for example, a function can return a dynamic array, but a limitation in the compiler won't allow you to import that function as part of a struct definition; however, you can import an extender method returning a dynamic array into the header which works the same).

Edit: I was just rereading the first post when I realized you said you put that entire first code snippet into the script header. NEVER define variables, pointers, struct instances, or functions in the header. EVER! Don't do it!! That's what import is for.

The reason? Anything in a script header is copied into every other script. That means that if you use import, then your variables and functions and what have you are properly linked back to the original, single item that the import references. If you defined the item in the script header though, a new item with the exact same name is created for every script, with its own value, completely separate from the other items with the same name.

Taking it further, if you were to define a function in a script header, and then you changed that function (even just by adding a comment in the function), then the next time you compile your game every room is going to have to be rebuilt. Quite a toll for adding a comment if your game has a lot of rooms. If you'd just used an import then you wouldn't have that problem!
Title: Re: Properly import structs
Post by: hedgefield on Sun 03/07/2011 20:37:39
Thanks guys, I was wondering too if there was a way to call functions from a struct, and this was exactly what I needed. Got it working now :)
Title: Re: Properly import structs
Post by: mode7 on Mon 04/07/2011 07:43:30
Thanks Wyz and Monkey for making his clear. Also thank you monkey for explaining script headers . I already noticed when coding concurrence that adding stuff to the headers adds a lot of compile time.
I guess this is solved now