sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

How do I put new values of any type onto the lua stack from c++?

Open TwinHits opened this issue 3 years ago • 1 comments

Hello!

How do I put new values onto the lua stack from c++?

I have a game engine with a sol3 lua user type interface and I want my user type to support:

val = usertype:getProperty(key)

Where val could be any type depending on the key string.

On the C++ side however, the only way I have found this to work is to convert the value to a string, return the string to the lua side and then let lua dynamically convert the string back to the intended value.

This conversion to string and back seemed extra to me. I haven’t found anything in the docs related to this, however it feels like if I am able to create a new sol::object on the C++ side, that would do what I want it to do. Is that possible?

TwinHits avatar May 11 '22 17:05 TwinHits

How do I put new values onto the lua stack from c++?

If you need to actually push and pop from the stack, then see for example sol::stack::push(L, arg) https://sol2.readthedocs.io/en/latest/api/stack.html?highlight=push#stack-push

create a new sol::object on the C++ side, that would do what I want it to do. Is that possible?

You can use auto object = sol::make_object(lua, val); along with sol::this_state if you dont have access to the lua state. See https://sol2.readthedocs.io/en/latest/tutorial/functions.html?highlight=make_object#any-return-to-and-from-lua

Rochet2 avatar May 11 '22 17:05 Rochet2