简体   繁体   English

如何使用LuaInterface从VB.NET调用Lua函数

[英]How to call a Lua function from VB.NET using LuaInterface

I have been attempting to figure out how to call a Lua function from a 'pre-compiled' LuaFunction. 我一直试图找出如何从“预编译”的LuaFunction调用Lua函数。 The general idea is that I want to call a Lua function from C#. 一般的想法是我想从C#调用Lua函数。 I am able to do it the other way around by using Lua.RegisterFunction() however just fine. 我可以通过使用Lua.RegisterFunction()来实现另一种方式,但是就可以了。

What ends up happening is that the callback returns null, aka the actual function does not seem to be read or exists when using 'Lua.LoadFile()'. 最终发生的事情是该回调返回null,也就是使用'Lua.LoadFile()'时似乎未读取或存在实际函数。 I am not exactly sure how to set this up though yet. 我还不确定如何设置它。

So to initialize the Lua script in VB.net, I use a class with the following: 因此,要在VB.net中初始化Lua脚本,我将使用以下类:

Private state As Lua
Private luafunction As LuaFunction

Public Sub New(ByVal playerInstance As PlayerClass)
        Me.playerInstance = playerInstance
        state = New Lua()
        state.RegisterFunction("PrivateMessage", Me, Me.GetType().GetMethod("PrivateMessage"))
        state("playerID") = Me.playerInstance.Index
        luafunction = state.LoadFile(SkillsFolder & "script.lua")
End Sub

luafunction being the pre-compiled script. luafunction是预编译的脚本。 I want to use LoadFile because it is being called more then once, and LoadFile over DoFile is more efficient. 我想使用LoadFile,因为它被调用的次数超过一次,并且通过DoFile加载文件更加有效。

I have the calling function here: 我在这里有调用函数:

Public Function ProcessAttack() As Boolean
        Dim luaFunc As LuaFunction = state.GetFunction("AttackProcess")

        If luaFunc IsNot Nothing Then
            Dim processed As Object = luaFunc.Call()
            Return processed(0)
        End If
    Return False
End Function

Obviously luaFunc is nothing/null. 显然luaFunc为空。 Doesn't actually work here. 实际上在这里不起作用。 The LUA script is as follows: LUA脚本如下:

function AttackProcess()
   PrivateMessage(playerID, "Test!")
   return true
end

It's a very basic script. 这是一个非常基本的脚本。 The idea is that when the player 'attacks' it should call the Lua script first to see if the function exists and can be handled in Lua, otherwise C# will handle it in a default method of it's choosing. 这个想法是,当玩家“攻击”时,它应该首先调用Lua脚本,以查看该函数是否存在并且可以在Lua中进行处理,否则C#将以其选择的默认方法对其进行处理。 (So if handled, AttackProcess() will return true, and then the C# coding the handles the basic attacking will not be called.) (因此,如果进行处理,AttackProcess()将返回true,然后将不调用对基本攻击进行处理的C#编码。)

I have a feeling that Dim luaFunc As LuaFunction = state.GetFunction("AttackProcess") is pretty much trying to search through nothing, I might need to use the variable that is precompiled, which is 'luafunction' to pull it, but I have been stomped on exactly how to get Lua to be called by C#. 我感觉Dim luaFunc As LuaFunction = state.GetFunction("AttackProcess")几乎什么都不想要,我可能需要使用预编译的变量,即'luafunction'来拉取它,但是我有关于如何使Lua由C#调用的问题,我们一无所知。

Thank you. 谢谢。

I was able to solve this by calling 'DoFile' instead of LoadFile. 我能够通过调用“ DoFile”而不是LoadFile来解决此问题。 Apparently the function itself needs to be loaded before you attempt to pull it into a LuaFunction. 显然,在尝试将其拉入LuaFunction之前,需要先加载函数本身。

For exmaple 例如

If File.Exists(SkillsFolder & "Main\" & playerInstance.ClassName & ".lua") Then
    state.DoFile(SkillsFolder & "Main\" & playerInstance.ClassName & ".lua")
    Dim luaFunc As LuaFunction = state.GetFunction("attack")
    If luaFunc IsNot Nothing Then
        luaAttack = luaFunc
    End If
End If

Now works, and luaAttack ends up with the proper function. 现在可以正常工作,并且luaAttack最终具有正确的功能。 You can also use LoadFile, then Call() once, and grab the functions that way as well. 您还可以使用LoadFile,然后使用一次Call(),并以这种方式获取函数。 The important thing here is that the coding is at least executed ONCE to be able to grab created functions. 这里重要的是,至少要执行一次编码才能获取创建的功能。 Loading it only will not work. 仅加载它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM