How can I get variable value from MoonSharp to C# unity?

I don’t know well MoonSharp. I wanna get value from MoonSharp script to C# script so I can use it in another field for myself.

eg.
C#

private int x;

string LuaString = @"
            function GetNumber()
                numA = 5
                numB = 4
            end                   
        ";

        _inputField.text = LuaString;

        _script.Globals["numA"] = x;
        DynValue dynValue = _script.DoString(LuaString);

Well, your lua code does nothing beside defining a new function named GetNumber (which actually doesn’t return anything so the name is kinda misleading). However you never actually called that method anywhere. So you defined that method in the global table of your environment and you’ve done nothing with it.

Here’s an example:

        string LuaString = @"
         function GetNumber()
             numA = 42
             numB = 2
         end                   
        ";
        _script.Globals["numA"] = 4;
        _script.DoString(LuaString);

        // get the GetNumber value from the global table. It's a lua closure since it's a method
        var func = (Closure)_script.Globals["GetNumber"];

        // print the value of the global "numA" which is a double value
        Debug.Log("numA:" + (double)_script.Globals["numA"]);

        // actually call that GetNumber method
        func.Call();

        // print the value of "numA" again
        Debug.Log("numA:" + (double)_script.Globals["numA"]);
        Debug.Log("numB:" + (double)_script.Globals["numB"]);

This should give you some insight.

However something like this would probably make more sense:

        string LuaString = @"
         
         function GetNumber(val)
            return numA * val
         end                   
        ";
        _script.DoString(LuaString);
        _script.Globals["numA"] = 4;

        var func = (Closure)_script.Globals["GetNumber"];

        var res = func.Call(42).CastToNumber();

        Debug.Log("result:" + (double)res);

Notice a few things here:

  • We again create a method called “GetNumber”. This time it has an argument “val” and also uses a global variable “numA”.
  • The global variable does not exist when the method is defined. We set the value after the lua code has been run / parsed.
  • When we call the method we have to provide the argument, in this case the number “42”.
  • As a result we get the value 168 (which is 42 * 4)

It’s not clear what you actually want to achieve. However you have to realise that the lua environment is a seperate environment. You actually put C# methods into a lua environment. For example imagine you have this C# method:

double DoSomething(double a, double b)
{
    return a + b * 2;
}

You can do this:

        string LuaString = @"
         
         function GetNumber(val)
            return numA * val + cSharpFunc(numA, 3)
         end                   
        ";
        _script.DoString(LuaString);
        _script.Globals["numA"] = 4;
        _script.Globals["cSharpFunc"] = (System.Func<double,double,double>)DoSomething;

        var func = (Closure)_script.Globals["GetNumber"];

        var res = func.Call(5).CastToNumber();

        Debug.Log("result:" + (double)res);

This will output the value “30”. The final equation would be

4 * 5 + 4 + 3 * 2 == 30

The last 4 + 3 * 2 would be calculated by our C# method.

Those examples should cover at least the basics. You have to get familiar with how lua works, what an environment table actually is, what a DynValue or Tuple is and how this all works together.

Thanks :slight_smile: