Randomly saw your comment (a month later). Consider reading "Rule of Zero" (google it). Also, check out the channel 9 talks on move semantics and unique_ptr. Read the reference for unique_ptr. Don't bother with the others until you understand unique_ptr.
No not quite. I'm thinking of an optional callback. I can't say for sure since I don't know the specifics of your application. Strongly coupling something to Lua isn't necessarily wrong. Again, it depends. Overgeneralization is also a cardinal sin.
In the case that you mention, you need to wrap the class anyways (luabind requires this). There's no way to alter the original class's virtual function table portably. I would prefer in such a case, to provide the additional functionality from lua in the wrapper. Thoughts?
Ok I thought about this a little more and I think there is actually a simple and clean way to do this. For some reason, I forgot about the Lua reference system. Feel free to make an issue for it on Github to remind me and I may get to it soon. I have the code more or less working in my head.
Thanks for providing an example and the link. I will say that there is a certain code smell to what you are doing. Saving gamestate in C++ with Lua extensions is great but I'm not convinced virtual function overriding is the best/only approach. You could, for example, pass the derived Lua table name to the gamestate saver in the constructor and call that function. That way, if there is any derived behavior, the correct function would be run.
As with the other request, I'll sleep on it a bit. If you could come up with a motivating example that couldn't be solved with other means, that would be significant.
I believe the difference between using quotes and brackets is that the former prioritizes the current directory over a system include. Thus, quotes should be used within a library whereas including external includes (like <lua.h>) should use brackets.
edit:
I realize now that I did this correctly (following my own advice) everywhere except for the C includes for some reason. I'll fix that at some point.
Ok so something like this is obviously impossible (mostly). Virtual function tables are a language construct so implementing something like this would be very difficult indeed. The virtual function call would only be seen correctly from Lua.
In practice, this usually isn't so bad because classes people want to use with Lua are typically only used in Lua. This is usually a good idea from a design perspective.
Oh i see, I thought you were talking about "virtual" functions in Lua. If you register a C++ class with virtual methods, the correct function overload will be called.
Suppose you have two classes, A and B, where A inherits from B and overloads a function zap. When Lua constructs an object of type A with a = A.new() say, C++ will instantiate it with new A. When a:zap() is invoked, the zap call will be invoked on a pointer of type A * so polymorphism is in full effect.
Yea this is easy actually. When a function in a table is invoked, it first looks in the table itself. If it isn't there, it will look in the __index table (assuming __index doesn't refer to a function). To override the function in the __index table, just provide a function in the original table.
e.g.
-- Assume Foo has a bar() method
Bar = Foo
function Bar:bar()
-- override Foo:bar here
end
Now, if you override the Bar constructor to set the metatable to Bar after calling the Foo constructor, you've now implemented a class which inherited from Foo and overrode bar.
The reason I opted not to use luabind is that luabind is a huge project with an undesirable boost dependency. IMHO, luabind is also a lot harder to use for more simple things and the most common tasks. The philosophy I had was to handle the majority of cases with the cleanest syntax possible. You're encouraged to look at the README for examples.
Also, my library has no dependencies outside of lua. My reasoning is that, lua was meant to be small and compact. Tacking on lots of cruft seemed to belie the underlying philosophy.
The challenges preventing me from diving straight into this are possible memory issues (i.e. a c++ std::function is passed to a lua function but the std::function gets deallocated in the meantime or a lua function is passed to C++ which gets stored somewhere but meanwhile that function gets nil'd out).
I would encourage you to explore other options first. Do you know if other lua binding libraries allow this and how they handle it if they do? One issue I can see is that Lua functions can't be popped off the stack and stored somewhere; the api doesn't allow it. This means the function needs to be used in that invocation. Also, if multiple functions are passed back, I'd have to take care of that too. I have a rough sketch of how I would approach this but I'm not convinced that it's hyper-critical, or that there's a clean way to solve these issues.
In principle this should be doable although it wouldn't be super easy to just pass a selector as an argument. The problem with doing so with the current design is that the selector is readable, writable, and callable. However, to write to it, it must know how to traverse to that element. This wouldn't be possible if that element was pushed directly to the stack (as opposed to being traversed to by chaining [] operators).
Creating a sel::LuaFun type may be the way to do it. I'll sleep on it I think, but as I said, I don't see any reason why it couldn't be done.
Let me know how it goes. If you find something that doesn't work so well it will probably help me too (even if you decide to ultimately use something else).
Thanks yea I remember you haha. I got a lot of good suggestions since that time. Building it has been fun and there were some tricky implementation details.
I started it in /r/cpp mainly because I thought it was an interesting case study for some more esoteric C++11 features but I think it's ready for more mainstream usage now.
Registering classes, member variables, class member functions, free standing functions, lambdas, functors, std::functions, is not easy :). It took a lot of work to make the interface clean while still supporting all the above for all the various types. Not to mention variadic returns too.
The main idea is that in order for a component (e.g. a weapon system) to emit information, it shouldn't have to know anything about the receivers of that information, including the recipient address. It's up to the recipient to register itself in a "phonebook" and subscribe to that event.
So you can infer that you need a general "event" class with some sort of enumerable identifier. A weapons system might emit a custom event inherited from the general event class. Also, you'lll need an event "dispatcher" where various components send the events and subscribe to them as well.
The basic implementation is very simple and easy to write. As you can probably imagine, there are many features you could add if your game needed them. For example, tagging the emitted event with the sender id. This can all by done via specializing the event class.
Does it support mouse capture and fullscreen on linux yet?
I liked it several months ago but those were absolute dealbreakers for me.
Sure you could "nope" it out of there and run away. I can't say this approach will serve you well long term though.
If you want some free career advice, never do when the poster above you did.
Randomly saw your comment (a month later). Consider reading "Rule of Zero" (google it). Also, check out the channel 9 talks on move semantics and unique_ptr. Read the reference for unique_ptr. Don't bother with the others until you understand unique_ptr.
Hand-cranked flashlight please! :D j/k that could be really irritating to play with haha.
I agree with you. If the user wants to learn OpenGL, the deprecated fixed-function pipeline should be ignored altogether.
No diploma is recognized.
No not quite. I'm thinking of an optional callback. I can't say for sure since I don't know the specifics of your application. Strongly coupling something to Lua isn't necessarily wrong. Again, it depends. Overgeneralization is also a cardinal sin.
In the case that you mention, you need to wrap the class anyways (luabind requires this). There's no way to alter the original class's virtual function table portably. I would prefer in such a case, to provide the additional functionality from lua in the wrapper. Thoughts?
Would being able to pass arbitrary lua functions to your C++ code help?
Ok I thought about this a little more and I think there is actually a simple and clean way to do this. For some reason, I forgot about the Lua reference system. Feel free to make an issue for it on Github to remind me and I may get to it soon. I have the code more or less working in my head.
Thanks for providing an example and the link. I will say that there is a certain code smell to what you are doing. Saving gamestate in C++ with Lua extensions is great but I'm not convinced virtual function overriding is the best/only approach. You could, for example, pass the derived Lua table name to the gamestate saver in the constructor and call that function. That way, if there is any derived behavior, the correct function would be run.
As with the other request, I'll sleep on it a bit. If you could come up with a motivating example that couldn't be solved with other means, that would be significant.
I believe the difference between using quotes and brackets is that the former prioritizes the current directory over a system include. Thus, quotes should be used within a library whereas including external includes (like <lua.h>) should use brackets.
edit:
I realize now that I did this correctly (following my own advice) everywhere except for the C includes for some reason. I'll fix that at some point.
OH.
Ok so something like this is obviously impossible (mostly). Virtual function tables are a language construct so implementing something like this would be very difficult indeed. The virtual function call would only be seen correctly from Lua.
In practice, this usually isn't so bad because classes people want to use with Lua are typically only used in Lua. This is usually a good idea from a design perspective.
:) Thanks for your feedback and suggestions thus far.
Oh i see, I thought you were talking about "virtual" functions in Lua. If you register a C++ class with virtual methods, the correct function overload will be called.
Suppose you have two classes, A and B, where A inherits from B and overloads a function
zap. When Lua constructs an object of type A witha = A.new()say, C++ will instantiate it withnew A. Whena:zap()is invoked, thezapcall will be invoked on a pointer of typeA *so polymorphism is in full effect.Am I understanding the question correctly?
Yea this is easy actually. When a function in a table is invoked, it first looks in the table itself. If it isn't there, it will look in the __index table (assuming __index doesn't refer to a function). To override the function in the __index table, just provide a function in the original table.
e.g.
Now, if you override the
Barconstructor to set the metatable toBarafter calling theFooconstructor, you've now implemented a class which inherited fromFooand overrodebar.The reason I opted not to use luabind is that luabind is a huge project with an undesirable boost dependency. IMHO, luabind is also a lot harder to use for more simple things and the most common tasks. The philosophy I had was to handle the majority of cases with the cleanest syntax possible. You're encouraged to look at the README for examples.
Also, my library has no dependencies outside of lua. My reasoning is that, lua was meant to be small and compact. Tacking on lots of cruft seemed to belie the underlying philosophy.
The challenges preventing me from diving straight into this are possible memory issues (i.e. a c++ std::function is passed to a lua function but the std::function gets deallocated in the meantime or a lua function is passed to C++ which gets stored somewhere but meanwhile that function gets nil'd out).
I would encourage you to explore other options first. Do you know if other lua binding libraries allow this and how they handle it if they do? One issue I can see is that Lua functions can't be popped off the stack and stored somewhere; the api doesn't allow it. This means the function needs to be used in that invocation. Also, if multiple functions are passed back, I'd have to take care of that too. I have a rough sketch of how I would approach this but I'm not convinced that it's hyper-critical, or that there's a clean way to solve these issues.
In principle this should be doable although it wouldn't be super easy to just pass a selector as an argument. The problem with doing so with the current design is that the selector is readable, writable, and callable. However, to write to it, it must know how to traverse to that element. This wouldn't be possible if that element was pushed directly to the stack (as opposed to being traversed to by chaining [] operators).
Creating a
sel::LuaFuntype may be the way to do it. I'll sleep on it I think, but as I said, I don't see any reason why it couldn't be done.Can you give me a code snippet of what you are trying to do?
Let me know how it goes. If you find something that doesn't work so well it will probably help me too (even if you decide to ultimately use something else).
Thanks yea I remember you haha. I got a lot of good suggestions since that time. Building it has been fun and there were some tricky implementation details.
I started it in /r/cpp mainly because I thought it was an interesting case study for some more esoteric C++11 features but I think it's ready for more mainstream usage now.
Registering classes, member variables, class member functions, free standing functions, lambdas, functors, std::functions, is not easy :). It took a lot of work to make the interface clean while still supporting all the above for all the various types. Not to mention variadic returns too.
The main idea is that in order for a component (e.g. a weapon system) to emit information, it shouldn't have to know anything about the receivers of that information, including the recipient address. It's up to the recipient to register itself in a "phonebook" and subscribe to that event.
So you can infer that you need a general "event" class with some sort of enumerable identifier. A weapons system might emit a custom event inherited from the general event class. Also, you'lll need an event "dispatcher" where various components send the events and subscribe to them as well.
The basic implementation is very simple and easy to write. As you can probably imagine, there are many features you could add if your game needed them. For example, tagging the emitted event with the sender id. This can all by done via specializing the event class.
Yea, unfortunately C++11 was necessary for me to create the syntax that I wanted.