[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Jscript
. . -> [Subject]  Minor problem with if
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Minor problem with if

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Tsinghahla   Sweden  (14 posts)  [Biography] bio
Date Tue 02 Jun 2009 08:23 PM (UTC)

Amended on Tue 02 Jun 2009 08:24 PM (UTC) by Tsinghahla

Message
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?


[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 02 Jun 2009 09:54 PM (UTC)
Message
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");


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Kyrock   (20 posts)  [Biography] bio
Date Reply #2 on Wed 15 Jul 2009 12:49 AM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 15 Jul 2009 05:56 AM (UTC)
Message

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";


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Kyrock   (20 posts)  [Biography] bio
Date Reply #4 on Wed 15 Jul 2009 08:29 PM (UTC)
Message
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.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,193 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]