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.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Intro
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Mon 17 Mar 2008 05:07 AM (UTC) |
Message
| I was wondering if anyone knows of a good introduction that I can use to learn Lua. I started reading one...but it made no sense to me so I am looking for one that is in like as simple english as possible please.
The previous one is http://www.lua.org/manual/5.1/manual.html.
Thanks. | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #1 on Mon 17 Mar 2008 05:36 AM (UTC) |
Message
| You want "Programming In Lua" - that is much more readable. The one you had is a reference for looking up obscure facts.
http://www.lua.org/pil/
The book "Programming In Lua" is excellently written, and is worth buying the hard copy of. However the online version is very good too. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #2 on Mon 17 Mar 2008 05:36 AM (UTC) Amended on Mon 17 Mar 2008 05:37 AM (UTC) by David Haley
|
Message
| Oh, goodness. The manual is definitely not a good place to start... it's really a reference, not a tutorial. :)
I'd definitely recommend the book Programming in Lua by Roberto Ierusalimchy, one of the Lua authors:
http://www.lua.org/pil/
Although the only book is for version 5.0, not 5.1, nearly all of the concepts apply.
EDIT:
*shakes his fist at Nick for posting at the same minute!*
:-) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #3 on Mon 17 Mar 2008 09:10 PM (UTC) |
Message
| First off thanks for the link. That one is a whole lot easier to understand.
I'm working on an if then statement and ran across this error...
Quote:
Error number: 0
Event: Compile error
Description: [string "Timer: "]:1: unexpected symbol near '@'
Called by: Immediate execution
This is the coding I am using...
if @stupidity = 1 then
Send (outr pennyroyal)
Send (eat pennyroyal)
end
the Send to box is set for script and I tried setting the Variable box to have stupidity in it but it won't work.
Also tried it this way...
if @stupidity=1 then
Send (outr pennyroyal)
Send (eat pennyroyal)
end
I'm sure it is probably a dumb mistake I am missing but I just can't find it. Anything you can tell me is appreciated whether it is how to fix it or suggestions on what I might need to look at to find out on my own how to solve it...either way thanks in advance.
| Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #4 on Mon 17 Mar 2008 11:20 PM (UTC) Amended on Mon 17 Mar 2008 11:22 PM (UTC) by Nick Gammon
|
Message
| OK, first question, is "stupidity" a MUSHclient variable or a Lua variable? The distinction is that MUSHclient variables are part of the world file (and saved when you save the world), and Lua variables are just for the current session.
If it was a Lua variable, I would write this (note various syntax changes as well):
if stupidity == 1 then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
If it is a MUSHclient variable, I would write this:
if GetVariable ("stupidity") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
A few things to be careful about:
- Lua treats numbers and strings as different (as do most languages), so if a variable has 1 in it, comparing to "1" will fail.
- The test for equality is "==" not just "=". So in your "if" statement you need to test "if this == that".
- Stuff you send to the MUD is a string and has to be quoted.
You can use the "if @stupidity == 1 then" approach, however then you have to check "expand variables" in the trigger / alias. That applies to MUSHclient variables, not Lua variables. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #5 on Mon 17 Mar 2008 11:29 PM (UTC) |
Message
| Sorry, forgot to specify that I am trying to do this in a timer.
Since I couldn't get it working that way so I tried to make a trigger off of regaining herb balance...
Now I get this message...
Quote:
Error number: 0
Event: Compile error
Description: [string "Trigger: "]:1: 'then expected near '='
Called by: Immediate execution
The code I am using is again...
if @stupidity = 1 then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
I have it set to send to script, regex, and expand variables. Not sure what else to try or do.
Heh. Just read your post Nick and it is a Mushclient variable. Will try what you suggested and see if that works.
Thanks. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #6 on Mon 17 Mar 2008 11:48 PM (UTC) |
Message
| Note that you need == to test for equality, not =. = is assignment, == is a test for equality. Lua does not allow assignment in if statements (which is rarely what people mean anyhow). |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #7 on Tue 18 Mar 2008 12:05 AM (UTC) |
Message
| If I wish to do a multiple string of if then statements...could I do it this way?
if GetVariable ("stupidity") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
if GetVariable ("paralysis") == "1" then
Send ("focus body")
end
Of would I use elseif statements? Which would be quicker and cleaner too?
If I go elseif statements it would look like this right?
if GetVariable ("stupidity") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
elseif GetVariable ("paralysis") == "1" then
Send ("focus body")
end
| Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #8 on Tue 18 Mar 2008 12:13 AM (UTC) |
Message
| Yes your elseif has the correct syntax, however I should point out that the two examples will work differently.
The first one, with two if statements, will be independent. For example, if "stupidity" and "paralysis" are both set (that is, "1") then it will send both responses.
The second example (with the elseif) will only look for "paralysis" if "stupidity" is not set. So the real question is, which do you actually want to have happen? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #9 on Tue 18 Mar 2008 12:35 AM (UTC) |
Message
| Hmmm...
Well in the game I can focus and do the herbal curing I believe...so I guess I could do the focus alone and then elseif all the herbal cures...thank you for pointing that out.
Alright for now I'll toy with what I have learned so far. Thanks for all the help everyone | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #10 on Fri 21 Mar 2008 05:56 AM (UTC) |
Message
| okay so I am trying to improve my system and was wondering if I have the coding right for this...
if GetVariable ("herbblance") == "0" then
if GetVariable ("slickness") == "1" then
Send ("outr calamus")
Send ("eat calamus")
if GetVariable ("slickness") == "0" then
if GetVariable ("stupidit") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
end
end
end
Got a feeling that this is wrong somewhere that is why I decided to double check here first.
Thanks all | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #11 on Fri 21 Mar 2008 07:07 AM (UTC) |
Message
| If you want to test several things at once, you can use "and" -- it makes the code a fair bit clearer.
So instead of:
if a then
if b then
doSomething()
end
end
you have:
if a and b then
doSomething()
end
|
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #12 on Fri 21 Mar 2008 10:03 AM (UTC) |
Message
|
Quote:
I am trying to improve my system and was wondering if I have the coding right for this ...
The syntax looks OK, the question is, what do you really want to achieve?
It would help if you indented your code a bit, like this:
if GetVariable ("herbblance") == "0" then
if GetVariable ("slickness") == "1" then
Send ("outr calamus")
Send ("eat calamus")
if GetVariable ("slickness") == "0" then
if GetVariable ("stupidit") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end
end
end
end
This is the same code, and it will do the same thing, however it makes it clearer that nothing will happen if the variable "herbblance" is not zero. Maybe or maybe not this is what you want.
It might help to write out in English what you are hoping to do, and then code from that. For example, what you effectively have is this:
- If "herbblance" is not zero, do nothing at all.
- If "slickness" is not one, do nothing at all.
- Otherwise, send "outr calamus" and then "eat calamus"
- If "slickness" is not zero, and "stupidit" is not one, do nothing more
- Otherwise, send "outr pennyroyal" and then "eat pennyroyal"
Is that really what you mean?
I should also point out, that both "herbblance" and "stupidit" look like misspellings to me. That is OK if they are simply variables and you are consistent, but if not, you will have other problems. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Sredmon
(13 posts) Bio
|
Date
| Reply #13 on Sat 22 Mar 2008 12:07 AM (UTC) |
Message
| Alright appreciate the help.
To specify...
I want the code to check for herbbalance. If the herbbalance is 1 then don't do anything. If it is 0 then move on to check slickness. If slickness is 1 then cure it if it is 0 then move on to check stupidity and so forth. I will later add in things like sleeping and aeon and anorexia but for now starting small and gradually working up.
Thanks all. | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #14 on Sat 22 Mar 2008 08:22 PM (UTC) Amended on Sat 22 Mar 2008 08:23 PM (UTC) by Nick Gammon
|
Message
| Probably the simplest way of expressing that is to use the "return" statement, which exits the current script level. Here is an example:
-- if herbbalance is 1 do nothing
if GetVariable ("herbbalance") == "1" then
return
end -- if herbbalance is 1
-- cure slickness if necessary
if GetVariable ("slickness") == "1" then
Send ("outr calamus")
Send ("eat calamus")
end -- if slickness is 1
-- cure stupidity if necessary
if GetVariable ("stupidity") == "1" then
Send ("outr pennyroyal")
Send ("eat pennyroyal")
end -- if stupidity is 1
See how this removes the need for lots of nested ifs?
However I sense a lot of repetition coming up here, how about making a function to make things simpler? Here is an example:
function checkfor (problem, cure)
if GetVariable (problem) == "1" then
Send ("outr " .. cure)
Send ("eat " .. cure)
end -- if we have that problem
end -- checkfor
checkfor ("slickness", "calamus")
checkfor ("stupidity", "pennyroyal")
The function "checkfor" checks for a problem, and if it exists, does "outr" of the appropriate cure, and then eats it. Now you only need one line for each possible ailment. This will save typing and look neater.
|
- 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.
28,126 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top