Lua userdata not available

102 Views Asked by At

I have some relatively simple Lua code, which links to some C and some Lua.

local messages = {"Hello World!", "This is some test text", "This is a very long piece of test text with no sense of punctuation or common decency because I want to line wrap okay.", nil}
local i = 1

return  {
    draw = function()
        local blue = zenith.color_rgba(0, 0, 255, 255)
        local white = zenith.color_rgba(255, 255, 255, 255)
        local red = zenith.color_rgba(255, 0, 0, 255)
        local black = zenith.color_rgba(0, 0, 0, 255)

        zenith.clear_to_color(black)

        if(messages[i]) then
            zenith.text_box(white, blue, white, messages[i])
        else
            zenith.text_box(black, red, white, "+++PINEAPPLE ERROR+++\n+++REDO FROM START+++")
        end
    end,

    event = function(ev)
        if(zenith.event_type(ev) == zenith.ev_key_down and
            zenith.event_key_code(ev) == "SPACE" and
            messages[i] -- don't go off the array end
        ) then
            i = i+1
        end
    end
}

This works as I'd expect it to. As I press space, the on screen message changes until it runs into the end of the array. The the zenith. methods are all implemented in C, using Userdata to store a color (I have multiple ways of constructing a color (rgba, hsl, named, so it made sense to pass it around and not use numeric arguments)

I then tried to change the structure, like so:

local messages = {"Hello World!", "This is some test text", "This is a very long piece of test text with no sense of punctuation or common decency because I want to line wrap okay.", nil}
local i = 1

local blue = zenith.color_rgba(0, 0, 255, 255)
local white = zenith.color_rgba(255, 255, 255, 255)
local red = zenith.color_rgba(255, 0, 0, 255)
local black = zenith.color_rgba(0, 0, 0, 255)


return  {
    draw = function()

        zenith.clear_to_color(black)

        if(messages[i]) then
            zenith.text_box(white, blue, white, messages[i])
        else
            zenith.text_box(black, red, white, "+++PINEAPPLE ERROR+++\n+++REDO FROM START+++")
        end
    end,

    event = function(ev)
        if(zenith.event_type(ev) == zenith.ev_key_down and
            zenith.event_key_code(ev) == "SPACE" and
            messages[i] -- don't go off the array end
        ) then
            i = i+1
        end
    end
}

The only change being I've extracted the colors (which are userdata) to the outermost scope. I expected this to continue to work, but without the constant allocation/collection of the colors. Instead, I just get a black screen. Any tips? are my colors being prematurely garbage collected because they're only available in the closure? Is there some other magic I've missed? The only difference between colors and messages is that they're Userdata.

1

There are 1 best solutions below

0
On

Solved! The outer scope is called before the display is initialized. This causes the color creation code to return a color object all set to 0 (because obviously we can't create colors before the display initializes, makes perfect sense(!)).