Reading nested Lua tables in C

Is it possible to parse arbitrary nested tables passed from Lua to C? I'm relatively new to Lua, but it looks like Playdate doesn't expose any of the stock Lua functions for working with lua_State, so my only choice is to use the playdate->lua functions, but those don't seem to allow for parsing tables arbitrarily.

The context here is that I'm porting the poorly performing core logic of my game from Lua to C, and when I was all in Lua, I defined a level like this:

LEVELS.intro1 = {
    name = "Basics 1", next = "intro2",
    emitter = { x =  20, y = 120, deg =  90, speed = 200, rate = 0.05, },
    target =  { x = 300, y =  160, deg = 270, },
    barriers = {
        { x = 20, y = 30, w = 360, h = 10 },
        { x = 20, y = 200, w = 360, h = 10 },
    ...
}

As a workaround, I've started creating Lua functions in C, like setName, setNext, initEmitter, initTarget, and addBarrier. Should I just continue down that road? Or pursue some other option?

Sorry I never gave you a reply to this, didn't have an answer for you at the time and then it fell off the radar. :confused: Hopefully you've already found a good solution to this. If not, here's a hack I threw together for a different thread that includes the Lua functions for manipulating the lua_State: Lua load function seems to expect a second argument. Why? - #17 by dave It's definitely a "could break in a future update" type thing, but I think you're good until we switch to Lua 5.5. And that would possibly break bytecode compatibility so it might be never.

1 Like