当前位置:网站首页>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 ))
边栏推荐
猜你喜欢

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

Go language foundation ------ 12 ------ JSON

WPF:解决MaterialDesign:DialogHost 无法关闭问题

Pat grade a 1029 median

Go language foundation ------ 14 ------ gotest

the installer has encountered an unexpected error installing this package

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

Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire

Ventuz Foundation Series "one step at the door"

多旅行商问题——公式和求解过程概述
随机推荐
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
链式长取值
EtherCAT state machine transition (ESM)
PHP wechat red packet grabbing algorithm
【LeetCode】4. Best time to buy and sell stock
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
Pat class a 1030 travel plan
What to do after the browser enters the URL
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Generate video using clipout in viz engine
yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Worldview satellite remote sensing image data / meter resolution remote sensing image
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
超限黑客认知
Free use until 2015 -- viz artist multi touch plug-in package
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
Pat grade a 1027 colors in Mars
vcs import src < ros2. Repos failed