2

I have this lua script inside "Conversion.lua" file:

local conversion = {}

function conversion.mmToin( value )
    return value * 0.0393701
end

return conversion

I need to use the function mmToin in C# code, contained inside the object conversion.

If the function would not be inside the object i would have use the following code:

Script scp = new Script();
scp.DoFile(GlobalConst.PATH_TO_SCRIPT_FOLDER + "Conversion.lua");
double resultFm = scp.Call(scp.Globals["mmToin"], 1).ToObject<double>();

but i can't use the function if i put it inside an object; i've tried:

double resultFm = scp.Call(scp.Globals["conversion.mmToin"], 1).ToObject<double>();

but is not working.

How can i use mmToin function inside C# code? Thanks.

S.Orioli
  • 443
  • 6
  • 21
  • You need to look up `mmToin` in the returned table, not in the global one. – tkausl Mar 04 '19 at 11:40
  • Can you write the snippet that do that? I searched in the returned table `DynValue.Table` but the length is 0 – S.Orioli Mar 04 '19 at 11:47
  • I don't really know how to do it in C# unfortunately, I'm used to using C to interact with Lua. However the Lua internals should work the same, so the `DoFile` should leave a table on the stack (the returnvalue) which has a key `mmToin` with the value of the function. – tkausl Mar 04 '19 at 11:49
  • What do you mean by "is not working"? What exactly goes wrong? – canton7 Mar 04 '19 at 11:52
  • Well exception is fired with message `function is not a function and has no __call metamethod`. I suppose because function is not found. – S.Orioli Mar 04 '19 at 11:59

1 Answers1

1

You need to keep a reference to the returned value of your lua script as a DynValue object, then look for the desired function in the Table property.

DynValue dyn = scp.DoFile(GlobalConst.PATH_TO_SCRIPT_FOLDER + "Conversion.lua");

and you should be then able to call your function with

scp.Call(dyn.Table.Get("yourFunctionHere"), parameters).ToObject<double>();
SirePi
  • 242
  • 1
  • 3
  • 11