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
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Sat Oct 11, 2008 5:20 pm   

[2.36] Calling function (lua) from zscript trigger
 
I'm sure this is just me doing something silly, but I couldn't seem to figure it out.

I have a simple function called 'log' that takes two parameters (classname and message). It is set to use LUA, and does a simple 'print' of the two variables with various fluffy formatting.

I have a separate zScript trigger that calls @log("myClass", "myMessage").

This fails. Note that I've tried removing all parameters, and just calling a plain lua function that does nothing more that print("boo") with the same effect.

However, if I change the @log function to be zScript as well (and obviously change print to #echo) everything works as you'd expect. What piece of obviousness am I missing?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sat Oct 11, 2008 6:18 pm   
 
Show us the exact code you're using, both the code in the @log function and the code that calls it. Use the XML.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Sat Oct 11, 2008 10:19 pm   
 
It is as simple as described :) Something interesting I discovered: the below worked fine _until_ I tried adding parameters to the function. As soon as I did that, attempting to call @testlog("param1") would _send_ (not echo) 'nil' to the mud. Removing the parameters from the function back to the code below leaves it in this broken state, and I'm not sure how to 'unstick' it.

As before, changing @testlog back to zScript (and going from print to #echo) fixes things and it works.

Code:

<class name="Testing" initenable="true" id="15398">
  <func name="testlog" language="Lua" id="15399">
    <value>print("boo")
</value>
  </func>
  <alias name="run_testlog" parsearg="false" id="15400">
    <value>@testlog()</value>
  </alias>
</class>
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sun Oct 12, 2008 8:50 am   
 
I need to see the exact code you're using, not a mockup. It could be a typo or something. The key problem, though, is that it breaks when your function has parameters and that function doesn't have parameters, so I think we're agreed that that function will work fine. I need to see your Lua code for the @log() function.

However, the reason it's sending nil to the MUD is because you haven't told it not to. By default, aliases send returns from functions to the MUD - the print function returns nil, so the @testlog function will also return nil. That's then sent to the MUD. The way you call a function without it sending anything is by doing #call @testlog().
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Sun Oct 12, 2008 4:40 pm   
 
The pasted code is the actual broken function. I stripped it down to the most basic 'broken' state I could just to eliminate silly things like typos. Thanks for pointing out the #call though... I somehow was totally overlooking that, and that resolved another issue I was having.

I'll keep tinkering... I'm sure it is something I have set up screwy on my end, that may not show up in the code itself.
Reply with quote
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Sun Oct 12, 2008 5:10 pm   
 
Sure enough, stupidity on my part as expected. I had "Show information messages" unchecked under Options->General->Scripting->General Scripting. Combined with not using #call, I was led down the wrong path.

So now I have painful basics workings, but I still have a question for you in using the LUA integration - what is the appropriate way to call a function from a lua alias?

Using the really basic example in this thread, assume a LUA alias called 'run_testlog', and a lua function called 'testlog'. From the docs, it appears I should be able to do:
zs.testlog() from within the run_testlog alias to call testlog. This doesn't work... (#call @testlog() work fine when set to zScript, of course).
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sun Oct 12, 2008 5:49 pm   
 
If it's a lua function (as in, described using lua's function...end syntax) it'll be exactly where you put it with the function...end. If you did function run_testlog, it'll be run_testlog() you need to call; if you put it in a table, it'll be in there.

If your function is a CMUD function containing Lua code, then I'm actually not certain how you'd go about doing that. But if it were me, I'd define all functions that were going to use Lua code as Lua functions using the function...end syntax. If I needed to call those functions from zScript for some reason, I'd have the CMUD function just call the Lua function I'd created.

So I'd have an OnConnect event doing "function test (msg) print(msg) end" to create the Lua function and then a CMUD function test doing "test(zs.param(1))". That way if my alias is using Lua code I can do test("whatever") and if it's using zScript code I can do #call @test("whatever").
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Sun Oct 12, 2008 5:57 pm   
 
Woo, thank you. That was exactly what I was missing - the concept of creating actual LUA functions (function..end) vs a CMUD function containing LUA code (which is what I couldn't figure out how to call correctly). I didn't understand where to 'create' them, but sticking them in something like OnConnect makes sense. Should you continue your LUA tutorial, maybe a section on this would be useful (assuming it isn't documented already and I'm overlooking it). For some reason this was difficult to wrap my head around until you laid it out specifically. Thanks again.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sun Oct 12, 2008 9:53 pm   
 
It's not covered elsewhere, and that'll definitely be one of the things I'll be writing about when I get round to finishing the Lua tutorial. No telling when that will be though, I've been too busy and don't see myself being any less so for a while :(

Incidentally, I just did some rummaging around in the Lua documentation and the proper way to call a CMUD function from Lua is with zs.func.funcname() - but I still like the method I described above better.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Daagar
Magician


Joined: 25 Oct 2000
Posts: 461
Location: USA

PostPosted: Mon Oct 13, 2008 1:21 am   
 
I had tried zs.func.funcname() without success... but considering the other silly issues I was having I don't put much stock in it. Your way did seem clean, and would make a clean way to build a library of functions.
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