当前位置:网站首页>Mutual call between Lua and C #
Mutual call between Lua and C #
2022-07-03 07:57:00 【qq_ two billion three hundred and eighty-five million seven hun】
C# And Lua The interaction of is mainly by Lua Officially provided LuaInterface.dll Library implementation of .
What is? LuaInterface
LuaInterface Including two dynamic link library files , One is luaInterface.dll, stay C# In the operation Lua One of the necessary dependent files is Luanet.dll,
C# And lua Interaction principle :
The address of the original blog is as follows :
C# And Lua The interaction process :
C# Call Lua : from C# File calls Lua Parser bottom layer Luanet.dll library ( from C Language writing ), Again by dll File execution corresponding Lua file ;
C# And Lua Interaction principle :
C# And Lua Interaction is mainly realized through virtual stack , The index of the stack is divided into positive and negative numbers , If the index is positive , be 1 Indicates the bottom of the stack , If the index is negative , be -1 Represents the top of the stack .
Lua Call C#: Mr Into C# Corresponding to the source file Wrap File or write C# Corresponding to the source file c modular , Then pass the contents of the source file Wrap Documents or C Module registered to Lua Interpreter , Then from Lua To call the function of this module .
Explain from the code file :
lua call CSharp The process :
lua->wrap->C#
Mr Into Wrap file ( Intermediate document / Adaptation file ),wrap File field method , Sign up to lua In the virtual machine ( Interpreter luajit), then lua adopt wrap It can be adjusted C# 了
Or in config You can also add the corresponding type to the file
CSharp call Lua The process :
C# Generate Bridge file ,Bridge transfer dll file (dll Yes, it is C The library of writing ), First call lua in dll file , from dll File execution lua Code
C#->Bridge->dll->Lua OR C#->dll->Lua
Explain from memory : To put it bluntly, it is to operate the stack
C# Call Lua:C# Put the request or data on the top of the stack , then lua Take the data from the top of the stack , stay lua Make corresponding treatment in ( Inquire about , change ), Then put the processing result back to the top of the stack , Last C# Then take it out from the top of the stack lua Processed data , To complete the interaction .
After creating a new project, put the LUAInterface.dll and luanet.dll Drag in C# In the new project , Add... To the reference luainterface References to
stay C# Execute access in Lua Code
Lua lua = new Lua(); // establish Lua Parser
lua["num"]=2; // Define a num
lua["str"]="a string"; // Define a string
lua.newTable("tab"); // Create a table tab={}
obtain lua Variables in environment
doublenum = (double)lua["num"];
string str = (string)lua["str"];
stay C# In the implementation of Lua Script files , Or script string
lua.DoFile("script.lua");// perform script.lua Script . The script should be consistent with C# The project is under the same directory ,lua The code format is ansi Format ; The folder should be placed C# Under the project of debu In the folder
lua.DoString("num=2");
lua.DoString("str='a string'");
object[] retVals = lua.DoString("return num,str");
In hot update , Just write an analysis lua Script code , then c# The code doesn't need to change , It only needs modify lua The script is good , adopt lua Script controls game logic .
stay CLR One of them C# Method register into Lua
// Register ordinary methods in a class
The first parameter is in l After the conversion lua Method name in , The second is C# Class name in , The third is C# Method name in
Lua lua = new Lua();
Program p=new Program();
lua.RegisterFunction("LuaMethod", p, p.GetType().GetMethod("CLRMethod"));
lua.DoString("LuaMethod()");
Console.ReadKey();
public void CLRMethod()
{
Console.WriteLine(" hold C# Method is registered LUA");
}
// Register the static method of a class
Lua lua = new Lua();
lua.RegisterFunction("CToLua", null, typeof(Program).GetMethod("MyStaticMethod"));
lua.DoString("CToLua()");
Console.ReadKey();
public static void MyStaticMethod()
{
Console.WriteLine(" hold C# The static method of lua");
}
stay Lua Use in editor c# Class in
require“luanet" Import and stock in , You can use the methods in the Library ,
-- load CLR The type of 、 Instantiation CLR object
Lua The code in ,lua call C# Class luainterface.exe luanet.exe Import project's debug Under the folder
require "luanet"
luanet.load_assembly("System")// Load assembly
num=luanet.import_type("System.Int32")// Load type
print(num) C# The code in ,C# call lua Method output ;
Lua lua = new Lua();
lua.DoFile("LuaUseScript.lua");
Console.ReadKey();
stay Lua Use in C# When creating objects for classes in , It will automatically match the most appropriate construction method
stay Lua Medium visit C# Properties and methods in
Lua In the code , visit C# The way and access of the properties of the object table The key index is the same as , such as obj.name perhaps obj["name"]
Lua In the code , visit C# Methods and calls of ordinary functions of objects table The function is the same as , such as obj:method1()
luanet.load_assembly("lelelua")
Program=luanet.import_type("lelelua.Program")
program1=Program()
print(program1.name)
program1:TestMethod() public string name = " Lele loves learning ";
public void TestMethod()
{
Console.WriteLine("C# The method in ");
}stay Lua Medium visit C# The method in - A special case , Notice that there are two return values
luanet.load_assembly("lelelua")
Program=luanet.import_type("lelelua.Program")
program1=Program()
void,strlength=program1:TestOut("leleaixuexi")
print(strlength) public void TestOut(string name, out int count)
{
count = name.Length;
}When there is out or ref When parameters are ,out Parameters and ref Parameters are returned with the return value of the function , And when called ,out Parameters don't need to be passed in
ref Parameter needs to be passed in , Return to one table There are two values (value1,value2)
When there are overloaded functions , The calling function will automatically match the first function that can be matched
have access to get_method_bysig Function to get C# Function usage of the specified parameter of the specified class in
luaMethod= get_method_bysig(Obj,"CSharpMethod","System.String")
luaMethod("siki")
stay Lua Register in C# Event delegation in (event delegate)
stay Lua Pass through Add Methods or Remove How to put a Lua Function registration or logout from C# Event delegation in
function method()
end
obj.SomeEvent:Add(methodname( No quotes ))
边栏推荐
猜你喜欢

EtherCAT state machine transition (ESM)

Go language foundation ----- 09 ----- exception handling (error, panic, recover)

Go language foundation ----- 15 ----- reflection

Install cross compiler arm none liunx gnueabihf

My touch screen production "brief history" 2

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

Getting started with minicom

Free use until 2015 -- viz artist multi touch plug-in package

【LeetCode】2. Valid parentheses · valid parentheses

haproxy+keepalived集群搭建02
随机推荐
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
oracle 插入单引号
Go language foundation ------ 14 ------ gotest
什么是定义?什么是声明?它们有何区别?
Getting started with minicom
WPF:解决MaterialDesign:DialogHost 无法关闭问题
Screenshot tool snipaste
VMware virtual machine configuration static IP
使用 FileChannel 进行文件的复制拷贝
tslib库的移植
PHP常用排序算法
vcs import src < ros2. Repos failed
WorldView卫星遥感影像数据/米级分辨率遥感影像
Quality blog——
Register keyword
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
*p++、*++p、++*p、(*p)++
Redis批量启停脚本
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select