当前位置:网站首页>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 ))
边栏推荐
- C2 several methods of merging VCF files
- 华为S5700交换机初始化和配置SSH和TELNET远程登录方法
- [at] abc 258G - Triangle 三元组可达-暴力
- Redis view client connection
- 一个实习生的CnosDB之旅
- Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
- Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
- Uniapp learning records
- Usage of (case, when) in PostgreSQL
- 【cocos creator】点击按钮切换界面
猜你喜欢

方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!

Research shows that breast cancer cells are more likely to enter the blood when patients sleep

Screenshot tool snipaste

Iterm2设置

Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico

Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select

STM32F103 SPI (pit Diary)

haproxy+keepalived集群搭建02

Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation

An article for you to understand - Manchester code
随机推荐
【LeetCode】4. Best time to buy and sell stock
Precautions for opensips and TLS SIP trunk docking
Go language foundation ----- 08 ----- interface
Huawei switch basic configuration (telnet/ssh login)
PHP common sorting algorithm
Pat class a 1028 list sorting
C language learning notes (mind map)
freetype库的移植
IP production stream is so close to me
MAE
How does yarn link help developers debug NPM packages?
C2-关于VCF文件合并的几种方法
vcs import src < ros2. Repos failed
When did you find out that youth was over
【踩坑系列】mysql 修改root密码失败
tp3.2和tp5.0的区别
haproxy+keepalived搭建01
Generate video using clipout in viz engine
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03