Parsing and Organizing Lua Objects

BlitzMax Forums/BlitzMax Programming/Parsing and Organizing Lua Objects

Gabriel(Posted 2008) [#1]
I'm in the process of reworking my GUI module a little bit and the main emphasis is on getting the XML layouts and Lua scripts better integrated.

The GUI has interfaces ( a group of gadgets which form one complete layout ) and gadgets. Lua has both gadgets and interfaces represented as a table of functions, one function for each method. Simple enough but works well.

What I've done is have the function which loads an interface, parse all the gadgets it loads and automatically attach "events" ( which are BMax callbacks which in turn call Lua functions ) to them. The scripts containing those functions are automatically loaded into the VM at the same time.

That's well and good but I need some way to be able to access these objects from within Lua. I have a BlitzMax function to get an interface or a gadget by name - and it is bound to Lua - but it's clumsy having to do that every time. Up until now I was just manually creating global variables in Lua for whatever interfaces and gadgets I needed, but now that I'm trying to tidy all this up and hopefully even automate the whole kaboodle, it seems like this may not be the best way to go. I know that Lua lookups for globals are fast, but they're still being clogged up with a lot of unnecessary variables if it's automated, so a cleaner method is called for.

I'm not too well versed on Lua, just the basics. So I'm currently thinking in terms of having a nested tables for this. Essentially put all my gadget tables into a gadget table, indexed by the name of the gadget, then put the gadget table for each interface into an interface table, indexed by the name of the interface.

This should spread the load a little by indexing things in groups. It still means that I have to use a table lookup ( or two ) eachtime I want to access an object, but it doesn't have to call BMax, so the overhead is pretty light.

Does that sound like a plan or is there a better way of automatically parsing and managing my objects?


nino(Posted 2008) [#2]
I use an numeric id but thats not much different than what you're doing. I decided to stay away from light user data (pointers) on the Lua side but it is there if you want to use it. You just need to be really careful with GC.

I use nested tables as well - usually for what would be an array of structures in c. The only issue is consuming it on the bmx side can get a bit confusing with the remembering where you are on the stack crap.