orangeduck / LuaAutoC

Automagically use C Functions and Structs with the Lua API
Other
265 stars 49 forks source link

How to expose a class, instance methods, class methods, class memberes without lua wrapper code #27

Open iongion opened 6 years ago

iongion commented 6 years ago

First, hat down for the tremendous and elegant work you made, life saving library 👍 I am trying to wrap my head against the Birdie example, but can one avoid the lua wrapping code and aren't birdie_index and birdie_index present in the global scope ?

I am new to both lua, LuaAutoC and the libraries I am trying to bind, only that I have no clue how to start supporting this, do you have any tips for implementing these classes, basically: 1) How to define a class 2) How to add class methods 3) How to add instance methods

I like very much how this is done with https://github.com/vinniefalco/LuaBridge to be able to create a namespace, add a class, add methods and so on ... but that is C++ and it is overkill for my project, probably if I would use C++, I would choose that, but I can barely write C for now :)

Any tips, code, samples, links would highly be appreciated! Thank you!(sorry for the git issue, don't know how otherwise I could ask for info)

orangeduck commented 6 years ago

Yes, you can avoid the lua wrapping code and just use birdie_index and birdie_newindex in the global namespace if you like. The wrapper code is just to make the Lua table object act like the struct. You can also avoid the lua wrapping code by using the lua C API to perform exactly the same thing as the wrapping code (which I think is what you want to do).

Writing complex lua code using the C API can be tricky - my advice would be to convert it line-by-line into C if you can. Start here: https://www.lua.org/manual/5.3/manual.html#4 and read all of chapter 4. It might be hard to get your head around at first but after reading it a couple of times you'll see it isn't too difficult to use.

I hope that helps!