Help - Search - Members - Calendar
Full Version: Tables In Mushclient
Achaea's Forums > Off-Topic > Tech Support > Client Help
Arzoc
Hello everyone

I've made my way to Mushclient and I am slowly getting the hang of programming in Lua.
But now I stumbled upon a (probably incredibly easy-to-solve) problem.

I made a small "Warning" table with only one key in it yet. It should
warn me on several occasions (theft for example)
This is what I did in my script :

Warning = {}
Warning.theft = {
ColourNote("darkred","white","Warning! Theft attempt!!")}

I though that would be the right way to do that.
But now I want it to play that ColourNote when I get this trigger :

^You get (\d+) gold sovereigns from a canvas backpack\.$

I already tried Warning.theft and such in script and in send but I
can't figure out how to access my table!
vadimuses
CODE
Warning = {}
Warning = {theft = "ColourNote("darkred","white","Warning! Theft attempt!!")"}


And to run it, do

CODE
assert(loadstring(Warning.theft))()


here's the test case I was working with:

CODE
Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> quack = {}
> quack = {steal = "print(6)"}
> assert(loadstring(quack.steal))()
6
>
Caelan
Why not just add a function to your script file and then call it whenever you need it?


CODE
function theft_attempt ()
ColourNote("darkred","white","Warning! Theft attempt!!")}
end -- function



Then when you trigger "^You get (\d+) gold sovereigns from a canvas backpack\.$" just click "Send to Script" and where it says script (lower right) just put theft_attempt.



edit: Though just so it sticks out to me I would probably do something like:
CODE
function theft_attempt (name, line, wildcards)
ColourNote("red", "black", "[WARNING!!!!]: ", "yellow", "black", "THEFT ATTEMPT!")
end -- function
Arzoc
Thank you both, I've changed my mind and will use Caelan's method.
But Vadimuses' method will (hopefully) prove worthy in the future!
Trevize
You can make Warning.theft it a function, also, like so:

CODE
Warning = {}
Warning.theft = function ()
  ColourNote("darkred","white","Warning! Theft attempt!!")
end -- func


Here's why what you did wasn't working. You were setting the variable Warning.theft to the return value of ColourNote, whatever that was. A variable set to equal a function returns the value upon setting it, though it does do the function at that time, it won't do it when you read the variable. Setting it to equal a function definition means it will -do- the code in the definition each time, instead.

Also, Vadi's example won't work. This one:
CODE
Warning = {}
Warning = {theft = "ColourNote("darkred","white","Warning! Theft attempt!!")"}

Because he uses double quotes to save it as a string, then ALSO uses double quotes around each parameter of the ColourNote. To nest quotes you have to switch the type, so this would work:
CODE
Warning = {}
Warning = {theft = 'ColourNote("darkred","white","Warning! Theft attempt!!")'}


Also, while it works, the first line is redundant, the Warning = {} one. You will want to either declare it as a table before or after, not both. Like so:
CODE
Warning = {theft = 'ColourNote("darkred","white","Warning! Theft attempt!!")'}

or
CODE
Warning = {}
Warning.theft = 'ColourNote("darkred","white","Warning! Theft attempt!!")'
Trevize
Also, don't put it in the 'script' box. Just put theft_attempt () or Warning.theft (), whichever you used, in the Send box if you have it set to 'Send as Script'.
Trevize
Yeah, yeah, three posts in a row. I'm too tired to mess with editing my other ones. Anyways!

Here's an example of using a parameter for the actual warning text, so you can use the function over and over again.

First, make the function in your script file like so:

CODE
Warning = function (text)
  ColourNote ("darkred", "white", text)
end -- func


What that does is it lets you decide what text goes into it when you call the function like so:

For your example, use this script:
CODE
Warning ("Warning! Theft attempt!!")


And if you wanted to use it for something else, you could do, say:

CODE
Warning ("You just fell!!")

or
CODE
Warning ("Your health vials are empty!!!")


See? It just puts whatever string you place in the function call in the 'text' variable for the function, then it uses it in the ColourNote call for the string that displays.

Hope that helps!
vadimuses
My example worked OK in the interpreter
Caelan
QUOTE (vadimuses @ Dec 22 2008, 09:01 AM) *
My example worked OK in the interpreter


pwnd
Trevize
QUOTE (vadimuses @ Dec 22 2008, 09:01 AM) *
My example worked OK in the interpreter


Try it in the client. wink.gif

CODE
Compile error
World: Trevize
Immediate execution
[string "Script file"]:1: '}' expected near 'darkred'
Error context in script:
   1*: Warning = {theft = "ColourNote("darkred","white","Warning! Theft attempt!!")"}
vadimuses
Oh, I wasn't using quotes in my example - but yeah double quotes are a nono. So just use [[]] or ''.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.