对象有问题,不再需要但仍有引用.结果:由于未收集对象,分配的内存大小不断增加.
如何解决这类问题?有没有办法找到只有一个引用的对象,或者生命周期超过某个值的对象?或任何其他解决方案?
使用Lua 5.1和C++与luabind.
谢谢.
我试图将我的std::map<std::string, std::string>作为类属性公开给 Lua。我已经为我的 getter 和 setter 设置了这个方法:
luabind::object FakeScript::GetSetProperties()
{
luabind::object table = luabind::newtable(L);
luabind::object metatable = luabind::newtable(L);
metatable["__index"] = &this->GetMeta;
metatable["__newindex"] = &this->SetMeta;
luabind::setmetatable<luabind::object, luabind::object>(table, metatable);
return table;
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以在 Lua 中做这样的事情:
player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
Run Code Online (Sandbox Code Playgroud)
但是,我在 C++ 中提供的代码没有被编译。它告诉我在这一行metatable["__index"] = &this->GetMeta;和它之后的行有一个对重载函数的模棱两可的调用。我不确定我这样做是否正确。
错误信息:
error C2668: 'luabind::detail::check_const_pointer' :
ambiguous call to overloaded function
c:\libraries\luabind-0.9.1\references\luabind\include\luabind\detail\instance_holder.hpp 75
Run Code Online (Sandbox Code Playgroud)
这些是SetMeta和GetMeta在FakeScript:
static void GetMeta();
static void SetMeta();
Run Code Online (Sandbox Code Playgroud)
以前我是为 getter 方法这样做的:
luabind::object FakeScript::getProp()
{
luabind::object obj = …Run Code Online (Sandbox Code Playgroud) 在gcc升级后,由于错误,我的项目无法构建:
In file included from /usr/include/luabind/wrapper_base.hpp:31:0,
from /usr/include/luabind/back_reference.hpp:27,
from /usr/include/luabind/class.hpp:93,
from /usr/include/luabind/luabind.hpp:28,
from /home/ockonal/Workspace/themisto/engine/include/Scripting/ScriptManager.hpp:21,
from /home/ockonal/Workspace/themisto/engine/source/Core/Root.cpp:28:
/usr/include/luabind/detail/call_member.hpp:319:1: error: missing binary operator before token "("
In file included from /usr/include/luabind/back_reference.hpp:27:0,
from /usr/include/luabind/class.hpp:93,
from /usr/include/luabind/luabind.hpp:28,
from /home/ockonal/Workspace/themisto/engine/include/Scripting/ScriptManager.hpp:21,
from /home/ockonal/Workspace/themisto/engine/source/Core/Root.cpp:28:
/usr/include/luabind/wrapper_base.hpp:92:1: error: missing binary operator before token "("
In file included from /usr/include/luabind/function.hpp:10:0,
from /usr/include/luabind/class.hpp:94,
from /usr/include/luabind/luabind.hpp:28,
from /home/ockonal/Workspace/themisto/engine/include/Scripting/ScriptManager.hpp:21,
from /home/ockonal/Workspace/themisto/engine/source/Core/Root.cpp:28:
/usr/include/luabind/detail/call_function.hpp:326:1: error: missing binary operator before token "("
In file included from /usr/include/luabind/detail/constructor.hpp:12:0,
from /usr/include/luabind/class.hpp:96,
from /usr/include/luabind/luabind.hpp:28,
from /home/ockonal/Workspace/themisto/engine/include/Scripting/ScriptManager.hpp:21,
from /home/ockonal/Workspace/themisto/engine/source/Core/Root.cpp:28:
/usr/include/luabind/wrapper_base.hpp:92:1: …Run Code Online (Sandbox Code Playgroud) 现在我想传递一个void*指向Lua 的指针,使用userdata?这该怎么做?
顺便说一下,我用过luabind,但是它无法通过一个void*指向Lua堆栈的指针,这很烦人!你们能帮助我吗?
struct Event
{
A* a;
B* b;
...
};
Event *e;
void* instance = (void*)e;
// param is a parameter that is passed from Lua script. param is a Event object. And I cast it into a void* type
string Answer(void* param)
{
WorkEvent *pWorkEvent = static_cast<WorkEvent*>(param);
ASSERT_RET(pWorkEvent, NULL);
string call_id = pWorkEvent->GetCallId();
CCmThreadManager::TType thrd_id = pWorkEvent->GetHandleThrdID();
Coroutine *pco = pWorkEvent->m_pco;
Run Code Online (Sandbox Code Playgroud) 我使用LuaBind在我的C++应用程序中嵌入了Lua.我需要有多个运行的变量,这些变量不能被运行相同文件名的其他对象访问.
例如:假设我有一个叫做的课程NPC.一个NPC拥有一个字符串,也就是它们运行的脚本的名称.NPC创建a 时,会创建一个名为的变量Health.当NPC被击中时,他们会失去5点健康.这些脚本在Lua中会是这样的:
local health = 10
function onHit()
health = health - 5
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是每个NPC运行此脚本的人都没有自己的健康实例.例如,假设我创建NPC A,并从其健康状况中减去5.然后,我创造NPC B.因为它将健康重置为10,如果我告诉NPC A打印健康,它会让我回到10,即使它应该是5.
如果我为每个对象都有一个不同的Lua实例,那么它会以这种方式工作,但在这种情况下我一次最终会有数百个实例,我理解这不是一件好事.
在Lua中有没有办法让变量像这样工作?如果没有,是否有一种脚本语言能够以有效的方式运行?
作为参考,这是我正在测试的代码:
LUA:
local health = 10;
function onHit()
health = health - 5
print_out(health)
end
Run Code Online (Sandbox Code Playgroud)
C++:
class NPC
{
public:
NPC(lua_State* inState);
void onHit();
const char* behavior;
lua_State* luaState;
};
NPC::NPC(lua_State* inState)
{
luaState = inState;
behavior = "testBehavior.lua";
luaL_dofile(luaState, behavior); …Run Code Online (Sandbox Code Playgroud) 我正在使用Luabind将一个基类从C++公开给Lua,我可以从Lua中派生类.这部分工作正常,我可以从Lua中的派生类调用C++方法.
现在我想要做的是在我的C++程序中获取指向基于Lua的实例的指针.
C++ - >绑定
class Enemy {
private:
std::string name;
public:
Enemy(const std::string& n)
: name(n) {
}
const std::string& getName() const {
return name;
}
void setName(const std::string& n) {
name = n;
}
virtual void update() {
std::cout << "Enemy::update() called.\n";
}
};
class EnemyWrapper : public Enemy, public luabind::wrap_base {
public:
EnemyWrapper(const std::string& n)
: Enemy(n) {
}
virtual void update() {
call<void>("update");
}
static void default_update(Enemy* ptr) {
ptr->Enemy::update();
}
}; …Run Code Online (Sandbox Code Playgroud) 我在DLL中导出了一些导出到Luabind的类,一切都适用于这两个类(LuaScriptManager,EventManager).我可以从Lua调用它们的函数,一切都很好,但现在我正在尝试在我的客户端可执行文件中设置一个与DLL链接的新类,到目前为止还没有运气.
这是我为每个函数调用的错误消息:"找不到匹配的重载,候选:void loadResource(ResourceManager&,std :: string const&)"
类绑定来自http://www.nuclex.org/articles/5-cxx/1-quick-introduction-to-luabind:
struct Manager {
Manager() :
m_ResourceCount(0) {}
void loadResource(const std::string &sFilename) {
++m_ResourceCount;
}
size_t getResourceCount() const {
return m_ResourceCount;
}
size_t m_ResourceCount;
};
static Manager MyResourceManager;
void Bind(lua_State* l)
{
// Export our class with LuaBind
luabind::module(l) [
luabind::class_<Manager>("ResourceManager")
.def("loadResource", &Manager::loadResource)
.property("ResourceCount", &Manager::getResourceCount)
];
luabind::globals(l)["MyResourceManager"] = &MyResourceManager;
}
Run Code Online (Sandbox Code Playgroud)
这是相应的lua测试代码:
-- The following call will fail with the above error
MyResourceManager:loadResource("abc.res")
--MyResourceManager:loadResource("xyz.res")
-- If the function is commented out, this will …Run Code Online (Sandbox Code Playgroud) 如何使用接受可变数量参数的luabind绑定函数?基本上,我想编写自己的print()函数.
我知道objectluabind 中的类作为参数可以接受任何数据类型,最好是接收动态表luabind::object作为参数.
我想在Lua中为脚本编写器设置一些函数,但是当调用这些函数时,我希望它们实际上使用我定义的参数调用单个函数.这可能看起来像:
LUA:
foo1()
foo2()
Run Code Online (Sandbox Code Playgroud)
C++:
int myFunction( lua_State * context )
{
int numArgs = lua_gettop( context );
int topOfStack = -3;
bool lightUserDataFlag = lua_islightuserdata( context, topOfStack );
if( lightUserDataFlag )
{
}
}
Run Code Online (Sandbox Code Playgroud)
}
所以我对设置有点困惑.我试图用param注册回调.这就是我到目前为止所做的事情并没有通过函数注册保存参数; 它似乎只保存我添加的最后一个参数.
void setupCallback()
{
lua_register( context, "foo1", Callback);
lua_pushlightuserdata( context, &value[0] );
lua_register( context, "foo2", Callback);
lua_pushlightuserdata( context, &value[1] );
}
Run Code Online (Sandbox Code Playgroud)
当lua调用我的回调时,我总是看到值[1],而不是值[0].我玩了注册回调然后添加一个表的概念,但我不确定这会让我更进一步.
至于原因:我正在尝试抽象函数名称,以便为我们的lua程序员编写代码更容易,但那些只需要转换为相同的参数化函数.可以想象它通过传递单个参数添加更改设置,但命名方式不同.
思考?