problem with variables and multiple functions

Started by RenatoDias, Mon 03/12/2012 19:13:43

Previous topic - Next topic

RenatoDias

I'm having some problems with variables. I've scripted the following:
Code: AGS

bool Light1;

function define_lights(){
    Light = true;
}

function oLight1_Look{
    if (Light1 == true){
        Display("The Light is on");
    }
}


But when I play the game and examine the light, it's doesn't show the message. If I remove the condition(if), it does show. What the heck is happening? What am I doing wrong?
Can I import the value from one function into another? They both are on the same script.

Slasher

#1
Light1 bool has to be set to 'true' by an action / event / condition before it will turn to true from false.

You have a 'Define lights' function, how have you set this to turn Light1 to 'true'?

Once you have set a bool you can use it's results in other functions (global variables can be used in any room. Room
variables just in that room.)

Looking at the light when false will show nothing as you have no condition for 'else' statement (else if Light1==false)

EXAMPLE
Code: AGS

function oLight1_Look
{
if (Light1 == true)
{ 
Display("The Light is on");
}
else if (Light1 == false)
{
  //DO THIS INSTEAD
}
}

RenatoDias

QuoteLight1 bool has to be set to 'true' by an action / event / condition before it will turn to true from false.
So, it must be set to true by an Object-Oriented(action/event) function?  I tried to do what I learned in C++, in which you create the variable before the main function(int main), set a value using the int main(or other functions), and use the variable on any function.

QuoteYou have a 'Define lights' function, how have you set this to turn Light1 to 'true'?
I applied the rule of first script(non-event/action) use(attach a value to a variable).

QuoteLooking at the light when false will show nothing as you have no condition for 'else' statement (else if Light1==false)
Do I really need an 'else if'? Normally, I don't use an 'else' unless I really need it.
Ok, I think I get it.

Crimson Wizard

To  elaborate more, the function you have:
Code: ags

function define_lights(){
    Light = true;
}

Has to be CALLED by something, it won't run on its own, therefore your Light variable won't be set.

If this is Global Script, I'd recommend to use predefined "game_start" function that is called automatically at game's start:
Code: ags

function game_start(){
    Light = true;
   < more initialization here, if needed >
}

or, if you insist using your function:
Code: ags

function game_start(){
   defined_lights();
}


If this is room script, as opposed to Global Script, you may use "First time enters room" event instead ("game_start" won't run there).

Quote from: RenatoDias
I applied the rule of first script(non-event/action) use(attach a value to a variable).
There's no such rule in AGS.

Quote from: RenatoDias on Mon 03/12/2012 19:13:43
Can I import the value from one function into another? They both are on the same script.
Only global variables, the ones declared outside function, are shared. Locally declared variables are not shared, and you can only pass them as parameters to functions or return values. In your case you should be alright with your variable, since it is global already.

Quote from: slasher on Mon 03/12/2012 19:37:54
Code: AGS

function oLight1_Look
{
if (Light1 == true)
{ 
Display("The Light is on");
}
else if (Light1 == false)
{
  //DO THIS INSTEAD
}
}


This could be written more simplier:
Code: ags

if (Light1)
{ 
   Display("The Light is on");
}
else
{
  //DO THIS INSTEAD
}

RenatoDias

room.asc There is this:
Code: AGS

bool Light1;
function room_FirstLoad(){
Light1 = true;
}


On GlobalScript.asc, this:
Code: AGS

oLight1_Look(){
    if (Light1 = true) {
        Display("Light is on");
    }
    else if (Light1 = false)
    {
        Display("Light is off")
    }
}

But only the 'else if' message is displayed, even though the variable was set to true in room.asc. Do I have to export this variable from room.asc to GlobalScript.asc, if yes, how? If no, what should I do then?

Crimson Wizard

Hmm, I do not even know how that compiled. You must have had another Light1 variable declared in GlobalScript too?

Room variables cannot be exported to GlobalScript. However, GlobalScript variables may be exported to all room scripts.
To do so, declare and export a variable in GlobalScript.asc:
Code: ags

bool Light1;
export Light1;

Then, declare same variable as import in GlobalScript.ash:
Code: ags

import bool Light1;

Now you can use that variable in any room. Note, you should NOT re-declare it in each room.

RenatoDias

I did, by accident. Tried the export-import thing and it worked. So, I export in GlobalScript.asc and import in GlobalScript.ash? Now I get it, thanks.

selmiak

#7
if you copypasted that out of your script and that gives weird results you should totally use
if (Light1 == true) {

and
    else if (Light1 == false)

with two equal sign in the conditions.

SMF spam blocked by CleanTalk