0

I'm trying to implement lua scripting in my C#.NET program, but when I execute the code, one of the methods is not been executed.

Here is the class:

namespace Program1
{
    public class LuaFunctions
    {
        Client Client { get; set; }
        private Lua lua { get; set; }

        public LuaFunctions(Client c) {
            this.Client = c;
            this.lua = new Lua();

            registerFunctions();
        }

        public void ExecuteCode(string code)
        {
            this.lua.DoString(code);
        }

        private void registerFunctions()
        {
            lua.RegisterFunction("message", this, this.GetType().GetMethod("Message"));
            lua.RegisterFunction("pname", this, this.GetType().GetMethod("playername"));
        }

        public void Message(string s)
        {
            System.Windows.Forms.MessageBox.Show(s);
        }

        public string playername()
        {
            return Client.Player.Name;
        }
   }
}

When I execute this line of lua code "message(pname)" it does not even try to execute the method "playername()" to return some value, so it crashs in the DoString() line because "pname" is "returning" null.

Kyore
  • 388
  • 2
  • 7
  • 29
  • 3
    If you are trying to call a `pname` function then you need `message(pname())`. `message(pname)` is is going to use `pname` the *function* as the value to the `message` function. – Etan Reisner Nov 24 '14 at 01:28
  • You're right, I was trying to create like some "custom variables" to use, like $name, so I would be able to use like, if($name == "Kyore") then, so you know how is possible to do that? – Kyore Nov 24 '14 at 02:03
  • If you are trying to have the variable automatically update/etc. you'd have to see if LuaInterface supports that. If you just want to be able to set the variable then you just need to set a global or local/environment variable before calling the code that wants to use it. Instead of registering a function. – Etan Reisner Nov 24 '14 at 13:32

2 Answers2

0

pname is being registered as a function not set as a variable containing a string value.

When message(pname) is executed the message function is being given the value of the pname function instead of being given a string (or the result of calling the pname) function.

To pass the result of the pname function to message you would need to use message(pname()).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Thank you for the explanation, do you know how to create a variable instead a function to return a value? – Kyore Nov 24 '14 at 13:54
  • 1
    @Kyore I don't. There should be a way to create a global that should be easy enough to find though (I would think). That being said isn't LuaInterface "on hold" at the moment? The github page seems to suggest using `NLua` instead it seems. – Etan Reisner Nov 24 '14 at 13:58
0

It depends on where exactly you are trying to create the variable. If you are trying to set some variable in Lua, then lua["somevariable"] = "value"; would work. A more complete example could be:

Passing raw values to the state:

double val = 12.0;
lua["x"] = val; // Create a global value 'x' 
var res = lua.DoString ("return 10 + x*(5 + 2)")[0] as double;

Retrieving global values:

lua.DoString ("y = 10 + x*(5 + 2)");
var y = lua["y"] as double; // Retrieve the value of y
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184