ILua scripting

by Unknown

Back to Project L.

Unknown2007-11-19 08:11:14
This worked for me:
CODE
tobias@cx2:~$ lua
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> t = {}
> t = function() print("A") end
> t = function() print("B") end
> t = function() print("C") end
> for _, f in ipairs(t) do f() end
A
B
C
>
Unknown2007-11-19 10:29:42
In Lua, tables are not guaranteed to keep the same order, though. I found a phpTable library (on the Lua wiki) that takes care of that quirk, and I used that for ensuring that my priority tables didn't get unsorted.
Unknown2007-11-19 10:49:36
QUOTE(Zarquan @ Nov 19 2007, 11:29 AM) 459269
In Lua, tables are not guaranteed to keep the same order, though. I found a phpTable library (on the Lua wiki) that takes care of that quirk, and I used that for ensuring that my priority tables didn't get unsorted.

Hrm.. Are you really sure that holds true for "pure array tables" too? These, when used with the ipairs() iteration function, *should* always give you the same and correct order. I know you're right when it comes to hash tables but I don't think it's the same with arrays, which is what he's using.
Unknown2007-11-20 12:20:19
It may or may not be true for "pure array" tables, as you say, but I found the original info on this page for anyone who's interested in the code. I personally don't use numerical array indices for my Lua tables often, instead preferring to use named hash keys for information lookups.
Unknown2007-11-20 12:35:53
QUOTE(Zarquan @ Nov 20 2007, 06:20 AM) 459416
It may or may not be true for "pure array" tables, as you say, but I found the original info on this page for anyone who's interested in the code. I personally don't use numerical array indices for my Lua tables often, instead preferring to use named hash keys for information lookups.


I am doing a little bit of each, so far. For anything I use internally and don't need to directly read or mess with much (like priorities) I am using numeric indices since (I assume) they are faster, and I am only using the table for ordering. For tables I use to actually store much data (afflictions, for example) I am using the hash table. In either case, that library will be useful. Thanks for the link!

Thanks for the example on the functions too. I never would have thought of actually using the local variable with function syntax; seems like a bizarre way to do things but it works! I've been too spoiled by strongly-typed languages where syntax like that makes the compiler explode.
Eldanien2007-12-31 06:56:02
Ignore this. Problem solved. I'm so in business.
Eldanien2007-12-31 19:36:26
So... I'm pseudocoding everything out and I realize a major flaw in my thinking. I'm accustomed to having my scripts run from a live application which holds state between script calls. MUSHclient stores variables, which I draw from within scripts, apply logic, store them back into MUSHclient before the script terminates.

I could probably manage to get Lua to read and write to a temp file to maintain information between script calls, but that's slow and clunky. What am I overlooking here? I'm still learning Lua, so please pardon me if this should be obvious. I'm coming from a very different mode of thinking, I think.

For example... getting smacked with tons of afflicts. Triggers get called, go through a cure cycle... there's still afflicts, but wouldn't the script end? Next time it fires, it won't have the remainder of the prior afflict table to work with.

Is there some workaround with timers that I can make use of, to keep Lua 'running' so to speak? Or do I have access to memory space that won't poof when the script terminates?
Unknown2007-12-31 19:50:20
QUOTE(Eldanien @ Dec 31 2007, 01:36 PM) 471731
So... I'm pseudocoding everything out and I realize a major flaw in my thinking. I'm accustomed to having my scripts run from a live application which holds state between script calls. MUSHclient stores variables, which I draw from within scripts, apply logic, store them back into MUSHclient before the script terminates.

I could probably manage to get Lua to read and write to a temp file to maintain information between script calls, but that's slow and clunky. What am I overlooking here? I'm still learning Lua, so please pardon me if this should be obvious. I'm coming from a very different mode of thinking, I think.

For example... getting smacked with tons of afflicts. Triggers get called, go through a cure cycle... there's still afflicts, but wouldn't the script end? Next time it fires, it won't have the remainder of the prior afflict table to work with.

Is there some workaround with timers that I can make use of, to keep Lua 'running' so to speak? Or do I have access to memory space that won't poof when the script terminates?


I may very well get corrected on this, because it's been a while since I coded my Lua system, but the objects you create in your MUSHClient session remain stateful, iirc. So, as long as your afflictions table is global in scope, it should exist in memory until you end your session. I used internally defined tables to track everything that I didn't need to "remember" from game session to game session.
Eldanien2007-12-31 20:03:59
Ah, I'm looking at Lua within LMTS, ignore the mud client entirely from consideration.

Does this hold true? If I create a global object, it remains in memory space until I explicitly set it to NIL or close the host?
Unknown2008-01-02 14:37:50
QUOTE(Eldanien @ Dec 31 2007, 09:03 PM) 471733
If I create a global object, it remains in memory space until I explicitly set it to NIL or close the host?

Yes.
Eldanien2008-01-02 18:11:44
Oh holy.
Eldanien2008-01-11 04:04:41
So, I've pared down the entirety of my Lua scriptfile to the following, to figure out a problem I'm having with triggers:

CODE
rex = require("rex_pcre")

function my_alias_handler( input )
    if input == "hello" then
        echo("Hello world!")
        return true
    end
end
mb.client_aliases = my_alias_handler

function trigger_handler( )
    for i = 1, mb.nr_of_lines do
        if mb.lines== "You are wielding a gnarled iron tahto staff in your hands." then
            hide_line(i)
        end
    end
end
mb.server_lines = trigger_handler


Checking inventory shows:

QUOTE
You are wielding a gnarled iron tahto staff in your hands.
You are holding:
stuff
You are wearing:
more stuff
You possess 103 items and are carrying no gold.
4923h, 4476m, 3954e, 10p, 20410en, 20410w elrxk-


My understanding is that this script should (among other things) gag the 'You are wielding a gnarled iron tahto staff in your hands.'

It does not.

Sending 'hello' functions as it should. I receive no errors while loading the script. What am I overlooking?

It's not that I want to gag that line, so much as I'm trying to get triggers to recognize in the first place. I put in an echo statement there and it never fired.
Unknown2008-01-11 14:00:55
I think you're trying to do too much in your trigger_handler function. Try this instead:

CODE
function trigger_handler()
  if mb.line == "You are wielding a gnarled iron tahto staff in your hands." then
    mb.gag_line = true
    mb.gag_ending = true
    return true
  end
  return false
end

mb.server_line = trigger_handler


You just need mb.line for the current line as this function is called for every line of text received.
You use mb.gag_line to tell it to gag the text you match.
You use mb.gag_ending to get rid of the CR/LF, separately from the text matched. This is needed because when you gag a prompt line, you don't have a line ending to gag.

Hope this works for you!
Unknown2008-01-11 16:10:40
You're coding for two different versions of ILua. Eldanien for the newer one and Zarquan for the older one.

Eldanien: Your code doesn't work because you're using the old ILua which ships with LMTS. Sorry I directed you to the documentation without thinking of this. What you need to do is either A) change to using Zarquan's syntax, for which there is not documentation that I know of, or B) make LMTS use the new ILua module or C) wait for someone else to get around to doing so.

I have been planning to myself but I never seem to get time off my other projects, when I manage to free up some free time..
Unknown2008-01-14 11:54:41
Noob question, how do I get the replacement prompt to echo the letters (h, m, e, etc) to echo the same color as the numerical values themselves? Since I -seriously- have -no- idea what I'm doing.
Unknown2008-01-14 13:40:03
Get rid of the C.x part of the string before each one. The "C" table is the color lookup table, and C.x refers to the ANSI default color (ESC
Unknown2008-01-14 18:14:14
QUOTE(CroX @ Jan 11 2008, 10:10 AM) 476051
You're coding for two different versions of ILua. Eldanien for the newer one and Zarquan for the older one.

Eldanien: Your code doesn't work because you're using the old ILua which ships with LMTS. Sorry I directed you to the documentation without thinking of this. What you need to do is either A) change to using Zarquan's syntax, for which there is not documentation that I know of, or cool.gif make LMTS use the new ILua module or C) wait for someone else to get around to doing so.

I have been planning to myself but I never seem to get time off my other projects, when I manage to free up some free time..


You can still get to the old documentation on the website. Go to the IMTS sourceforge site linked on the first page of this thread, and click 'old revisions' at the top. Go to the revision that says 'basics added' - everything documented there works in the LMTS version. Anything added later will not work.
Unknown2008-03-20 23:40:51
Ok... so I copied and pasted the information from the beginning of this thread as instructed and...

it doesn't work. The triggers.lua works, in so much as it will print the test-echo I put at the beginning of the file to be sure it's loading, and it loads the prompt.lua also as if I introduce errors it warns on them.

But it's not -doing- anything, as near as I can figure. I don't know if this is because

mb.server_prompt = prompt_handler

isn't doing anything, or what. If I add an echo anywhere in the prompt.lua it doesn't seem to do anything either.


Perhaps I'm just a total n00b. I think I might stick with ruby in kmuddy and just code myself from the ground up, but I thought figuring out the ILua thing would be nice.
Unknown2008-03-21 11:43:43
If you're using my LMTS 2, you'll need to adapt the scripts in this thread to use the new ILua conventions (as described on Whyte's documentation wiki at http://imts.sf.net). This code is for the older LMTS, which uses an older ILua, and the two systems are not compatible.
Unknown2008-03-21 19:04:00
Alrighty... I'll go about figuring out how to do that then, since I don't know any actual ILua yet. I'm planning on using the LMTS as my mapper regardless, and it seems kind of dumb to write a second proxy to handle atcp information... so I'll make this work if it hurts me. hehe. When I get it done, I'll likely post it somewhere so other folks don't run into what I did. Thanks for the help man.