Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Eastwing on Mon 23/06/2014 04:54:13

Title: SOLVED: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 04:54:13
I'm trying to import struct as shown in this thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=49706.msg636476245#msg636476245).

First my structure, variable and it' exporting in Global script:
Code (ags) Select

struct CharacterHiding
{
    bool top;
    bool left;
    bool botoom;
    bool right;
};
CharacterHiding ArthurHiding;
export ArthurHiding;


then importing in any script or header:
Code (ags) Select

import CharacterHiding ArthurHiding;


And when trying to run or compiling game I've got error:
QuoteError (line 2): expected variable or function after import, not 'CharacterHiding'

The only difference I see is I'm using a variable instead of an array. Is using arryays required?
Title: Re: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 07:08:01
Sorry, folks, my bad

I just need to place struct in header.
Title: Re: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 11:28:26
Well, now i get this:
QuoteError (line 8): Undefined token 'ArthurHiding'

But the editor recognize variable and show list of structure parts. What I'm doing wrong?
Title: Re: Importing struct (again)
Post by: arj0n on Mon 23/06/2014 12:16:29
GlobalScript.ash:
Code (ags) Select

// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.
struct CharacterHiding
{
    bool top;
    bool left;
    bool botoom;
    bool right;
};
import CharacterHiding ArthurHiding;


GlobalScript.asc:
Code (ags) Select

// main global script file
CharacterHiding ArthurHiding;
export ArthurHiding;


Room function:
Code (ags) Select

function room_FirstLoad()
{
if (ArthurHiding.bottom ==0){
//do something
}
Title: Re: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 13:24:56
Do I need import it in each Room? Can I just make import in global header?
Title: Re: Importing struct (again)
Post by: Joe on Mon 23/06/2014 13:52:38
Of course you can make just 1 import in global header.
Title: Re: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 14:14:55
Quote from: Joe on Mon 23/06/2014 13:52:38
Of course you can make just 1 import in global header.
I do that already actually. And, as I sayed, there is an error.

By now:
- Structure called CharacterHided is in global header. Imort of variable called ArthurHided is in global header too.
- Declaration of 'ArthurHided' variable is in global script. Export of it too.

And I stiil got that message (Error (line 8): Undefined token 'ArthurHiding')
Title: Re: Importing struct (again)
Post by: arj0n on Mon 23/06/2014 14:29:47
ajusted my last post, it should be working fine that way.
Title: Re: Importing struct (again)
Post by: Joe on Mon 23/06/2014 15:19:28
It should work the way Arj0n says. But maybe your Global script is not the top level one. You can reorder them in the tree pane. Make sure the GlobalScript is the 1st one. If it still doesn't work I don't know what else we can do.

EDIT: well actually what I said doesn't make much sense since global script will always be in higher position than room script...
Title: Re: Importing struct (again)
Post by: Snarky on Mon 23/06/2014 15:36:04
Quote from: Eastwing on Mon 23/06/2014 14:14:55
I do that already actually. And, as I sayed, there is an error.

By now:
- Structure called CharacterHided is in global header. Imort of variable called ArthurHided is in global header too.
- Declaration of 'ArthurHided' variable is in global script. Export of it too.

And I stiil got that message (Error (line 8): Undefined token 'ArthurHiding')

Looks like a proofreading problem to me. If you're declaring 'ArthurHided' and trying to use 'ArthurHiding' it's obviously not going to work.

I disagree with Joe's advice, BTW. It only applies if you're trying to use the struct in another global script (not a room script), and as a general principle, "the" GlobalScript (with all the AGS event handlers) should NOT be the top-level global script. In fact, in most cases it should be the lowest global script, because that allows it to use any of the functions exported by other script modules. If you do need to access a struct in other script modules, you should separate it into its own script file.
Title: Re: Importing struct (again)
Post by: Eastwing on Mon 23/06/2014 23:38:34
Quote from: Snarky on Mon 23/06/2014 15:36:04Looks like a proofreading problem to me. If you're declaring 'ArthurHided' and trying to use 'ArthurHiding' it's obviously not going to work.

No, it is not :) I wrote "ArthurHided" here by mistake, but in scripts it's all right.

Quote from: Joe on Mon 23/06/2014 15:19:28
It should work the way Arj0n says. But maybe your Global script is not the top level one. You can reorder them in the tree pane. Make sure the GlobalScript is the 1st one. If it still doesn't work I don't know what else we can do.

EDIT: well actually what I said doesn't make much sense since global script will always be in higher position than room script...

QuoteI disagree with Joe's advice, BTW. It only applies if you're trying to use the struct in another global script (not a room script), and as a general principle, "the" GlobalScript (with all the AGS event handlers) should NOT be the top-level global script. In fact, in most cases it should be the lowest global script, because that allows it to use any of the functions exported by other script modules. If you do need to access a struct in other script modules, you should separate it into its own script file.

Yes, thanks, it's looks like there is solution. Honestly, I thought my own scripts are same level that room scripts and "GlobalScript"  highest periority.

But there's next problem
Game compiled, but when I'm trying to run it there is error message: Script link failed: Runtime error: unresolved import 'ArthurHiding'

I tried to rebuild all files but nothing changes
Title: Re: Importing struct (again)
Post by: Khris on Tue 24/06/2014 00:01:09
Please show us the relevant parts of your scripts and headers.
Use copy-paste, so they appear exactly as you have them in your game, and tell us which code snippet is what.

To reiterate: struct definitions and import lines go into the header, declarations and export lines go into the script:
Code (ags) Select
// header
struct MyStruct {
  int a;
};
import MyStruct test;

// script
MyStruct test;
export test;


The variable test can now be used inside the script, in all scripts below and in all room scripts. The import line has to be in the header of the script that's declaring the variable or below.
Title: Re: Importing struct (again)
Post by: Eastwing on Tue 24/06/2014 00:12:06
Khris, I'm doing exactly like in your example.

GlobalScript.ash header:
Code (ags) Select
struct CharacterHiding
{
    bool top;
    bool left;
    bool bottom;
    bool right;
};

import CharacterHiding ArthurHiding;


And GlobalScript.asc:
Code (ags) Select
CharacterHiding ArthurHiding;
export ArthurHiding;


Do I need to show something else?

UPDATE
I'm just remove all mentions of structure and it's instance, rebuild all files, write code again and then rebuild all files again.
I have no idea what it was, but at lest now game starts and works fine

Thanks all for your help!
Title: Re: SOLVED: Importing struct (again)
Post by: Eastwing on Tue 24/06/2014 00:42:49
Well, may be I'm going crazy but It's starts again.
Title: Re: Importing struct (again)
Post by: Snarky on Tue 24/06/2014 00:46:40
Quote from: Eastwing on Tue 24/06/2014 00:12:06
Do I need to show something else?

It would be useful to see the code that failed. I mean the scripts where you're trying to use ArthurHiding.

Quote from: Eastwing on Tue 24/06/2014 00:42:49
Well, may be I'm going crazy but It's starts again.

If you'll be more specific we'll be able to help more easily. What, exactly, is going wrong?
Title: Re: Importing struct (again)
Post by: Eastwing on Tue 24/06/2014 00:55:17
I found that problem with unresloved import arises when I try to use instance.

There is function in script Test.asc:
Code (ags) Select
function Seeking(Character *badguy)
{
if (ArthurHiding.top)
badguy.SayBackground("Where are you?");
else
badguy.SayBackground("I can see you!");
}


This function is imported to GlobalScript header and called in RepExec of Room1.

UPDATE
Problem don't arises in room scripts, only in "custom global"
Title: Re: Importing struct (again)
Post by: Joe on Tue 24/06/2014 01:46:37
THAT is the problem. You have another global script (test.asc) which is in higher level than GlobalScript and it's trying to acces a variable declared in GlobalScript. I suggest you to define the struct and import the variable in the Test.ash and declare the variable in Test.asc.
Title: Re: Importing struct (again)
Post by: Eastwing on Tue 24/06/2014 02:29:58
Okay, I finally got it.

I move declaration of structure and import of instance to special header, declaration of instance and it's export moved to special script. This script placed to higher level. Now it's works perfect

Thank you very much, guys!