Is it possible to pass a C++ class as a Lua table from a Gem?

0

I have a C++ class inside of my Gem that holds data that I need to pass to the Lua environment. I am aware that Lua tables cannot currently be passed into Gems (a limitation of AZstd::any mentioned in the documentation and on the forums), but I want to go the other way. I know normal Lua has certain C methods (see section 3.7 – Functions and Types) to facilitate conversion from C/C++ to Lua, but I don't know how to access those from Lumberyard (or even if those methods are available in Lumberyard). My questions are as follows:

  • Is it possible to pass a C++ class as a Lua table from a Gem?
    • If so, how? I didn't see any documentation on this, and I don't know where to look.
    • If not, what other method can I use to pass this data? A pointer (I have done this before, but I wanted to try to avoid managing my own memory)? (As for why I need to do it this particular way: I have a collection of C/C++ libraries, both 3rd party and personally created, that facilitate my development. A preliminary attempt to convert one of the simplest libraries into pure Lua failed spectacularly (it took a massive performance hit).)
asked 6 years ago314 views
3 Answers
0
Accepted Answer

Hello @REDACTEDUSER

Yes you can. You can use the Behavior Context to reflect a C++ class into a Lua table. Take a look at this page for more information about the Behavior Context and how you would go about achieving that.

Thanks!

answered 6 years ago
0

Good Question -- Let me find some answers for ya on this topic :)

Apologies for the delays.

answered 6 years ago
0

After much fiddling, I ended up getting a series of classes successfully passed between Lua and C/C++. Doing so required some modification to my core classes (which think I can avoid in the future using wrappers). Additional things that helped me while struggling through this:

  • The actual documentation for the behavior context class has a larger code snippet. It essentially has all of the code you linked, but as a single piece, which helped me see exactly where this code had to go.
  • Some type of macro is needed (AZ_TYPE_INFO_SPECIALIZE or AZ_RTTI) in order to reflect a class or type. I believe AZ_RTTI has to be in the public part of a class header, and the AZ_TYPE_INFO_SPECIALIZE has to be part of the AZ namespace in order for them to work. If you attempt to reflect without adding these macros, you will get some assertion error.
    • NOTE: This also applies to any type that your class returns, including lists/vectors of those classes (i.e. any type not AZ_TYPE_INFO_SPECIALIZE-d in the dev\Code\Framework\AzCore\AzCore\RTTI\TypeInfo.h file)
  • \dev\Gems\LmbrCentral\Code\Source\Rendering\MaterialHandle.cpp showed me that methods attached to the reflected classes didn't need to actually be part of the reflected class (MaterialHandle reflection).
  • \dev\Code\Framework\AzCore\AzCore\Script\ScriptSystemComponent.cpp Has a full example of reflecting an enum through a class (PlatformID) Thanks! My code is so much easier for me to understand and performs faster as a result of this!
answered 6 years ago

This post is closed: Adding new answers, comments, and votes is disabled.