|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Mon Jul 16, 2007 5:42 pm
Executing LUA scripts from the command line |
I decided to create a new thread for this topic since the other Lua topic was getting rather long.
I've been using the new version where pressing Ctrl-` toggles the "language" of the command line. So you toggle it into Lua mode, and now the command line is sent to Lua instead of zScript.
Of course, the problem with this is that you can't really use it when normally MUDding. You can't just type the text that you want to send to the MUD (you need to use the Lua send command for that). Also, it doesn't recognize aliases, or anything like that. It's actually not a very useful mode at all, and I'm constantly finding myself entering the wrong text when in the wrong mode.
So while this was an interesting test, I think I need some other way to execute Lua from the command line...keeping the command line in it's normal "smart" mode. Seems like the easiest thing to do is invent another line-starting character that tells the zScript command line parser to parse it using Lua instead of sending the command to the MUD. Unfortunately, as we all know, many MUDs use the various special characters for stuff like chat. And CMUD already has a lot of it's own "special characters". So I kind of hate to create yet another one.
Now, CMUD 2.0 *does* have a new "#LUA" command. So you can do this:
Code: |
#LUA {a=123;print a} |
for example. Maybe having that along with the Ctrl-` toggle is good enough? Maybe you won't be able to give any feedback until you have a chance to try it out, but I'm interested in other suggestions. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Jul 16, 2007 5:48 pm |
I think that should be plenty, but will zScript interact with the #lua command in any way, or will the argument always be interpreted as a string? So the only access to zScript variables through Lua will be with the zsMudWindow objects you've created and literal @whatever variables in the #lua command itself won't be expanded? This isn't a suggestion - it's absolutely fine if that doesn't happen, it might even work better that way - just a question.
Of course if this hadn't been included but someone decided they needed it, it wouldn't have been too hard to do something like assert(loadstring(zs.params())() in an alias and make their own. |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Mon Jul 16, 2007 5:54 pm |
Seems simple enough and addresses a concern I had.. how do you complex scripts on the command line. Even though you'd be gluttonn if they got to long at the command line.
|
|
_________________ Asati di tempari! |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Mon Jul 16, 2007 8:30 pm |
The #LUA command takes a string argument. As with all string arguments, you can use "" strings (where the @ is not expanded) or {} strings (where the @ *is* expanded). So you can do this:
Code: |
#VAR A "hello"
#LUA {print(~"@A~")} |
or this:
Code: |
#LUA %concat("print(~"",@A,"~")") |
or this:
Code: |
#LUA "print( zs.var.a)" |
In the first two cases, the @A is expanded by CMUD parser as the string argument to the #LUA command. Since the Lua "print" command requires quotes around string values, you have to play games with ~" to force it to put literal quotes into the string. So yes, it's going to be a bit of a pain to use the #LUA command in some cases because both Lua and CMUD use the " for string values.
The reason you *cannot* do this:
is that when CMUD parses this, it sees the "@A", which tells it to insert the literal string @A and strip the quotes. So Lua sees "print(@A)" which doesn't work at all.
You cannot do multi-line commands with this. If you want to do complex scripts on the command line, you need to press Ctrl-` to toggle the Lua command line mode. Once you are in Lua mode, then you can enter multiple lines as expected.
The #LUA command is really just a shortcut for the "#MSS {script} {Lua}" command. Even though Lua doesn't use MSS, I have tied the Lua implementation into the script engine so that you can work with Lua just like the other alternate scripting languages if you wish.
And yes Fang, your loadstring example is a good way to show how much control you have if you want to use it.
But this is exactly why I wanted to make it really easy to access CMUD variables from within Lua. The last example of "print(zs.var.a)" is a lot easier than any of the other methods, and a lot faster too. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Jul 16, 2007 10:07 pm |
The main reason I can see myself using this would be for testing, so it's nice that there's the option to send everything literally. The quotes thing isn't that bad since Lua has more than one option for quotes.
|
|
|
|
|
|