Question

I have some background lua codes and I load them on each startup, They work fine, But Is there any way I can convert them to C#?

Here Is my meta table codes:

player = {}
player.__index = player

function player:Add(name)
    return setmetatable({name = name}, player)  
end

function player:Nick()
    return self.name
end

It works fine, Here Is how currently my codes work:

print(ply:Nick())

Currently I search a Player.cs object using a void at my registering lua functions code, I don't have any special void or function on my C# code that has Player object on It, But I want to change all of them to Player object, Also Is there any way I can use the Player.cs voids/strings or anything from lua without registerting them? Player.cs objects are non-static.

Était-ce utile?

La solution

I found out that KopiLua and LuaInterface (Latest) Has metatables, All you have to return Is your object, You can not return classes or anyhow add them to LuaInterface, but you can return objects or functions that can do the same. from lua, peoples can use object functions. Functions that has an argument (Even empty) Are used with a :, and others with a dot. Here's the example:

// C#:
public List<string> testing()
{
   return new List<string>()
   {
      {"Test"},
      {"Another test"}
   };
}

// Player object:
public int Health { get { return 1; } }
public int Hp() { return this.Health; }

// lua:
testing:Clear()
print(ply.Health)
print(ply:Hp())
ply.Health = 2
// You can not change a function style value like Hp(), So no way to change health this way except making a set function

Autres conseils

I'm not sure to understand exactly what you need but let me try. I would set an identifier to my LUA classes (like 'csid' = "Player") which I could use from the 'LuaTable' object retrieved in C# to identify the object to instantiate. Then you could have either your C# classes have a factory method taking a 'LuaTable' to build an instance from and a method on the instance allowing to get a 'LuaTable' from it. Or have a LUA wrapper class for each of your C# classes with 'ToLUA' and 'FromLUA' methods to convert both ways.

Whenever some C# code is called with a 'LuaTable' you would check if it has 'csid' member and if so either use a switch statement to choose your class or use Reflection with the retrieved name to build your C# object. And whenever you'd need to pass a C# object to LUA you would use the opposite conversion method the same way.

The totally generic but really not ideal solution would be to generate a dll from C# when you read everything from LUA and then load it. But that will be useful only if your C# code is completely generic and doesn't have to know that LUA is using 'Players' or 'Cars' or whatsoever.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top