Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: CaptainD on Mon 02/11/2015 21:38:57

Title: Setting a bool with a GUI
Post by: CaptainD on Mon 02/11/2015 21:38:57
Hmm.. okay, I've been trying various things with this, checking and rechecking the manual, and I guess I've missed something obvious but I just can't get this to work.

Basically, I've got a GUI with the four basic mathematical functions.  When you click a button, a bool is set.  The GUI as far as I know works okay.

When it goes back to the room script and I have a set of IF conditions checking the bools, it just isn't doing anything.  I've only got "Display" in there to check the code is working correctly before I put in what I really want it to do.  The GUI is called when am object is interacted with, it's in "normal" mode so as far as I understand it, setting it to visible should turn it on.  The bools are reset to false after the script is run.

Fairly sure there's something really obvious that I'm not seeing... :sad:

Code (ags) Select

function oC0_Interact()
{
cTan.Move(303,28, eBlock, eWalkableAreas);
gFunctions.Visible = true;
gFunctions.Clickable = true;
if (Pls == true){
Display ("Plus");
Pls = false;
gFunctions.Visible = false;
}
else if (Min == true){
Display ("Minus");
Min = false;
gFunctions.Visible = false;
}
else if (Mult == true) {
Display ("Multiply");
Mult = false;
gFunctions.Visible = false;
}
else if (Div == true) {
Display ("Divide");
Div = false;
gFunctions.Visible = false;}
}

Title: Re: Setting a bool with a GUI
Post by: Khris on Mon 02/11/2015 22:19:11
Turning the GUI visible won't pause the current script function. Which means your code will only ever run lines 3-5.

Plus, from your code I gather that the player cannot select more than one operation, so using four bools does not adequately reflect the possible game states. You should rather use a single int or enum to store the selection.
(Four bools only make sense if the player can select multiple operators, then confirm the selection.)

First, set your GUI to "pause game when shown". Then put this in the Plus button's code:
  gFunctions.Visible = false;
  CallRoomScript(1);


In the room, add
function on_call(int p) {
  // plus
  if (p == 1) {
    Display("Plus");
  }
  // minus
  if (p == 2) {
    Display("Minus");
  }
  // etc
}
Title: Re: Setting a bool with a GUI
Post by: CaptainD on Mon 02/11/2015 23:39:50
Thanks Khris.
Title: Re: Setting a bool with a GUI
Post by: CaptainD on Wed 04/11/2015 20:43:54
I can't seem to get it to accept the variable.  If I don't define it anywhere I get:
GlobalScript.asc(548): Error (line 548): Undefined token 'F'


If I define it at the beginning of the room script (which is what I thought I should be doing) I get:
room8.asc(36): Error (line 36): 'F' is a global var; cannot use as name for local

I've also tried putting it in as a Global variable just out of curiosity, but this didn't work either.

I'm not sure where I should be defining the variable ?????????

Apologies in advance if I'm being stupid and it's painfully obvious...
Title: Re: Setting a bool with a GUI
Post by: Khris on Wed 04/11/2015 22:15:59
As of 3.1 AGS has a Global Variables pane where you can easily create global variables.

The alternative is via scripting:
// in GlobalScript.ash
import int F;

// in GlobalScript.asc
int F;
export F;
Title: Re: Setting a bool with a GUI
Post by: CaptainD on Wed 04/11/2015 22:25:34
I've done that and... please don't hate me! - I still get an error.

Now on the line:
Code (ags) Select
function on_call(int F){ 

I get the error:
room8.asc(34): Error (line 34): 'F' is a global var; cannot use as name for local
Title: Re: Setting a bool with a GUI
Post by: Khris on Wed 04/11/2015 23:43:43
Wait, what do you need this variable for? I thought this was about the operator selection.
To use CallRoomScript and on_call, you don't need any additional variables.

When AGS finds CallRoomScript(3) in the global script, it will call on_call(3) inside the room script's scope.
So declaring function on_call(int F) { ... } is all you need for this to work.
Here, F is a variable local to the on_call function, and its initial value will be the one passed in the function call (in this example: 3).