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, 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 ➜ General ➜ Commands function

Commands function

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


Posted by Val   (27 posts)  Bio
Date Fri 12 Oct 2012 10:26 AM (UTC)

Amended on Fri 12 Oct 2012 11:09 PM (UTC) by Nick Gammon

Message
Hi

I have the following code, built up from past help using this forum


<aliases>
  <alias
   match="keypad_0"
   enabled="y"
   expand_variables="y"
   group="cast"
   send_to="12"
   sequence="100"
  >
  <send>local x = GetVariable("targetme")

local spellname = GetVariable("spellname1")
local targetme = GetVariable("targetme")
local class = GetVariable("class")
local target2 = GetVariable ("target2")
local bag = GetVariable ("spellbag")

Commands = {}
Commands["room"] = "cast " .. class .. " ".. spellname .. " on " .. targetme
Commands["me"] = "cast " .. class .. " ".. spellname .. " on " .. targetme
Commands["none"] ="cast " .. class .. " ".. spellname
Commands.Default = "cast " .. class .. " " .. spellname .. " on " .. target2

tosend = Commands[x]
--this line sets your command to a default if it's something not already in Commands
if not tosend then tosend = Commands.Default end

Send(tosend)
</send>
  </alias>
</aliases>



I would like to add an extra line to the Commands which will send the local variable 'spellbag' to the MUD.

I've tried "" ..bag, which will sends the variable but it will stop there and not send the second line. I've also tried using / also ; and enclosing the entire line in brackets.

I know it will be something obvious (which is just annoying for me) so the white flag is up and I'm asking for help.

Thank you

Val

[Moderator edit] Code tags added.
Top

Posted by Nick Gammon   Australia  (23,052 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 12 Oct 2012 11:11 PM (UTC)
Message
Can you post what you tried, but which did not work?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Val   (27 posts)  Bio
Date Reply #2 on Sat 13 Oct 2012 01:32 PM (UTC)
Message
Thanks for the prompt reply Nick

The one that works best is this (but not correctly) -

<aliases>
<alias
match="keypad_0"
enabled="y"
expand_variables="y"
group="cast"
send_to="12"
sequence="100"
>
<send>local x = GetVariable("targetme")

local spellname = GetVariable("spellname1")
local targetme = GetVariable("targetme")
local class = GetVariable("class")
local target2 = GetVariable ("target2")
local bag = GetVariable ("spellbag")


Commands = {}
Commands["room"] ="cast " .. class .. " ".. spellname .. " on " .. targetme
Commands["me"] = "cast " .. class .. " ".. spellname .. " on " .. targetme
Commands["none"] = "" .. bag, "cast " .. class .. " ".. spellname
Commands.Default = "cast " .. class .. " " .. spellname .. " on " .. target2

tosend = Commands[x]
--this line sets your command to a default if it's something not already in Commands
if not tosend then tosend = Commands.Default end

Send(tosend)</send>
</alias>
</aliases>


The line "Commands["none"] = "" .. bag, "cast " .. class .. " ".. spellname" is the one I have been trying. The first variable (bag) works, but the second section ("cast " .. class .. " ".. spellname) is not sent.


I've also tried the following, I'll just print the one line as the rest of the program is the same

Commands["none"] = ("" .. bag, "cast " .. class .. " ".. spellname)

I thought the brackets would allow for the full line


Commands["none"] = "" .. bag/ "cast " .. class .. " ".. spellname

I thought the / would signify a new line


Commands["none"] = (" "" .. bag, "cast " .. class .. " ".. spellname ")

I thought I may have needed the inverted commas and brackets for the complete section.


One last thing I tried was placing in the additional Command line

Commands["none"] ="" .. bag

before the mainline as the bag variable has to come first. This sent the first command but not the second.

I hope this was not too confusing to follow

Thanks

Val
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #3 on Sat 13 Oct 2012 05:02 PM (UTC)

Amended on Sat 13 Oct 2012 05:06 PM (UTC) by Twisol

Message
First: You don't need to concatenate bag with an empty string. It already is a string, so nothing happens.

Commands["none"] = bag, "cast " .. class .. " ".. spellname
Lua doesn't natively know about what MUDs are, or how you send them commands. At this level it just concerns itself with strings and variables. You're using a comma to add additional information about a value, but Lua uses the comma for a different purpose. It's used to separate values, whether they're being assigned to multiple variables or being passed to a function.
aFunction("foo", "bar")
local x1, x2 = "foo", "bar"
In your case, the second string on the right side of the assignment doesn't have a matching variable to be set on the left, so it's dropped and ignored.

In order to get the effect you want, you need to join the two values with an "end-of-line marker". The escape sequence "\r\n", which is carriage return followed by newline, denotes this.
Commands["none"] = bag .. "\r\n" .. "cast " .. class .. " ".. spellname
Alternatively, you could keep a list of lines, and send each one separately. This just defers the addition of the end-of-line marker, really - Send() adds it at the end of everything you give it.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Val   (27 posts)  Bio
Date Reply #4 on Sun 14 Oct 2012 09:08 AM (UTC)
Message
Thank you Twisol

I amended the line to


Commands["none"] = bag .. "\r\n" .. "cast " .. class .. " ".. spellname


Unfortunately I got the error


Compile error
World: MyMud
Immediate execution
[string "Alias: "]:15: unfinished string near '"' 



So I removed the "" around \r\n, just in case this was to highlight the the addition in the line and also removed the .. because I got a message saying that there was an unexpected symbol near '..'so the line now looks like this


Commands["none"] = bag \r\n .. "cast " .. class .. " ".. spellname


This allowed the program to run, but everything was sent on the same line so would not be accepted by the mud

I would use the Send () but I would like to solve the problem so I can learn from it

Let me know what you think and thank you

Val
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #5 on Sun 14 Oct 2012 06:01 PM (UTC)

Amended on Sun 14 Oct 2012 08:08 PM (UTC) by Twisol

Message
Okay, so you're writing this from within MUSHclient's Alias dialog. When MUSHclient saves an alias (or a trigger, or a timer) from the editor dialog, it pre-processes the text you give it. In this case, it immediately transforms the literal "\r\n" into a newline sequence when the script is saved, producing a malformed script. I find this behavior counter-intuitive, but here's how you work around it:
Commands["none"] = bag .. "\\r\\n" .. "cast " .. class .. " ".. spellname

Since you want the "\r\n" to reach Lua intact, you need to escape the slash into "\\", so that when MUSHclient saves the script, it becomes just "\" again.

[EDIT]: As per below, this actually happens when the alias is matched, but before the content is passed to the scripting engine. It comes to the same thing, just slightly delayed in occurrence. Thanks, Nick!

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,052 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 14 Oct 2012 07:39 PM (UTC)
Message
It doesn't save differently to what you enter, however text in the alias "send to" box is pre-processed when the alias is processed (this possibly is not a good idea). That pre-processing involves honouring \r\n and similar sequences.

Your proposed fix of doubling the backslashes works around this.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #7 on Sun 14 Oct 2012 08:05 PM (UTC)
Message
Nick Gammon said:
text in the alias "send to" box is pre-processed when the alias is processed (this possibly is not a good idea).

Ah, gotcha. So it still boils down the text being pre-processed before the Lua engine gets ahold of it.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Val   (27 posts)  Bio
Date Reply #8 on Mon 15 Oct 2012 04:10 PM (UTC)

Amended on Mon 15 Oct 2012 04:11 PM (UTC) by Val

Message
Hi

Entered the code as suggested and it works perfectly, so a big "thank you" to you Twisol and Nick.

Out of interest I tried the Send () idea mentioned by Twisol. Although it worked I got a third output which I did not want, a 0 was sent to the MUD, is there something I missed to complete the line so it stops after the second Send ()?


Commands["none"] = Send(bag) 
Send ( "cast " .. class .. " ".. spellname)


Thanks

Val
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #9 on Mon 15 Oct 2012 10:43 PM (UTC)

Amended on Mon 15 Oct 2012 10:44 PM (UTC) by Twisol

Message
I think you misunderstood what I meant. Send() returns a status code telling you whether it worked or not. Most of the time that's 0, meaning "Everything's OK!".

When I said Send() appends the "\r\n" sequence to the end of the text you give it, I meant that it sends the text, plus an "\r\n" sequence if it didn't already end in one. It doesn't actually modify the argument.

This is more in line with my intent:
-- Every entry is a list of lines.
-- In this case, the comma works as we want, because
-- the enclosing table can accept any number of values.
Commands["none"] = {"bag", "cast " .. class .. " " .. spellname}

-- If one is a list, they all should be a list. Do they have to be?
-- No, but it simplifies your code if you're only dealing with one
-- "shape" of data.
-- In this case, we have a list with only one line.
Commands["room"] = {"cast " .. class .. " ".. spellname .. " on " .. targetme}

-- Take the entry we want and concatenate its lines together.
-- The "\r\n" end-of-line delimiter will be inserted between each.
local tosend = table.concat(Commands[x], "\r\n")

Send(tosend)

While this approach has a certain architectural appeal to it, I'd recommend sticking with the simpler technique. This doesn't look like kind of place you'd need the extra expressive power, so it ends up being extra complexity.

'Soludra' on Achaea

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


25,700 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.