SOLVED: Importing struct (again)

Started by Eastwing, Mon 23/06/2014 04:54:13

Previous topic - Next topic

Eastwing

I'm trying to import struct as shown in this thread.

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

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


then importing in any script or header:
Code: ags

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?

Eastwing

Sorry, folks, my bad

I just need to place struct in header.

Eastwing

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?

arj0n

#3
GlobalScript.ash:
Code: ags

// 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

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


Room function:
Code: ags

function room_FirstLoad()
{
if (ArthurHiding.bottom ==0){ 
//do something
}

Eastwing

Do I need import it in each Room? Can I just make import in global header?

Joe

Of course you can make just 1 import in global header.
Copinstar © Oficial Site

Eastwing

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')

arj0n

ajusted my last post, it should be working fine that way.

Joe

#8
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...
Copinstar © Oficial Site

Snarky

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.

Eastwing

#10
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

Khris

#11
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
// 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.

Eastwing

#12
Khris, I'm doing exactly like in your example.

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

import CharacterHiding ArthurHiding;


And GlobalScript.asc:
Code: ags
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!

Eastwing

#13
Well, may be I'm going crazy but It's starts again.

Snarky

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?

Eastwing

#15
I found that problem with unresloved import arises when I try to use instance.

There is function in script Test.asc:
Code: ags
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"

Joe

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.
Copinstar © Oficial Site

Eastwing

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!

SMF spam blocked by CleanTalk