Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Basic if statements

Basic if statements

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


Posted by SR388   (4 posts)  Bio
Date Sun 09 Dec 2007 07:05 AM (UTC)
Message
I've learned how to check variables and modify them based on certain data, such as the example below:

crit = tonumber (GetVariable ("crit")) or 0
crit = crit + 1
SetVariable ("crit", crit)

I'm curious how to enter a command based on a variable. For example,

crit = tonumber (GetVariable ("crit")) or 0
if crit = 1 [execute command]
if crit = 2 [execute some other command]

This way I can set up an automated chain of events from start to finish.
Top

Posted by Onoitsu2   USA  (248 posts)  Bio
Date Reply #1 on Sun 09 Dec 2007 08:12 AM (UTC)
Message
something like this

if crit == 1 then
WHATEVER
elseif crit == 2 then
WHATEVER ELSE
end -- if

the --if is optional but can be recommended so you know that is the end of an if statement and not a loop or other block structure that uses the 'end' as a termination word.

-Onoitsu2
Top

Posted by Isthiriel   (113 posts)  Bio
Date Reply #2 on Sun 09 Dec 2007 11:22 AM (UTC)
Message
Also, as your scripts become more complicated, indentation is your friend. Use an editor that has a fixed width font (aka monospaced, such as Courier New) and then make matching levels of nested blocks to line up vertically so you can see at a glance what code belongs to which block, like so:


function doSomething(stuff)
  for key, val in pairs(stuff)
    if val != nil then
      print(key .. ":" .. val)
    end --if
  end --for
end --doSomething()


How much you indent is a matter of personal choice. Holy wars are fought over whether two-space, three-space, four-space and tabs-instead-of-spaces is the "best".
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #3 on Mon 10 Dec 2007 05:24 PM (UTC)
Message
If you are looking to make a switch or case statement, it might also be useful to make a table of values or functions. See this post for a few more details: http://www.gammon.com.au/forum/?id=7891 Granted, most of that is just me being bored rather than being useful, but there is a nice little example of a set of if/elseif statements with return values.

It is much easier to fight for one's ideals than to live up to them.
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.


21,310 views.

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

Go to topic:           Search the forum


[Go to top] top

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