Lua Beginner

Posted by AaronM86 on Mon 29 Mar 2010 07:04 PM — 17 posts, 65,159 views.

#0
I was wondering if someone could help teach me how to define variables with Lua. Last night I just switched from Jscript to Lua and am trying to read up on tutorials and what not, but am having trouble finding this. I figure if I can get one thing figured out, it'll give a foundation for it in my mind and then I can expand my tinkering from there.

So, for an example, what I'm trying to do here is, I have an alias titled, "fd *". This used to set a variable titled, "fishDirection". I made this variable through the Variable submenu in my World Configuration. It used to work fine with Jscript, but I'm getting errors now.

The following is what I have edited the alias to thus far, trying to apply Lua to it.

<aliases>
<alias
match="fd *"
enabled="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>SetVariable ("fishDirection", "%1" )
Note("fishDirection set to '" + "%1.'")
Send("look");</send>
</alias>
</aliases>
#1
I had an idea I wanted to test, wondering if it might work, in Jscript I used to make functions and I could run them by using say Send: eightball(), and that would run the eightball function from my script notepad. Would the same thing work in Lua or is there a different way of writing it?
Australia Forum Administrator #2
Concatenation in Lua is .. rather than +

So it should read:


Note("fishDirection set to " .. "%1")


Although since wildcard strings are substituted you could just do:


Note("fishDirection set to %1")


As for calling the function, yes you can do it that way. Another way is to put "eightball" in the "script" box of the trigger. But I assume your question is for calling maybe different functions here and there.
#3
Excellent :D This was great help and it will surely get me on my way to putting other puzzles together. Thank you for taking your time to help me.
#4
Hello, was wondering if I could get a little assistance with a chat redirector script that I started to have troubles with after I set it to keep evaluating, as I needed to make another "say" trigger recently and couldn't get it to fire unless I had set my plugin's redirector script logging my says orginally to keep evaluating also. The trigger in the plugin looks like this.


<triggers>
  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^\((.*)\/(.*)\)\:(.*)$"
   omit_from_output="y"
   regexp="y"
   script="redirect"
   sequence="100"
  >
  <send></send>
  </trigger>

  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^(.*?) (say|ooc|yell|shout|ghosttalk|tell|newbiechat|war)(.*?)$"
   omit_from_output="y"
   regexp="y"
   script="redirect"
   sequence="100"
  >
  </trigger>
</triggers>



What is happening is, when I send a chat with mixed words in the trigger it will send my message twice. For example:

I type 'guildchat ooc'

(Terkal/[+} Sovereign {+]): ooc
(Terkal/[+} Sovereign {+]): ooc

Above is what is sent to my chatworld, and the double message is sent to all guild members.
Amended on Wed 31 Mar 2010 05:10 AM by Nick Gammon
Australia Forum Administrator #5
They both call "redirect" so it sends the stuff twice. Make only one of them call redirect, the other one can call something else.
#6
Hrm, never had that problem anytime before and they were always set to run the same thing, this only started happening when I added the keep evaluating here:

keep_evaluating="y"
match="^(.*?) (say|ooc|yell|shout|ghosttalk|tell|newbiechat|war)(.*?)$"
omit_from_output="y"



Regardless, I went ahead and change it to a different call name for both and it is still having the same results of double sending.

<trigger
enabled="y"
match="^\((.*)\/(.*)\)\:(.*)$"
omit_from_output="y"
regexp="y"
script="redirecty"
sequence="100"
>

That is the change I made to the call of the first one.
#7
My problem may lie directly within the script itself, I had to add a redirecty function.

<script>
<![CDATA[
chat_world = "Dark-Legacy Chats"
local first_time = true

function redirect (name, line, wildcards, styles)

local w = GetWorld (chat_world)

if first_time and not w then
local filename = GetInfo (67) .. chat_world .. ".mcl"
Open (filename)
w = GetWorld (chat_world)
if not w then
ColourNote ("white", "red", "Can't open chat world file: " .. filename)
first_time = false
end
end

if w then
for _, v in ipairs (styles) do
w:ColourTell (RGBColourToName (v.textcolour),
RGBColourToName (v.backcolour),
v.text)
end
w:Note ("")

end

end


function redirecty (name, line, wildcards, styles)[

local w = GetWorld (chat_world)

if first_time and not w then
local filename = GetInfo (67) .. chat_world .. ".mcl"
Open (filename)
w = GetWorld (chat_world)
if not w then
ColourNote ("white", "red", "Can't open chat world file: " .. filename)
first_time = false
end
end

if w then
for _, v in ipairs (styles) do
w:ColourTell (RGBColourToName (v.textcolour),
RGBColourToName (v.backcolour),
v.text)
end
w:Note ("")

end

end

]]>
</script>
#8
function redirecty (name, line, wildcards, styles)[

I removed "[" and got it to load right, typo error on my part. But it's still doing the double send, hrm *ponder*...
Australia Forum Administrator #9
AaronM86 said:

Hrm, never had that problem anytime before and they were always set to run the same thing, this only started happening when I added the keep evaluating here:


Well previously, only one trigger fired. Now two fire and they both call the same function so it does its stuff twice.
#10
Hello, I've converted all my old jscript into lua and organized all my triggers into one file, organized by group name. Made an alias to turn off entire group names at a time, works very efficiently! :D One question I was wondering about, however:

<triggers>
<trigger
enabled="y"
expand_variables="y"
group="Looting"
match=" *an ancient tome of knowledge*"
send_to="12"
sequence="100"
>
<send> world.Send("get all.tome @trapp")
if string.lower("%2") == "1" then
world.Send("exam tome")
elseif string.lower("%2") == "2" then
world.Send("exam tome")
world.Send("exam tome")
end</send>
</trigger>
</triggers>


Will using @trapp in the world.Send still fire the variable correctly in this circumstance?
Australia Forum Administrator #11
Yes but I don't understand why you are doing this:


if string.lower("%2") == "1" then


The number 1 doesn't have an upper and lower-case version, so changing the case doesn't achieve anything.

USA #12
I told him to use it in an earlier topic (where case was important), and he probably just modified what he knew worked. It's how you learn! :) But yeah, Nick's right. 1 has only one "case", so you shouldn't have to lowercase anything you're comparing against it.
Amended on Thu 01 Apr 2010 07:14 AM by Twisol
#13
Nod, Twisol, that is the case. Without using the string check, just to confirm, would I write it like this:

if("%2") == "1" then





Thank you both again, especially for your time and patience; you have been loads of support.


#14
Hello and good morning :)

Was hoping to get a little assistance, I've been searching through the list of functions to try and piece this all together. My goal is to have an alias that will Note a list to the world of all my triggers, by group name, that are enabled/disabled. Have one note for all that trigger groups that are enabled and another note for all that are disabled.

I've narrowed it down, to guessing, that I have to use some sort of combination of:

tl = GetTriggerList()
if tl then
for k, v in ipairs (tl) do
Note (v)
end -- for
end -- if we have any triggers

/

Note (GetTriggerOption ("mytrigger", "match"))

"group": (string - group name)
"enabled": y/n - trigger is enabled

/

Note (GetTriggerInfo ("monster", 2))

8: Enabled (boolean)
26: Group name (string)



Those are just copy/pastes of the function examples from the website here. Am I on the right track with guessing these are the functions I would have to use to accomplish my goal?


Australia Forum Administrator #15
Yes you are on the right track. Combined with a bit of table handling. This should do it:


<aliases>
  <alias
   match="show_groups"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

local tl = GetTriggerList()
if not tl then
  print "No triggers"
  return
end -- if

local groups = {}

-- work out which ones enabled 

for k, v in ipairs (tl) do

  local group = GetTriggerOption (v, "group")
 
  -- create table entry for this group first time around
  groups [group] = groups [group] or { enabled = 0, disabled = 0 }
 
  if GetTriggerOption (v, "enabled") == 1 then
    groups [group].enabled = groups [group].enabled + 1
  else
    groups [group].disabled = groups [group].disabled + 1
  end -- if

end -- for

-- show results, per group

for k, v in pairs (groups) do
  ColourTell ("white", "", string.format ("%%15s: ", k))
  if v.enabled &gt; 0 then
    ColourTell ("green", "", string.format ("%%i enabled ", v.enabled))
  end -- if
  if v.disabled &gt; 0 then
    ColourTell ("yellow", "", string.format ("%%i disabled ", v.disabled))
  end -- if
  Note ""  -- new line

end -- for

</send>
  </alias>
</aliases>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


A whole group is not necessarily enabled (so this alias shows, per group, how many in that group are enabled and how many disabled).
Amended on Thu 01 Apr 2010 09:51 PM by Nick Gammon
#16
Ah, excellent! I had attempted to set this up all morning lol, kept running into a bit of trouble. I see what I was doing wrong now. Thanks :D Appreciate it a ton.