Struggling.

Posted by Siris on Fri 27 Aug 2010 10:35 PM — 18 posts, 73,758 views.

#0
I'm struggling with grasping LUA's language, I'm trying to set up some direction aliases and cannot for the life of me get it to work.

<aliases>
<alias
match="ne"
enabled="y"
send_to="10"
keep_evaluating="y"
sequence="100"
>
<send>if fighting = 1 then
send "flee northeast"
else
send "northeast"
end</send>
</alias>
</aliases>


I've scripted extensively in zmud and I want a more powerful {and free} Client. However I can not figure out what I'm doing wrong. This is the error I get:

Compile error
World: Moongate
Immediate execution
[string "Immediate"]:1: 'then' expected near '='
Amended on Fri 27 Aug 2010 10:39 PM by Siris
USA #1
You want to use ==, not =. Single-equals is for assignment (i.e. storing a value in a variable). Double-equals is for comparison. :)

Also, you need to use Send("flee northeast"). Case is extremely important, "send" refers to a different value than "Send".

You also have a syntax error in the else branch:

send, "northeast"


Commas never go between the function and its arguments, only between the arguments themselves. :)
Australia Forum Administrator #2
There is a fantastic online book about Lua, written by the developer:

http://www.lua.org/pil/

In your case you have two problems. Unlike what you may be used to in zMud (and Visual Basic) to test for "equals" you have to use ==.

Thus:


if fighting == 1 then


A single "=" is used to assign. For example:


a = 2    -- assign
if a == 2 then   -- test
  print ("a is two!")
end


Also, the internal functions are case-sensitive, it is Send, not send.

Use the "Complete" button in the editor to complete a function name, which also corrects the capitalization.

Australia Forum Administrator #3
Ninja'd! And I didn't spot the comma.
#4
lol, i proof read it again and tried to fix my syntax errors, i'm not new to scripting just new to lua. ;) thank you for your fast response guys.
#5
<variables>
<variable name="fighting">1</variable>
</variables>

<aliases>
<alias
match="ne"
enabled="y"
send_to="12"
keep_evaluating="y"
sequence="100"
>
<send>if fighting == 1 then
Send ("flee northeast")
else
Send ("northeast")
end</send>
</alias>
</aliases>

Errors are done with, however it will not send flee northeast to the mud. I hate to seem like such a newbcake, but I fail to see what I'm missing
USA #6
Ah, because there are two kinds of variables: MUSHclient variables, and the scripting language's native variables. MUSHclient variables are held by MUSHclent, and you're using one by using <variable>. On the other hand, scripting variables (in this case, Lua's variables) are native language constructs, which you're attempting to use in your if-statement.

Change fighting in the if-statement to GetVariable("fighting"). Also, because MUSHclient variables can only contain strings, you need to check it against "1" instead of 1.

That's probably the most common trip-up when you're dealing with MUSHclient. :)
Australia Forum Administrator #7
Yeah. The other question is how does "fighting" get set in the first place?

It could be easier to set the Lua variable rather than the MUSHclient variable.

eg.


<triggers>
  <trigger
   enabled="y"
   match="* kicks you!"
   send_to="12"
   sequence="100"
  >
  <send>fighting = 1</send>
  </trigger>
</triggers>


Now that trigger sets the Lua variable rather than the MUSHclient variable, and then your alias works as is.
#8
Thank you again. I will read the manual, and hopefully stop pestering you fine gentlemen. One last question though. Nick Your gui mapper seems to only use one line exit codes do you think it could be adapted to read multiple line exit codes? My mud's room code is as follows:


 Mayor's Office                                         -      -      -
(-------------------------------------------------)     - <---(M)-D-> -
                                                        -      S      -

  The office of Lord Telleri is luxurious enough to impress visitors with
the importance of the occupant, but still has all the markings of a working
office.  Despite the best efforts of secretaries and assistants, papers are
stacked on every corner of the priceless hand-carved desk, and half-read
briefs litter the credenza.  
*[Quest] Lord Telleri stands before you with a slight grin on his face.


and unfortunately some room descriptions are identical, however could that be manually mapped when the rooms are identical?
Amended on Fri 27 Aug 2010 11:33 PM by Nick Gammon
Australia Forum Administrator #9
The GUI mapper really comes in two parts. The "core" mapper is a Lua module (mapper.lua) which comes with MUSHclient. This will draw a GUI map based on whatever information gets fed into it.

There are a couple of implementations of mapper plugins (one is the ATCP_Mapper.xml). I also did a "simple" one which doesn't use the telnet codes that the ATCP mapper does. This is presented here:

http://www.gammon.com.au/forum/bbshowpost.php?id=10138&page=7

As described in that thread, the core problem is to recognize rooms uniquely. If they happen to give you room numbers, that's great! Otherwise I hashed up the room description (and the exits I think) to give something that hopefully is unique.

In your case you might be able to adapt the simpler mapper to hash up the lines you showed (Mayor's Office, exits, description) to give something unique.

The exits themselves aren't that critical, as I think the plugin recognized when you typed a direction (eg. East) and when you entered a new room (because of the hash) and therefore it knows that east from room x takes you to room y.

There might be a way of handling absolutely identical room descriptions (assuming the exits are also identical) by remembering which room led to this one.

For example room a (reached from b) could be different from room a1 (reached from c) even if a and a1 had the same descriptions (because you remember how you got there).

USA #10
Twisol said:

...
Also, you need to use Send("flee northeast").

...


To be perfectly accurate, in Lua when passing a single string literal or single table construct to a function, the parentheses are optional.


print "Hello world."

require "tprint"
tprint { 1,2,3 }


print("Hello world.")
require("tprint")
tprint( {1,2,3} )


are equivalent.

So his original

send "flee northeast"

only needed the capitalization fixed.
USA #11
WillFa said:
To be perfectly accurate, in Lua when passing a single string literal or single table construct to a function, the parentheses are optional.

It's an exceptional case grandfathered in from Lua's roots, and it only works when you have just one argument (which must be a string literal or a table literal). I'd rather teach people the way that always works first. =/

#12
thank you guys, got everthing working. hopefully i woulnt be bugging you guys for simplistic problems everyday.
USA #13
Well, don't be too afraid to ask again. :) I helped a lot of really new users every day in the MUSHclient clan on Achaea, and it makes me happy to see someone learn the ropes and begin doing interesting things.
USA #14
Twisol said:

WillFa said:
To be perfectly accurate, in Lua when passing a single string literal or single table construct to a function, the parentheses are optional.

It's an exceptional case grandfathered in from Lua's roots, and it only works when you have just one argument (which must be a string literal or a table literal). I'd rather teach people the way that always works first. =/




That's what I said...

I don't agree with your insinuation that it's "grandfathered". It was a deliberate choice and I've never read anything suggesting that if they had the choice to make all over again, they wouldn't make the same one. I don't think it's so exceptional when 95% of all Lua code I've read or written starts with it. (require "foo")

You're fixing mistakes that aren't mistakes. He didn't 'need to use Send()'. He needed a capital S. Don't teach that Send "flee northeast" is wrong, because it's not, and it will confuse your student when they do try to figure out why something like require "foo" works. (Is require a keyword? Nope, it's just a function included by default. But I was taught that functions need parentheses...)





http://xkcd.com/386/
Amended on Sat 28 Aug 2010 03:20 AM by WillFa
USA #15
WillFa said:
That's what I said...

I know it is. I just disagree that it needed to be said.

WillFa said:
I don't agree with your insinuation that it's "grandfathered". It was a deliberate choice and I've never read anything suggesting that if they had the choice to make all over again, they wouldn't make the same one. I don't think it's so exceptional when 95% of all Lua code I've read or written starts with it. (require "foo")

The table-literal calling shorthand came from SOL, Lua's predecessor. The semantics changed from "defining a record" to "calling a function with this table", but the syntax remained. http://www.lua.org/history.html

I can't find anything about the string-literal calling shorthand.

I'm not saying these things shouldn't exist, nor am I saying they shouldn't be taught. I'm saying the universal way should be taught first. And please; I never said foo{} and foo"" didn't work, I expressly didn't mention them. I wanted to solve the problem, and give a syntax that they can use anywhere.

WillFa said:
You're fixing mistakes that aren't mistakes.

Same to you. :)


[EDIT] Just so you know, I only responded to your original point because you aimed it specifically at what I had said (or rather, hadn't said). I don't care if you say it or not, but don't make me look ignorant. I do a good enough job of that on my own :P

[EDIT] For the sake of the OP, this marks my last post on the subject. I think we both understand eachother at this point, and I don't like arguments :S
Amended on Sat 28 Aug 2010 03:36 AM by Twisol
USA #16
If I could just clarify something here... In my experience as a consultant, I get to deal with different classes of people, whether the "Suits", the Programmers, or the IT staff. When talking to them, you need to have some frame of reference so everyone's "on the same page" and "speaking the same language" and communicating effectively.

When dealing with the computer types, the common jargon seems to sprout from the RFCs. When reading the RFCs, the language is very specific on what verbs to use, and when.

If "must" or "need" is used, it is a requirement for the implementation.
"Should" denotes something that's strongly recommended.
"May" is optional.


Perhaps I over-reacted to reading your quote, and I just wanted to clarify it for the other Geeky RFC minded people out there.

In this context: "You need to use \[this format]" Is inaccurate, because that format isn't an absolute requirement. (And if it was a requirement to use parens, then the string/table scenario can't work.)

So, in RFC-speak:
You SHOULD use the form:
Func(param, param2)
unless you're passing 1 param and that parameter is a literal string or table, which you MAY use the form:
Func "param"
or
Func {param}



So, this whole 'argument' is because the word "need" has very specific and absolute meaning to me and some other people. :)

Pedantic? Yea... but we're talking about code. And that brings up the old joke

"This stupid computer doesn't do what I want it to. It only does what I tell it to!"



I apologize for any hurt feelings and ruffled feathers. :)
USA #17
Hehe, okay, I see where you're coming from now. I am, unfortunately, not as precise as you are in your terminology. My "you need to" is not an absolute, but is relative to what is already present. I don't read RFCs very often, but I know that they try to nail things down exactly. You sometimes need to understand the origin of the "exact" terminology to know what the specific meaning is, though. I just try to explain things as easily to understand as possible. Colloquially, you might say.