Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua用户数据不可用_Lua_Lua 5.2_Lua Userdata - Fatal编程技术网

Lua用户数据不可用

Lua用户数据不可用,lua,lua-5.2,lua-userdata,Lua,Lua 5.2,Lua Userdata,我有一些相对简单的Lua代码,它链接到一些C和一些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 =

我有一些相对简单的Lua代码,它链接到一些C和一些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
}
这是我所期望的。当我按空格键时,屏幕上的消息会发生变化,直到到达数组的末尾。
zenith.
方法都是用C语言实现的,使用Userdata来存储颜色(我有多种构造颜色的方法(rgba、hsl、named),因此传递颜色而不使用数字参数是有意义的)

然后我试着改变结构,就像这样:

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
}

唯一的变化是我提取了颜色(这是userdata)到最外面的范围。我希望这会继续工作,但没有持续的颜色分配/收集。相反,我只是得到了一个黑屏。有什么提示吗?我的颜色被过早地垃圾收集,因为它们只在闭包中可用吗?有没有其他我错过的魔法?颜色和颜色之间的唯一区别e> 消息表示它们是用户数据。

已解决!在初始化显示之前调用外部作用域。这会导致颜色创建代码返回一个颜色对象,并将其全部设置为0(因为显然我们无法在显示初始化之前创建颜色,这非常有意义(!)