stupid question about global variables :D

Started by Mel_O_Die, Fri 24/11/2006 20:04:47

Previous topic - Next topic

Mel_O_Die

so, all is in the title

i habitually declare variables in top of global script like:

int hour;
or
int hour=1;

but it's only works with code in the global script, i can't use them in rooms

so for each new variable i need in room (like, if the player have done a certain action, the "action" variable, will increase of one etc.)
i had to create it with the interaction editor/new action, select "variable is set to a certain value" and "edit variables" in the "change" button and finally create a new variable in that window
aw

so if you see what i mean, it's a very complicated method to do that, is there any easiest way in ags to do that? (i made some searches in the forum and in the notice but i found nothing interresting)
like create it in script with a simple line?

thanks!
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

SSH

Err, you can do that in a room script...
12

Khris

I think Mel_O_Die meant he can't use the variables, as opposed to declaring them.

Underneath "int hour;", add "export hour;"
In the script header, add "import int hour;" and voilà .

Mel_O_Die

thankx khrismuc

sorry for my english, sometimes it's very hard to me for explain my problems :d

so i had to put an "export hour;" in the header of my room script, and it can be used and modified by getgraphicalvariable or setgraphicalvariable?

i'm a little lost cause of my old method :d
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Ashen

No.
Get/SetGraphicalVariable refer to Interaction Editor Variables - the sort you create this way:
Quote
i had to create it with the interaction editor/new action, select "variable is set to a certain value" and "edit variables" in the "change" button and finally create a new variable in that window

import/export work on Script variables, the ones you create like:
Quote
int hour;
or
int hour=1;

As is explained in the BFAQ, and in a number of threads on the forum, you'd use them like this:
In the Global Script (Ctrl-G):
Code: ags

int Hour = 1;
export Hour;


In the Global Header (Ctrl-H):
Code: ags

import int Hour;


You can now use Hour in Room scripts, like you can in the Global Script:
Code: ags

if (Hour == 12) {
  Display ("It's Midday!");
  Hour ++;
}
I know what you're thinking ... Don't think that.

Mel_O_Die

okay thanks a lot!!

i never used the global header  ::) ::)
but it seems to be great for me :)

so i also get my answer for the variable setting through interaction editor is impossible do to that an other way

thanks

(it really was a stupid question huhu  ;D)
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

kantor_98

I have a couple of small problem using import and export commands, so I like to post here the code and some small proposals for help wording. What follows is concerning the creation and use of the global user defined variable "contor".

1.   Import â€" export commands for global user defined variables


// main global script file
int contor;
#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
  //gui[0].Visible=false;
  gui[4].Visible=false;
  gui[5].Visible=false;
  gui[6].Visible=false;
  gui[7].Visible=false;
  gui[8].Visible=false;
   
export contor;

#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE




a) In Scripting, Code & Interaction From AGS wiki, it is written:

Step 1. If the variable will have global scope (such as health, money, etc.), go to the global script.

It is not saying where to global script. After some unsuccessful placements, I find out it must be placed just before the #sectionstart game_start statement.

b) In HELP it is written:
export variable [, variable ... ] ;
Declares that variable can be exported and accessed by other scripts. You must place this at the end of your global script.

“The export command must be placed at the end of the game_start section â€" before the #sectionend game_start statement ” -  I think it is much more clear if you say it like this, and not like it is written in HELP.

c) Where to put the import command:

// room script file
import int contor;

in each room that you need it.

By the way: I try with “edit script header” for all the rooms, but it didn't work. How can be used a easier way to create a general user defined variable that will be used in ALL rooms ?



SSH

#7
Quote from: kantor_98 on Mon 27/11/2006 11:50:23

By the way: I try with “edit script header” for all the rooms, but it didn't work. How can be used a easier way to create a general user defined variable that will be used in ALL rooms ?




What do you mean "it didn't work"? That is what you are supposed to do...

Here's my instructions:

1. Variables must always be declared before they are used. They can be declared anywhere, but you can't use them until after they are declared. Therefore the easiest place to declare them is right at the top of your global script before any functions.

2. To use a global script variable in rooms, you must export it from the global script. This can be done anywhere AFTER the declaration of the variable, even on the next line if you like.

3. You must also import it to the rooms. The easiest way to do this is in the global header.

So put this at the top of your global script:
Code: ags

int contor;
export contor;


and put this anywhere in the global header:
Code: ags

import int contor;


It's that simple. #sectionstart/end does not affect it at all. You must put it before game_start if you use the variable in game_start. The export command must be outside any function.



Just to be clear, when khrisMUC says "you got several things wrong  there", below,  he's referring to kantor, not to me!
12

Khris

You got several things wrong there:

-You can declare variables everywhere you like, but you'll have to move them outside a function if more than one function is supposed to access it.
Globals don't need to go before game_start, but you'll have to declare them before you actually use them.
That's why the manual doesn't say where, it's up to you.

-The export bit goes right after the declaration.
Your global script should look like this:
Code: ags
// main global script file

int contor;
export contor;

#sectionstart game_start ...
...
...


-To import the variable to make it accessible by all rooms, open the script header and add this line:
Code: ags
import int contor;


The script header it put above the global and every room script at compile time.

Edit: SSH beat me, posting anyways

Ashen

#9
Beaten by both of them, have an edited version anyway:

Can you just check the first bit of code you posted? It looks like you've missed the closing } from game_start - if that's just a typo here then OK (still correct it to save any confusion), but in the actual game script that would cause errors.

Quote
“The export command must be placed at the end of the game_start section â€" before the #sectionend game_start statement ” -  I think it is much more clear if you say it like this, and not like it is written in HELP.

I don't think it is that much clearer, it's also kind of wrong. In practice, it shouldn't matter WHERE you put the export statement provided it's after the declaration, and outside of any functions. Chris has said (somewhere, can find the actual quote EDIT:found it) that the manual only says the end of the Global Script to make sure this is done. Bringing the #sectionend statement into the description - IMO - makes it more likely someone will get confused and place it INSIDE the game_start function.

I usually place them like KhrisMUC said, and it works fine.

Quote
(import int) in each room that you need it.

By the way: I try with “edit script header” for all the rooms, but it didn't work. How can be used a easier way to create a general user defined variable that will be used in ALL rooms ?

That would create a seperate instance of the int in each room - so setting it to 5 in room 3 would have no effect when you check it in room 7. The Global Header is the right place to make it accessable to all rooms - as SSH asked, how did it not work for you? If you also had imports in seperate room headers, it's possible the 'Room version' was being used and not the 'Global version' - so again room 3's contor would have no effect on room 7's.
I know what you're thinking ... Don't think that.

SSH

I was wondering if people would find it useful to have a module that did something like:

Global.Set("contor", 0);
Global.Increment("contor");
if (Global.Get("contor")==1) {
  // do something
}

I kind of already have the infrastructure for this in my MultiResponse module, so I could probably knock this up in 2 shakes of a donkey's tail.
12

Ashen

That seems like it's mostly done by the GlobalInt commands anyway. Why not just set up an enum or something to 'name' them?

What would Global.Increment do - I'd guess the same as '+= 1'? That's a bit more convenient that the GlobalInt method, but other than that I'm not sure I'd see the point.
I know what you're thinking ... Don't think that.

kantor_98

I should said "I HAVED" some problems instead of HAVE. Sorry for my typing. I use the code as I written and it's work !
1. What do you mean by "GLOBAL HEADER" ? I assume that it is the space between "main global script file" and "sectionstart game_start "
2. Yes, I missed a bracket } when I copied the text in the forum
3. I try to put the import command in the "script header" (CTRL+H), to not write it for each room, but I receive an error message, as the variable "contor" was not defined. Unfortunately I was not writing the error message, I promise it would not happen again.

So the script that I post it is working, folks !

Ashen

The 'Global Header' is the Script Header (Ctrl-H), as opposed to Room Scripts or Module Headers (that's how it's refered to in the BFAQ, at least, and how I usually think of it).

Quote
I receive an error message, as the variable "contor" was not defined. Unfortunately I was not writing the error message, I promise it would not happen again.

That could mean anything, not that it really matters if you've got it working for yourself. It's possible you mispelt contor in the Header or the Global Script, or that you'd declared or exported it inside a function. Like I said - doesn't matter if it works now, but it'd be nice to know what was wrong, so it doesn't happen again.
I know what you're thinking ... Don't think that.

SSH

Quote from: Ashen on Mon 27/11/2006 12:43:51
That seems like it's mostly done by the GlobalInt commands anyway. Why not just set up an enum or something to 'name' them?

Well, the idea was to make using named global ints easier for n00bs, and enums would probably make n00bs go all weak at the knees... ;)
12

Da_Elf

i find i tend to use graphical variables alot

SSH

Can I ask why you use Graphical Variables, Da_Elf? Do you tend to do most of your stuff in the interaction editor?
12

Da_Elf

i dont use the pre-done things like choose from the list to remove an object etc etc. i use the script window to enter in my script. normally i will do something like add an event which uses variables then create my graphical variable then ill delete that event. the variable is still there and can be used but i access it from scripting. i am using 1 global variable though and thats the script someone showed me to get time to advance every time i changed a room.

SSH

Quote from: Ashen on Mon 27/11/2006 12:43:51
That seems like it's mostly done by the GlobalInt commands anyway. Why not just set up an enum or something to 'name' them?

What would Global.Increment do - I'd guess the same as '+= 1'? That's a bit more convenient that the GlobalInt method, but other than that I'm not sure I'd see the point.

It came to me the other day what the point woule be: being able to set up some kind of debug mode in the module that let you browse the values of all current globals... veeeery handy!
12

SMF spam blocked by CleanTalk