|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Dec 21, 2007 3:40 pm
[2.18] Using %i in Lua |
So I was experimenting and seeing how well CMUD converts tables into string lists - the answer, it seems is very well (or perhaps this is a result of the conversion to hash tables, so the command just uses the hash). Excellent stuff. There's a problem, however.
Run this Lua code from the command line:
zs.forall({"hello","goodbye"},"#say lol! %i!") |
And you'll notice that the string "%i" is being parsed out properly by CMUD's parser but isn't being given a value. This isn't a problem with the table syntax, because this slightly more zScript-friendly format also doesn't work:
zs.forall("hello|goodbye","#say lol! %i!") |
So I can only conclude that something more general is going on. Normally I wouldn't post something like this (why would you ever call forall from lua?) but it might be an example of something more serious. |
|
|
|
rck Novice
Joined: 26 Oct 2005 Posts: 32 Location: California
|
Posted: Fri Dec 21, 2007 9:54 pm |
It still loops for every iteration that would normally happen, so I assume it's just a bug.
Definitely a bug. |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Mon Jan 07, 2008 10:06 pm |
I think the problem is that Lua is sending the zs.forall command a literal string value. In other words, %i is within the literal string and doesn't get compiled by CMUD into a proper zScript reference. Since Lua doesn't have the concept of a literal string vs an "expanded" string, I'm not sure exactly what to suggest.
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Jan 07, 2008 11:00 pm |
Ah, I see. It's weird that the forall parses out the %i but at the same time doesn't give it a proper value. I guess I assumed that the %i would either be left alone or filled in. If you did something similar in zScript, like #say "lol! %i!" then the %i will be literally printed, and Lua seems to be doing something halfway between the two, parsing out the %i but not filling its value. I suppose it's the #say command that's causing the %i to vanish - running zs.forall({"hello","goodbye"},"#say \"lol! %i!\"") from Lua has the same effect as the zScript example I just gave.
Like I said at the beginning, though, it's not a big deal. |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Mon Jan 07, 2008 11:06 pm |
Also other variables are also expanded %names %windowid, whatever they all get expanded correctly, it seems its just the loop-centric variables that don't function %i %key %val using #loop #forall #loopdb
|
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Mon Jan 07, 2008 11:59 pm |
If other variables are getting expanded then this is certainly a bug. I've added it to the bug list.
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Feb 05, 2009 7:53 pm |
This was a tricky one.
In CMUD, the various Loop commands (#LOOP, #FORALL, #LOOPDB, etc) are executed within the compiled code with the loop variable placed on the stack. When a loop is executed via a CMUD Command API call (like with zs.cmd.forall, or just zs.forall, or via the CMUD COM API), then the commands to be executed within the loop are compiled and executed separately, and CMUD actually calls some zMUD compatibility code to execute the loops without being compiled together with the command block.
In the zMUD compatibility code, the loop variable, like %i, is stored as a system global, like any other %sys variable. Same with %key and %val. So I have changed CMUD so that if you reference %i, %key, %val, etc outside of the normal stack frame reference (like called from Lua or COM), then CMUD will expand the system variable rather than just returning a null value.
This should fix the issue of calling these various looping commands from a script or COM API without effecting the normal operation of compiled CMUD scripts.
Just remember that using this old style loop, the %i will always point to the inner-most loop. So, in a really complex case like this:
Code: |
zs.forall("hello|goodbye","#loop 2 {#say lol! %i,%j}") |
you get the result:
Code: |
lol! 1,hello
lol! 2,hello
lol! 1,goodbye
lol! 2,goodbye |
where %i gives the index of the inner #loop and %j gives the index of the outer zs.forall. Whereas the normal zScript code:
Code: |
#FORALL "hello|goodbye" {#loop 2 {#say lol! %i,%j}} |
gives the proper result of
Code: |
lol! hello,1
lol! hello,2
lol! goodbye,1
lol! goodbye,2 |
Nothing I can do about this difference...it's just a result of trying to call uncompiled zScript from Lua.
So while simple stuff will work, avoid doing really weird stuff like this. If you are using Lua, just use the built-in Lua loops and conditionals without trying to call the zScript loops. You'll avoid weird problems like this, and your script will be faster using the native Lua loops. |
|
|
|
|
|
|
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
|
|