Compare variables

Posted by Winddancer on Fri 26 May 2023 05:06 PM — 3 posts, 10,083 views.

#0
I want to execute a command, depending on the value of a variable.

I probably am missing something here, as the following does not work:


  <alias
   match="v_sslam"
   enabled="y"
   expand_variables="y"
   group="specials"
   send_to="12"
   omit_from_output="y"
   sequence="100"
  >
  <send>Note ("inside " .. "@myspecialname")
Send("slam")
Send("@myelbow")
Send("knee")
Send("headbutt")
if @punch_attack == 'yes' then
  Send("punch")
end
Send("kick")
</send>


Regardless wether the variable has the value yes or no, the command punch is not sent.

When I change the alias to:

  <alias
   match="v_sslam"
   enabled="y"
   expand_variables="y"
   group="specials"
   send_to="12"
   omit_from_output="y"
   sequence="100"
  >
  <send>Note ("inside " .. "@myspecialname")
Send("slam")
Send("@myelbow")
Send("knee")
Send("headbutt")
if @punch_attack == yes then
  Send("punch")
end
Send("kick")
</send>


Regardless wether the variable has the value yes or no, the command punch is sent.

What am I doing wrong? What am I missing?
USA Global Moderator #1
Missing quotation marks.

Try this instead

 <alias
   match="v_sslam"
   enabled="y"
   expand_variables="y"
   group="specials"
   send_to="12"
   omit_from_output="y"
   sequence="100"
  >
  <send>
Note("inside @myspecialname")
Send("slam")
Send("@myelbow")
Send("knee")
Send("headbutt")
if "@punch_attack" == "yes" then
  Send("punch")
end
Send("kick")
</send>



Side note:

I always advise people that using expand_variables-style "@" notation inside the send block is a dangerous idea that everyone should get out of the habit of doing because it will at some point cause a problem.

@whatever gets set via find/replace before the code gets sent to the script engine, so anything you might set inside the code itself will not get picked up. In this specific case you're fine, but people usually don't realize this difference and then get stuck wondering why something a little more complex doesn't pick up the variables they just set.

I think everyone should get used to using SetVariable/GetVariable instead when inside a script section (send_to="12") and only use "@" expansion in match patterns (when you need an alias to match a pattern based on some variable that is set).
Amended on Fri 26 May 2023 05:58 PM by Fiendish
Australia Forum Administrator #2
Template:faq=32
Please read the MUSHclient FAQ - point 32.


There might be other useful tips for you on that page as well. In your case it wasn't a wildcard, but the concept is the same.