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

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Basic if statements

Basic if statements

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

Posted by SR388   (4 posts)  [Biography] bio
Date Sun 09 Dec 2007 07:05 AM (UTC)
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.
[Go to top] top

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

if crit == 1 then
elseif crit == 2 then
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.

[Go to top] top

Posted by Isthiriel   (113 posts)  [Biography] bio
Date Reply #2 on Sun 09 Dec 2007 11:22 AM (UTC)
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".
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #3 on Mon 10 Dec 2007 05:24 PM (UTC)
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.
[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.


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.


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]