Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Luainterface, how to pass a whole class from a c# code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 arbuzzo   Members   -  Reputation: 101

Like
0Likes
Like

Posted 02 September 2012 - 11:38 PM

I want to pass whole class from c# code to lua, so I can create in LUA a new object and use its methods, fields and so. When it's done, I'd like to know if it's possible to use objects in lua, which were created in a c# code and then passed somehow to lua.
Here's my code:
atm, its not possible to create in my luainterface object of class Person1 and then use its function
when I enter say() into lua scripter (without creating any object there), I'm getting output of Person2.say()

using System;
using LuaInterface;
using System.Reflection;
namespace ConsoleApplication
{

public class Person1
{
public void say()
{
Console.WriteLine("person1 says: hehe");
}
}

public class Person2
{
public void say()
{
Console.WriteLine("person2 says: hihi");
}
}
class Class1
{
static void Main(string[] args)
{
Lua lua_compiler = new Lua();
Person1 person1 = new Person1();
Person2 person2 = new Person2();
lua_compiler.RegisterFunction("say", person1, person1.GetType().GetMethod("say"));
lua_compiler.RegisterFunction("say", person2, person2.GetType().GetMethod("say"));

while (true)
{
string line = Console.ReadLine();
try { lua_compiler.DoString(line); }
catch { }
}
}
}
}



Ad:

#2 6677   Members   -  Reputation: 1050

Like
0Likes
Like

Posted 03 September 2012 - 08:23 AM

Might it be an issue with global vairables on lua. Also lua doesn't have built in support for objects, it isn't an object orientated language. People have managed to simulate it with metatables though, I don't know enough lua to help you further.

#3 Hodgman   Moderators   -  Reputation: 13533

Like
0Likes
Like

Posted 03 September 2012 - 09:33 AM

with your RegisterFunction calls, you're setting a Lua global variable called "say" (or _G["say"]), with a function that calls person1.say(). You're then overwriting that same global variable with a function that calls person2.say().

The documentation contains some example Lua code like below, for creating C# objects from Lua:
load_assembly("System.Windows.Forms")
Form = import_type("System.Windows.Forms.Form")
form1 = Form()





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS