Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: 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.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: 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.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: 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!
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: 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:
Code:
#LUA {print("@A")}

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.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: 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.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net