View Full Version : Can NetRemote send LUA code to Girder?
June 11th, 2007, 06:08 AM
I am looking for a solution to call a lua function in Girder, but from a NetRemote client. What I need is to handover the function call including parameters. The function itself is running in Girder.
So for example Girder has loaded a luascript that offers the function "switch_light_on(parameters)" and I want to call it directly from NetRemote without using the eventmanagement etc.
Is it possible?
June 11th, 2007, 09:44 AM
While the technique I'm about to describe DOES use the event manager, hopefully it accomplishes what you're looking for:
1. Create the script that you want to execute in Girder
2. Set up a raw communication server event with a convenient name ("NR.Test", for example) to trigger the script you just created.
3. From NetRemote, make the following call:
NetRemote.ExecuteAction( -1, 0, 1, "NR.Test("..param1.."|"..param2..")" );
4. At the beginning of your Girder function which will be triggered by the "NR.Test" event, insert the following code to extract the passed parameters:
local p1, p2;
-- Parse the payload into two strings
_, _, p1, p2 = string.find( pld3, "(%w+)%|(.+)" );
You have now set up a mechanism to trigger a Girder function from NetRemote, passing two parameters. The pld3 variable is a global Girder variable which contains the raw text of what was passed from NetRemote. After step 4 above, the p1 and p2 variables will contain param1 and param2 from NetRemote (note that these are now strings, so you might use tonumber() if you need to do numeric comparisons with them). It is possible to scale this parameter passing approach to handle more parameters, or to pass a table instead.
June 11th, 2007, 09:59 AM
I use the same method described however I use the following to parse the parameters
local parms = string.Split(pld3, '|')
this way you can pass in varying number of parms.
June 11th, 2007, 11:41 AM
Good point! Assuming you've added a string.split function, that's a more elegant approach :). You can find a number of string.split examples with a Google search (but I don't think it's part of the base LUA distribution, at least as far as I know).
June 11th, 2007, 01:22 PM
It was recently added by Mike, I use it quite frequently.
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.