当前位置:网站首页>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 ))
边栏推荐
- Pat class a 1028 list sorting
- PHP wechat red packet grabbing algorithm
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
- Huawei switches are configured with SSH login remote management switches
- 在浏览器输入url后执行什么
- haproxy+keepalived搭建01
- tslib库的移植
- Idea unreference Display Effect
- yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
- Pat class a 1030 travel plan
猜你喜欢

一条通往服务器所有端口的隧道

IP production stream is so close to me

Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico

LwIP learning socket (application)

Go language foundation ----- 13 ----- file

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

Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once

Go language foundation ----- 16 ----- goroutine, GPM model
![[cocos creator] Click the button to switch the interface](/img/b8/f0fd54a2a197cbfd788990e2806b52.png)
[cocos creator] Click the button to switch the interface

vcs import src < ros2. Repos failed
随机推荐
Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!
Generate video using clipout in viz engine
WorldView卫星遥感影像数据/米级分辨率遥感影像
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
[MySQL 13] if you change your password for the first time after installing mysql, you can skip MySQL password verification to log in
Idea dereference display effect
Pat class a 1030 travel plan
Huawei s5700 switch initialization and configuration SSH and telnet remote login methods
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
【LeetCode】2. Valid parentheses · valid parentheses
Redis配置文件
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
Wechat applet taro learning record
微软安全响应中心
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Worldview satellite remote sensing image data / meter resolution remote sensing image
register关键字
什麼是定義?什麼是聲明?它們有何區別?
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)