我想知道如何使用Lua中另一个脚本的函数.例如,say GameObjectUtilities保存许多GameObject脚本将使用的函数.本Slime(一GameObject)脚本想要使用的功能GameObjectUtilities.
我无法解决这个问题.我看过这里,但我还是没有完全理解.我是否需要创建一个模块或表来保存函数以GameObjectUtilities使其中的函数用于其他脚本?如果是这样,最好的方法是什么?
这很奇怪.当我以正常方式执行时,它确实有效.问题是,当我运行我的应用程序并尝试使用该脚本时,它不起作用.我不明白.
我将Lua嵌入到C/C++应用程序中.有没有办法从C/C++调用Lua函数而不先执行整个脚本?
我试过这样做:
//call lua script from C/C++ program
luaL_loadfile(L,"hello.lua");
//call lua function from C/C++ program
lua_getglobal(L,"bar");
lua_call(L,0,0);
Run Code Online (Sandbox Code Playgroud)
但它给了我这个:
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
Run Code Online (Sandbox Code Playgroud)
我这样做时只能调用bar():
//call lua script from C/C++ program
luaL_dofile(L,"hello.lua"); //this executes the script once, which I don't like
//call lua function from C/C++ program
lua_getglobal(L,"bar");
lua_call(L,0,0);
Run Code Online (Sandbox Code Playgroud)
但它给了我这个:
hello
stackoverflow!!
Run Code Online (Sandbox Code Playgroud)
我想要这个:
stackoverflow!
Run Code Online (Sandbox Code Playgroud)
这是我的lua脚本:
print("hello");
function bar()
print("stackoverflow!");
end
Run Code Online (Sandbox Code Playgroud) 我最近开始使用SFML,并注意到没有提供任何种类的"FreeResource"方法.例如,sf::Font有一个函数调用LoadFromFile,但没有函数来释放资源.
我觉得这很奇怪.我错过了什么吗?我唯一的选择是创建sf::Font指针并动态分配和删除它吗?
我正在创建一个按钮类,我很难决定两种解决方案.
1)对Button类进行模板化,并在按下按钮时使用其构造函数中的函数对象进行调用.我正在编码的那个人担心这会导致代码膨胀/颠簸.
2)创建一个ButtonAction基类,每个按钮都有一个不同的ButtonAction.因此,Button当按下按钮时,类在其构造函数中采用ButtonAction进行调用.
我们也考虑过使用函数指针,但没有仔细考虑过.