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.
// room script file
bool IsBoomBoxOn=true;
function iboombox_AnyClick()
{
IsBoomBoxOn=true;
if (IsBoomBoxOn == true) {
cksnipps.SayBackground("On");
}
else cksnipps.SayBackground ("off");
IsBoomBoxOn=false;
}
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:
bool IsBoomBoxOn = true;
function iboombox_AnyClick()
{
if (IsBoomBoxOn == false) {
cksnipps.SayBackground("On");
IsBoomBoxOn=true;
}
else {
cksnipps.SayBackground ("off");
IsBoomBoxOn=false;
}
}
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.
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
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");
}
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.
bool IsBoomBoxOn = true;
or
bool IsBoomBoxOn = false;
...should be okay.
Oh, you are right, strangely I never paid attention to that.
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