Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorry for asking here, can you reply me? #254

Open
ryuj-h opened this issue May 25, 2021 · 3 comments
Open

Sorry for asking here, can you reply me? #254

ryuj-h opened this issue May 25, 2021 · 3 comments

Comments

@ryuj-h
Copy link

ryuj-h commented May 25, 2021

https://stackoverflow.com/questions/67685270/how-to-pass-lua-table-to-the-c-class-constructor-using-luabridge

@ryuj-h
Copy link
Author

ryuj-h commented May 25, 2021

class MyObjectClass
{
public:
MyObjectClass(lua_State* L/How to passed lua table here?/)
{
luabridge::LuaRef param = luabridge::LuaRef::fromStack(L, 2);
if (param.isTable())
{
cout << param[1] << endl;
//...
}
}
};

getGlobalNamespace(L)
.beginClass("MyObjectClass")
.addConstructor<void ()(lua_State)>()
.addFunction("printmynum",&MyObjectClass::printmynum)
.endClass();

solved..

@ryuj-h ryuj-h closed this as completed May 25, 2021
@dmitry-t
Copy link
Collaborator

You can also pass luabridge::LuaRef to the constructor. One line less.

@ryuj-h
Copy link
Author

ryuj-h commented May 25, 2021

c++

class MyObjectClass
{
public:
	MyObjectClass(lua_State* L)
	{
		LuaRef param = LuaRef::fromStack(L, 2);
		if (param.isTable())
		{
			LuaRef num1 = param.rawget("num1");
			LuaRef num2 = param.rawget("num2");
			LuaRef num3 = param.rawget("num3");
			LuaRef num4 = param.rawget("num4");

			cout << num1 << endl;
			cout << num2 << endl;
			cout << (int)num3 << endl;
			cout << (int)num4 << endl;
		}
	}
	void printmynum()
	{
		cout << "HELLO" << endl;
	}
};


L = luaL_newstate();//lua_open();
luaL_openlibs(L);

getGlobalNamespace(L)
	.beginClass<MyObjectClass>("MyObjectClass")
	.addConstructor<void (*)(lua_State*)>()
	.addFunction("printmynum", &MyObjectClass::printmynum)
	.endClass();

lua
object1 = MyObjectClass({num1 = 9999,num2 =11244,num3 =643642534,num4 =734547});
object2 = MyObjectClass({num1 = 9999,num2 =11244,num3 =643642534,num4 =734547});
object3 = MyObjectClass({num1 = 9999,num2 =11244,num3 =643642534,num4 =734547});
object4 = MyObjectClass({num1 = 9999,num2 =11244,num3 =643642534,num4 =734547});

object1:printmynum();
object2:printmynum();
object3:printmynum();
object4:printmynum();

This code make crash with error

"Lua error: ...s--------------------------------\hellolua.lua:10: attempt to call a nil value (method 'printmynum')
PANIC: unprotected error in call to Lua API (attempt to call a nil value)"

Do you know what I missed?

@ryuj-h ryuj-h reopened this May 25, 2021
@ryuj-h ryuj-h closed this as completed May 25, 2021
@ryuj-h ryuj-h reopened this May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants