当前位置:网站首页>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 ))
边栏推荐
- [USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
- VMware virtual machine configuration static IP
- Clip Related Script
- [MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
- 一条通往服务器所有端口的隧道
- C language learning notes (mind map)
- 华为交换机:配置telnet和ssh、web访问
- Install cross compiler arm none liunx gnueabihf
- PHP common sorting algorithm
- JS common basic case sorting (continuous update)
猜你喜欢
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
Viz artist advanced script video tutorial -- stringmap use and vertex operation
Pat class a 1028 list sorting
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
I want to do large screen data visualization application feature analysis
Zohocrm deluge function application time verification
EtherCAT state machine transition (ESM)
在浏览器输入url后执行什么
HDMI2.1与HDMI2.0的区别以及转换PD信号。
随机推荐
优质博客——
LwIP learning socket (API)
Idea dereference display effect
【LeetCode】2. Valid parentheses · valid parentheses
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
一条通往服务器所有端口的隧道
Idea unreference Display Effect
YOLO系列 --- xml2txt脚本
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Structure of golang
Docker installs MySQL and successfully uses Navicat connection
register关键字
idea取消引用顯示效果
Differences between tp3.2 and tp5.0
【cocos creator】获取资源uuid
What to do after the browser enters the URL
E: 无法定位软件包 ros-melodic-desktop-full
PHP微信抢红包的算法
Wechat applet taro learning record