Closed GasimGasimzada closed 6 months ago
What is wrong with the first example you give? I think that (or something very very very close to it) should work.
That does not work because sol::property
does not accept an argument. What I need to do is to create a class that has a getDirection
function, then use that as the property:
struct UIViewStyle {
String getDirection() {
converDirectionToString(this->direction);
}
YGDirection direction = YGFlexDirectionRow;
};
But this is an antipattern because my UIViewStyle struct is a plain old struct with no data in it and I essentially need to either store the entire serialization as a member function of the class or create a Lua specific proxy class that maps to this, which becomes very complex when this object is part of another structure, which I will need to create Lua proxy class as well.
What I need to do is to create a class that has a
getDirection
function, then use that as the property
In general for sol, you can do all kinds of magic in the function signature that sol will recognize and act upon. For example, you can add an argument to have access to the lua state like this: https://sol2.readthedocs.io/en/latest/api/this_state.html
For your case you can bind a function that takes in a UIViewStyle&
or UIViewStyle*
or something similar (both work, because sol magic).
Here is a working example (godbolt link):
In general all functions tied to lua userdata are just free functions that pass the data forward. It is possible to tap into that in sol as well as seen above, which can allow you to make quite free and interesting choices in the way you bind things. However, sol also has abstractions that allow you to just bind the member functions and variables directly for ease of use.
@Rochet2 Thank you for the help! I had no idea if this was possible and there are so many places where I needed to do this and ended up creating lots of adapter/proxy classes to handle that. Now, I have removed those dependencies and made them significantly simpler using this method.
I have a
struct
that has no methods inside it:I want to expose this via Sol but modify what's returned for some of the properties without adding getter functions inside the struct. I like to keep the structs as structs without any methods inside them.
Is there a way that I can create a free function that accepts the property as a getter and returns a string value (
YG*
values are all enums)? Something like this:Or is it possible to create strings from enums; so, whenever I pass enum to Lua or retrieve an enum from Lua, I always work with strings instead of C++ enums, which are integers in this case: