Gammon Forum
Entire forum
MUSHclient
Lua
Can someone tell me what's wrong with this script please?
Can someone tell me what's wrong with this script please?
|
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Chade
(13 posts) bio
|
Date
| Wed 28 Jan 2009 05:10 PM (UTC) |
Message
| 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. | top |
|
Posted by
| Worstje
Netherlands (899 posts) bio
|
Date
| Reply #1 on Wed 28 Jan 2009 05:33 PM (UTC) |
Message
| 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. | top |
|
Posted by
| Chade
(13 posts) bio
|
Date
| Reply #2 on Wed 28 Jan 2009 05:42 PM (UTC) |
Message
| 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 | top |
|
Posted by
| Chade
(13 posts) bio
|
Date
| Reply #3 on Wed 28 Jan 2009 05:45 PM (UTC) |
Message
| Quotation marks not brackets... it's late, you'll have to forgive me! | top |
|
Posted by
| Nick Gammon
Australia (22,989 posts) bio
Forum Administrator |
Date
| Reply #4 on Wed 28 Jan 2009 07:19 PM (UTC) Amended on Wed 28 Jan 2009 07:20 PM (UTC) by Nick Gammon
|
Message
| 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:
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | 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.
13,111 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
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.