Hi
I'd like to start by saying that the book has been an amazing resource. I would also like to clarify that I've been using it more as a reference than following it step-by-step, which may be the reason I'm facing issues.
I'm trying to add scripting to my AI, but I want to keep it simple for now. Just have the brain and movement LUA and handle everything else in C++. I set up a test project to tinker with LUAPlus until I have a firm grasp of it before adding it to my engine. The current issue I'm facing is binding a table member function to a C++ variable.
In the code that I've added, Line 19 in the cpp works fine, but 27 causes a crash with the error message: "Assertion failed: 0, at ../luafunction.h line 41". When I remove lines 14-16 in test.lua, the error no longer happens.
Based on this I assume the issue is that tableObj1.GetByName("functionName") returns a function with no reference to its parent table. I've read the source code for the TeapotWars and I can't see what I'm doing wrong.
Here is my code
main.cpp
Display All
test.lua
Display All
I'd like to start by saying that the book has been an amazing resource. I would also like to clarify that I've been using it more as a reference than following it step-by-step, which may be the reason I'm facing issues.
I'm trying to add scripting to my AI, but I want to keep it simple for now. Just have the brain and movement LUA and handle everything else in C++. I set up a test project to tinker with LUAPlus until I have a firm grasp of it before adding it to my engine. The current issue I'm facing is binding a table member function to a C++ variable.
In the code that I've added, Line 19 in the cpp works fine, but 27 causes a crash with the error message: "Assertion failed: 0, at ../luafunction.h line 41". When I remove lines 14-16 in test.lua, the error no longer happens.
Based on this I assume the issue is that tableObj1.GetByName("functionName") returns a function with no reference to its parent table. I've read the source code for the TeapotWars and I can't see what I'm doing wrong.
Here is my code
main.cpp
Source Code
- pLuaState = LuaPlus::LuaState::Create(true);
- pLuaState->DoFile("test.lua");
- LuaObject globals = pLuaState->GetGlobals();
- LuaObject metaTableObj = pLuaState->GetGlobals().Lookup("RandomMove");
- if (metaTableObj.IsNil())
- {
- metaTableObj = pLuaState->GetGlobals().CreateTable("RandomMove");
- }
- metaTableObj.SetObject("__index", metaTableObj);
- metaTableObj.RegisterObjectDirect("GetX", (TestLua*)0, &TestLua::GetX);
- metaTableObj.RegisterObjectDirect("SetX", (TestLua*)0, &TestLua::SetX);
- TestLua obj1(6);
- LuaObject table1Obj = pLuaState->GetGlobals().CreateTable("table1");
- table1Obj.SetLightUserData("__object", &obj1);
- table1Obj.SetObject("__index", metaTableObj);
- table1Obj.SetMetaTable(metaTableObj);
- pLuaState->DoString("table1:prints()");
- auto prints = table1Obj.GetByName("prints");
- if (!prints.IsFunction())
- cout << "nil function" << endl;
- else
- {
- LuaPlus::LuaFunction<void> func(prints);
- func();
- }
test.lua