Minor problem with if

Posted by Tsinghahla on Tue 02 Jun 2009 08:23 PM — 5 posts, 23,550 views.

Sweden #0
Hello!

Can't get a simple if-script to work and I can't work out where I am doing it wrong.

First, two aliases to set a variable on/off which seems to work as intented.

First one:
world.setVariable( "Healing", "true" );

Second one:
world.setVariable( "Healing", "false" );

No problems there.

My script:

<triggers>
  <trigger
   enabled="y"
   match="^paralysed."
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>var Healing;

Healing = world.GetVariable("Healing");

if (Healing == true)
  world.Execute("heal me paralysis");
else
  world.note("Healing is disabled");</send>
  </trigger>
</triggers>


I tried to use the example from the documentation and simply switching 'MyName' as the example was and use Healing (as that is the variable I use)

I've tested this and no matter if the variable is true or false, I only get the world.note to fire and never the execute.

'hon' is the alias to set the Healing variable to true

TRACE: Matched alias "hon"
paralysed.
TRACE: Matched trigger "^paralysed."
Healing is disabled

What am I doing wrong here?


Amended on Tue 02 Jun 2009 08:24 PM by Tsinghahla
Australia Forum Administrator #1
Your variable is "true" (which is a string with "true" in it) but you are testing against the boolean value true, which is a different thing.

Try:


if (Healing == "true")
  world.Execute("heal me paralysis");
else
  world.Note("Healing is disabled");

#2
if you use true and false then you could also use it as is without testing.

if (Healing) {
  world.Execute("heal me paralysis");
} else {
  world.note("Healing disabled");
}


Also, if you wanted to test for false you can just put !Healing.
Australia Forum Administrator #3

Healing = world.GetVariable("Healing");


Healing is a string here, not a boolean. To use it the way you suggest you would have to convert it, eg.


Healing = world.GetVariable("Healing") == "true";

#4
Jscript doesn't care if it's a string cause it automaticly casts. In order for it to care you'd have to test with a triple equal sign. i.e. :

Healing = world.getVariable("Healing");
if (Healing === true) {
  world.note("Healing is true");
} else {
  world.note("Healing equals a string and it's value is true");
}

in this case it'll note "Healing equals a string and it's value is true" because it's testing for a boolean true in which it isn't. If I put quotes around true then you'd get the "Healing is true".

I use true and false in my variables all the time and use it as is.