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.

5
  • You need to look up mmToin in the returned table, not in the global one. Commented Mar 4, 2019 at 11:40
  • Can you write the snippet that do that? I searched in the returned table DynValue.Table but the length is 0 Commented Mar 4, 2019 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. Commented Mar 4, 2019 at 11:49
  • What do you mean by "is not working"? What exactly goes wrong? Commented Mar 4, 2019 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. Commented Mar 4, 2019 at 11:59

1 Answer 1

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>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.