Jump to content
Sign in to follow this  
Ajira

How to make a toggle switch ?

Recommended Posts

I'm trying to make an on/off toggle for a function, but i can't get it to work somehow.. maybe you guys can help me out ?

In the player's init i put:

Enabled = true;
_this addAction ["Disable", "toggle.sqf", "", 0, false, true, "", "(Enabled)"];
_this addAction ["Enable", "toggle.sqf", "", 0, false, true, "", "(!Enabled)"];

toggle.sqf I put:

if (Enabled) {
 hint "off";
 Enabled = false;
else { 
 hint "on";
 Enabled = false;
}

While it does show me the option on the mousewheel menu, clicking the option doesn't seem to do jack shizzle.

Any ideas? :confused:

Share this post


Link to post
Share on other sites

Hmm. ?

if (Enabled) {
 hint "off";
 Enabled = false;
else { 
 hint "on";
 Enabled = [color="Red"]true[/color];
}  

Share this post


Link to post
Share on other sites

ok aside of the typo, it doesn't do anything.. no hint message either. if it was just the typo it would still give the hint message.

Share this post


Link to post
Share on other sites
if (Enabled) [color="Red"]then[/color]{
 hint "off";
 Enabled = false;
[color="Red"]}[/color] else { 
 hint "on";
 Enabled = true;
};

Share this post


Link to post
Share on other sites

Alternate method:

Init line of object

this addAction ["Enable", "toggle.sqf", "", 0, false, true, "", "isNil 'AJI_enabled'"];

this addAction ["Disable", "toggle.sqf", "", 0, false, true, "", "!isNil 'AJI_enabled'"];

End of toggle.sqf

if (isNil 'AJI_enabled') then {
hint "ON";
AJI_enabled = true;
} else { 
hint "OFF";
AJI_enabled = Nil;
};

Edited by tcp

Share this post


Link to post
Share on other sites

I don't think you should use "_this" in a units init field. Try "this" instead. Also, make sure you are starting Arma2 so that script errors are shown. In your shortcut, i.e.:

"path\arma2.exe" -showScriptErrors

Also, I'm not sure if you can "perform code" within the command parameters, but instead if forced to start a script.

And as tcp illustrates, a variable that haven't been initialized can be problematic sometimes. An undefined variable is not false or true, it is simply nil (nothing).

Some script examples use the template of

nil = [] execVM "script.sqf" - you should never use nil like this. Instead go

dummy = [] execVM "script.sqf"

Last part might be a bit offtopic, but nice to remember.

Share this post


Link to post
Share on other sites

Hey guys, thanks for the replies.

The problem is more or less that the scripts don't exchange the values somehow. toggle.sqf is called (added a "hint" before the check), but it doesn't see or change the value of the variable.

tried it with both "this" and "_this" on the init code, but it makes no difference.

Share this post


Link to post
Share on other sites

Perhaps because you did not initialize "Enabled" before, and you'll get an expression error: if(Enabled which is undefined) then{....

Add a line like Enabled=false; to the very beginning of your init.sqf or use the way tcp suggested, by setting it to nil every time you toggle off and ask if its nil when toggling on.

EDIT: Sorry, that was just what CarlGustaffa told you, read it too late.

Edited by Bon

Share this post


Link to post
Share on other sites

He initially had

Enabled=true;

That should work depending where it was initialized and that it was only done once (for a particular player). I was just illustrating a different method.

Also, there was a missing } bracket in my code, fixed now.

There might be a similar syntax error in the code you are using now, Ajira, so if you post your code and tell us where you are initialize everything, we might see something new.

Share this post


Link to post
Share on other sites

why do you need 2 actions menus for your Toggle function ?

Enabled = true;

_this addAction ["Disable", "toggle.sqf", "", 0, false, true, "", "(Enabled)"];

_this addAction ["Enable", "toggle.sqf", "", 0, false, true, "", "(!Enabled)"];

is this your init input ? shouldent _this be this

2nd. it will add in 2 more menus. which will become messy.

you only need one addAction on the init input define state in it aswell

this addAction [[b]"Enable"[/b], "toggle.sqf",[b]1[/b],0,false];
[/Code]

this is logic. because,. why would you disable something that has not been enabled or Vs  ?
so toogle script you just make like,
[code]
_unit = _this select 0;
_id = _this select 2;
_arg = _this select 3;
_unit removeaction _id;
_unit setVariable ["TOGGLE",_arg];// if state needed from a external script,

if (_arg == 1) then 
{
_TOG = _unit addAction [[b]"Disabled"[/b], "toggle.sqf",[b]0[/b],0,false];
hint "you can do $foo in here";
};

if (_arg == 0) then 
{
_TOG = _unit addAction [[b]"Enable"[/b], "toggle.sqf",[b]1[/b],0,false];
hint "you can do $bar in here"
};

this way you only have to add in just 1 more action menu index to the current list.

Edited by nuxil

Share this post


Link to post
Share on other sites

sweet, that totally fixed it. Thanks man :D

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×