Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Newbie Newt on Fri 02/09/2016 22:48:11

Title: Toggling variables (on/off)
Post by: Newbie Newt on Fri 02/09/2016 22:48:11
I am relatively new to AGS scripting. I would like to make an object such as a boombox toggle on and off with each click. I'm having trouble getting a simple message to toggle between on/off.  Any help would be appreciated.

Code (ags) Select

// room script file


bool IsBoomBoxOn=true;
function iboombox_AnyClick()

{
IsBoomBoxOn=true;
if (IsBoomBoxOn == true) {
    cksnipps.SayBackground("On");
       
}
   
 
  else cksnipps.SayBackground ("off");
  IsBoomBoxOn=false;
  }


Title: Re: Toggling variables (on/off)
Post by: Crimson Wizard on Fri 02/09/2016 22:56:33
A couple of mistakes there, such as command put at incorrect place ("IsBoomBoxOn=true;") and commands after "else" not grouped by curvy brackets like { ... }.
But the first issue is the inconsistent indentation style. It is always recommended to indent your script properly to make it easier to see how commands are grouped together. Simply cleaning indentation may instantly point to where the error originates.

Here's fixed code:
Code (ags) Select

bool IsBoomBoxOn = true;
function iboombox_AnyClick()
{
  if (IsBoomBoxOn == false) {
    cksnipps.SayBackground("On");
    IsBoomBoxOn=true;
  }
  else {
    cksnipps.SayBackground ("off");
    IsBoomBoxOn=false;
  }
}
Title: Re: Toggling variables (on/off)
Post by: morganw on Sat 03/09/2016 01:23:12
I think that the true and false values may need to be switched around, as at the moment IsBoomBoxOn will never be set to false.
Title: Re: Toggling variables (on/off)
Post by: Crimson Wizard on Sat 03/09/2016 03:41:43
Quote from: morganw on Sat 03/09/2016 01:23:12
I think that the true and false values may need to be switched around, as at the moment IsBoomBoxOn will never be set to false.
Oh, right. Fixed that.
Also another thing was that a variable was assigned outside of the function, which is not supported by AGS as far as I remember.

Also, since I am back to this, here is a simplier way to write the above. I did not put it in my first reply, because it may not be immediately clear for the beginner scripter, but I'll add just for reference
Code (ags) Select

bool IsBoomBoxOn = true;
function iboombox_AnyClick()
{
  IsBoomBoxOn = !IsBoomBoxOn; // <--- this inverts IsBoomBoxOn (if it were false, sets to true, and vice versa)
  if (IsBoomBoxOn)
    cksnipps.SayBackground("On");
  else
    cksnipps.SayBackground ("Off");
}
Title: Re: Toggling variables (on/off)
Post by: morganw on Sat 03/09/2016 04:34:30
Quote from: Crimson Wizard on Sat 03/09/2016 03:41:43
Oh, right. Fixed that.
Also another thing was that a variable was assigned outside of the function, which is not supported by AGS as far as I remember.

I think setting a default value is allowed (and in some cases is important to know what it going on - declaring variables in AGS will also magically initialise them to a value rather than null), but you can't call a function and assign the result.

Code (ags) Select
bool IsBoomBoxOn = true;
or
Code (ags) Select
bool IsBoomBoxOn = false;
...should be okay.
Title: Re: Toggling variables (on/off)
Post by: Crimson Wizard on Sat 03/09/2016 13:45:01
Oh, you are right, strangely I never paid attention to that.
Title: Re: Toggling variables (on/off)
Post by: Newbie Newt on Sun 04/09/2016 03:38:56
I appreciate the help. Sometimes you reach a point where you can't look at the code anymore... You need a second set of eyes.

Thanks again