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.
pname
is being registered as a function not set as a variable containing a string value.When
message(pname)
is executed themessage
function is being given the value of thepname
function instead of being given a string (or the result of calling thepname
) function.To pass the result of the
pname
function tomessage
you would need to usemessage(pname())
.