Pass Object to Lua Script from C#

拟墨画扇 提交于 2019-12-24 12:18:43

问题


I am using LuaInterface with C#, and have got everything set up correctly.

What I want to be able to do is that when the script is started using lua.DoFile(), that the script has access to a Player object that I can send...

Current Code:

public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
    QMain.lua.DoFile(LuaScriptPath);
}

But as you can see the script will not have access to the Player object.


回答1:


I see two options. The first one is to make your player a global variable for Lua:

QMain.lua['player'] = Player

Then you'll be able to access player in your script

The second option is to have the script define a function accepting player as parameter. So if your current script contains ...code... now it will contain:

function RunQuest(player)
    ...code...
end

and your C# code will look something like this:

public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
    QMain.lua.DoFile(LuaScriptPath); // this will not actually run anything, just define a function
    QMain.lua.GetFunction('RunQuest').Call(player);        
}


来源:https://stackoverflow.com/questions/7724791/pass-object-to-lua-script-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!