Can someone tell me what's wrong with this script please?

Posted by Chade on Wed 28 Jan 2009 05:10 PM — 5 posts, 19,456 views.

#0
Hi,

Was just wondering what was wrong with this script which is contained in an alias. If I use the alias no matter what the variables hammer1_ and hammer2_ equal it only performs the first two actions.

<aliases>
<alias
name="clf7_"
match="^\s*clf7\s*$"
enabled="y"
expand_variables="y"
group="Chade_Combat"
regexp="y"
send_to="12"
ignore_case="y"
keep_evaluating="y"
sequence="10000"
>
<send>if @hammer1_ == calcise and
@hammer2_ == calcise then
Execute("art maneuver perform crushleg left " .. GetVariable("target"))
Execute("art maneuver perform crushleg left " .. GetVariable("target"))
else
Execute("wipe hammer11898;wipe hammer168415;envenom hammer11898 with calcise;envenom hammer168415 with calcise")
Execute("art maneuver perform crushleg left " .. GetVariable("target"))
Execute("art maneuver perform crushleg left " .. GetVariable("target"))
end</send>
</alias>
</aliases>

Thanks for any help.
Netherlands #1
Simple. You are probably not comprehending how script variables work with Lua variables.

if @hammer1_ == calcise and
   @hammer2_ == calcise then
  -- ...
end


This code will be translated by MUSHclient into the following Lua code:

if hammer1234 == calcise and
   hammer5678 == calcise then
  -- ...
end


In other words, it compares the Lua variables hammer1234 and hammer5678 to the Lua variable calcise. And my gutfeeling is that is not what you want. All non-assigned Lua variables are interpreted as nil.

In other words:

if nil == nil and
   nil == nil then
  -- ... this always executes! Oops!
end


So, what is the solution you are looking for?

if "@hammer1_" == "calcise" and
   "@hammer2_" == "calcise" then
  -- This works as you (most likely) intended it to work.
end


This way Lua is made to compare strings with strings.
#2
Thanks,

My coding knowledge is limited to say the least, brackets make it work perfectly and this is going to save me a hell of a lot of resources!

Cheers,

Chade
#3
Quotation marks not brackets... it's late, you'll have to forgive me!
Australia Forum Administrator #4
You seem to be mixing methods here:


if @hammer2_ == calcise then 
  Execute("art maneuver perform crushleg left " .. GetVariable("target"))


I suspect you have copied examples from different places. ;)

Apart from the quotes problem, a more consistent approach is either:



if "@hammer2_" == "calcise" then 
  Execute("art maneuver perform crushleg left @target")


or:


if GetVariable ("hammer2_") == "calcise" then 
  Execute("art maneuver perform crushleg left " .. GetVariable("target"))


Be cautious when using the variable substitution in scripts (eg. @hammer2_) because the subsitution is done before the script starts running. For example:


SetVariable ("hammer2_", "sword")
print ("@hammer2_") --> prints old value, not "sword"


In other words, MUSHclient replaces things like @hammer2_ before running the script, so changing the hammer2_ variable value during the script execution is too late.

A neater method in Lua is to use the "var" module, like this:



require "var"  -- do this once at the start

if var.hammer2_ == "calcise" then 
  Execute("art maneuver perform crushleg left " .. var.target)


The var module uses Lua metatables to lookup the MUSHclient variable contents, based on what variable you are trying to access. You can also assign like this:


var.target = "Nick"