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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Lua version of "elsif"? NEW: more if/then questions

Lua version of "elsif"? NEW: more if/then questions

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


Pages: 1 2  

Posted by Caelen   (81 posts)  [Biography] bio
Date Sat 10 Jul 2010 08:32 AM (UTC)

Amended on Sat 10 Jul 2010 08:37 AM (UTC) by Caelen

Message
How would I make an "else if" argument in Lua? I'm trying to make a multi-stage conditional trigger to fill three different pipes or light any of three different pipes, based on the item I put inside them.


alias: fillpipe *
if "%1" == "elm" then
 Send "outr 1 elm"
 Send "fill @elmpipe with elm"
else if "%1" == "valerian" then
 Send "outr 1 valerian"
 Send "fill @valerianpipe with valerian"
else if "%1" == "skullcap" then
 Send "outr 1 skullcap"
 Send "fill @skullcappipe with skullcap"
else
 print "fillpipe with elm, valerian, or skullcap only."
end


I haven't found any documentation on Lua's version of the old "elsif" to work with, and "elsif" doesn't seem to work.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Sat 10 Jul 2010 08:42 AM (UTC)

Amended on Sat 10 Jul 2010 08:44 AM (UTC) by Twisol

Message
It's just "elseif". ;)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #2 on Sat 10 Jul 2010 08:44 AM (UTC)
Message
Oy, figures it'd be something simple. XD
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #3 on Sat 10 Jul 2010 08:46 AM (UTC)
Message
Also, this probably won't matter much in this case, but here's a nice little trick for when you need to check the same variable against a bunch of values:

local pipeherbs = {
  elm = true,
  valerian = true,
  skullcap = true,
}

if pipeherbs["%1"] then
  Send("outr 1 %1")
  Send("fill " .. GetVariable("%1pipe") .. "with %1")
else
  print("fillpipe with elm, valerian, or skullcap only.")
end


It semi-emulates a switch statement.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #4 on Sat 10 Jul 2010 08:53 AM (UTC)
Message
Oooh, handy. I'll remember that one.

The final draft, tested and functional,


<aliases>
  <alias
   match="fillpipe *"
   enabled="y"
   expand_variables="y"
   group="Pipe"
   send_to="12"
   sequence="100"
  >
  <send>if "%1" == "elm" then
 Send "outr 1 elm"
 Send "put elm in @elmpipe"
elseif "%1" == "valerian" then
 Send "outr 1 valerian"
 Send "put valerian in @valerianpipe"
elseif "%1" == "skullcap" then
 Send "outr 1 skullcap"
 Send "put skullcap in @skullcappipe"
else
 print "fillpipe with elm, valerian, or skullcap only."
end</send>
  </alias>
  <alias
   match="lightpipe *"
   enabled="y"
   expand_variables="y"
   group="Pipe"
   send_to="12"
   sequence="100"
  >
  <send>if "%1" == "elm" then
 Send "light @elmpipe"
elseif "%1" == "valerian" then
 Send "light @valerianpipe"
elseif "%1" == "skullcap" then
 Send "light @skullcappipe"
else
 print "lightpipe with elm, valerian, or skullcap only."
end</send>
  </alias>
</aliases>

[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #5 on Sun 18 Jul 2010 11:43 AM (UTC)
Message

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="You reel your line in and stop with the hook at * feet."
   send_to="12"
   sequence="100"
  >
  <send>require "wait"

wait.make (function ()
if %1 &gt; 30 then
 Send "reel in"
else
 wait.time (5)
 if (GetVariable("caughtafish")) == "yes" then
  wait.time(45)
  print "5"
  wait.time(1)
  print "4"
  wait.time(1)
  print "3"
  wait.time(1)
  print "2"
  wait.time(1)
  print "1"
  wait.time(1)
  print "REEL IN"
 else
  Send "reel all the way"
 end
end
end)</send>
  </trigger>
</triggers>


I'd like to make the second if/then check for one of two things (if the variable is true OR if the wildcard is greater than 10) and return as true and continue the timer. I want it to be continue the timer if one or both of the conditions are met, but not if neither of them are. How can I do this?

My other option is to just make a second trigger that picks up the same thing, have it keep evaluating and push the sequence up a bit, and have it set the variable in question, but I'm sure there's an easier way... I saw something with a (<text>|<text>) format for an OR setup, but I'm not sure how to use that within an if/then.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #6 on Sun 18 Jul 2010 07:16 PM (UTC)
Message
The first thing you need to realize is that the regexp syntax - stuff like ^I (like|hate) pie\.$ - is not Lua. It's parsed by a separate engine called PCRE. So it's probably not a good idea to take syntactic cues from regexp syntax. :)

At any rate, you can do exactly what you're asking for. Example without it:

if a then
  if c then
    Note("foo")
  elseif d then
    Note("foo")
  end
elseif b then
  if c then
    Note("foo")
  elseif d then
    Note("foo")
  end
end


That can be straightened out to:

if (a or b) and (c or d) then
  Note("foo")
end


Basically, the "or" corresponds to two branches having the same content, while the "and" corresponds to an if within an if. I hope that made sense!

Also, a/b/c/d above are just placeholders. You could drop in other boolean expressions like %1 > 10 as you asked for below. And parentheses are important! I can never remember the precedence rules for and/or/not, so I just avoid the issue with parentheses so it's abundantly obvious what it does.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #7 on Mon 19 Jul 2010 09:00 AM (UTC)
Message
Brings me back to my old college classes in logic with that setup... and yeah, I see it now. Thanks! Even showed me how to do the "and" arguments too :3
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #8 on Tue 20 Jul 2010 10:17 PM (UTC)
Message
Since I love ifs, more questions! Since I can now use "true" and "false" as my variables (I forgot to get 4.53... now I have 4.54 <3 ), would I be able to shorten

if (GetVariable("asdf")) == "true" then

to

if (GetVariable("asdf")) then

and get the same effect?
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Tue 20 Jul 2010 10:25 PM (UTC)

Amended on Wed 21 Jul 2010 12:03 AM (UTC) by Nick Gammon

Message
You need to distinguish between MUSHclient variables (which are saved in the world file) and Lua variables (which are not, unless you "serialize" them).

MUSHclient variables are always strings (ie. alphanumeric) and in Lua a string is always true.

That is:


x = "true"

if x then
  print "this is true"
end -- if

x = "false"

if x then
  print "this is true"
end -- if


Compare to:



x = true

if x then
  print "this is true"
end -- if

x = false

if x then
  print "this is true"
end -- if


If you try this out, the first example will print "this is true" both times, the second one, only the first time.

For simple tests within triggers etc. you are better off just using Lua variables, in which case you can use "if x then ...".

But if you want to use MUSHclient variables, you have to stick to testing the string, eg. "if GetVariable ("x") == "true" then ...".

- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #10 on Tue 20 Jul 2010 11:29 PM (UTC)
Message
The only values considered boolean-false in Lua are nil and false. A string in a boolean context (as in your "if GetVariable('foo')" question) is always considered true, even if it's empty. That's why you do need the comparison to "true", because it compares the two strings and tells you if they're identical.

Lua variables are more versatile and arguably easier to use in general. I usually reserve MUSHclient variables for tasks like regexp interpolation (like ^(@!friends) waves hello\.$), serialization, and communication between plugins, because those are the only common things Lua variables can't readily do.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #11 on Wed 21 Jul 2010 01:41 AM (UTC)
Message
Hmm... alright, I think I see it. But, that raises another question. What is a Lua variable, compared to a MUSH variable?
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #12 on Wed 21 Jul 2010 01:45 AM (UTC)
Message
Lua:
local x = 42 -- Lua variable, setting.
Note(x)      -- Lua variable, using.


MC:
SetVariable("x", "42") -- MC variable, setting. STRING, not number.
Note(GetVariable("x"))       -- MC variable, getting.


MC variables are just storage slots, cubbyholes you can drop data into. To do anything with the data, you need to get it back and then do whatever. Lua variables, while also fundamentally "storage", can be manipulated directly.

It should be noted that GetVariable() returns a string, which is a Lua value. Likewise, SetVariable() takes a string. The main difference is that they can only be strings, and you have to get them back with the function before you can mess with them. MC variables are not fundamentally different beyond this indirection.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #13 on Wed 21 Jul 2010 03:02 AM (UTC)
Message
When you set a variable, is it stored and ready to use by any trigger or alias that might call on it? I use assorted triggers to set variables that are used to say what to do, like, one variable per affliction to cure in Achaea, and a single "healme" alias that cures one affliction per use in the order I want them cured. Can I do the same thing with Lua variables instead of MUSH variables?
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #14 on Wed 21 Jul 2010 03:05 AM (UTC)
Message
Yes, but you just remove the 'local' prefix. When you use 'local', it's only available within the block it's defined in. A trigger's send box basically counts as a block.

When you remove the 'local' prefix, the variable is set into the "environment", or in other words becomes a global. It can be accessed from anywhere as long as there isn't another local variable of the same name hiding it.


Basically, just use 'local' for temporary variables and don't use 'local' when you want to use the same data within more than one block.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[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.


48,888 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

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 HostDash]