Hello, I'm trying to port over some libraries to kickstart prototyping on voxatron (such as 30log or cpml) and I got a weird issue with functions not finding an upvalue.
Let's say we have a B.lua file and a A.lua file
B.lua contains this:
B = {} B_mt = {} local function new() return setmetatable({}, B_mt) end function B.new() return new() end function B.draw() print("Called",0,0,7) end B_mt.__index = B function B_mt.__call() return B.new() end B = setmetatable({}, B_mt) |
It's basically a global class-ish definition with a constructor, an alias to the constructor via __index and the private constructor which is called internally.
Let's say we have an A.lua script which does:
local b = B() function _update() end function _draw() b.draw() end |
It simply gets a B instance and calls its draw function yet it doesn't work on Voxatron 0.3.5. I don't know what are the private/public global/local stuff available on this platform but this kind of pattern doesn't work well. I get this kind of error.
In vanilla lua, it works pretty well, I can use dofile or require sequentially those files and call _draw() without error.
So I don't know if it's an error or a quirk designed by Voxatron limitations, but I'd like to know if it's intended and what are the extends of using local/global variables between scripts in Voxatron.
Thanks and have a nice day.
Edit : I'm calling those pseudo-modules because as we don't have Lua's module system, there are a few patterns that allows for psuedo modules to happen. They're just loaded during the cart initialization and in a sequential manner.
Edit 2 : For further indication, this is cpml's way of making classes.
B = setmetatable({}, B_mt) |
You probably don't mean to be doing that after defining all of B's functions.
It still works because it's set to B_mt's __index. But I'm trying to simplify the bug case and it seems it also crashes without the metatable at all.
Using this code also doesn't work.
B = {} local function new() return {draw = B.draw} -- ugly but only there to make the snippet work once it's fixed end function B.new() return new() end function B.draw() print("Called",0,0,7) end |
Update: it sounds like a scoping issue. Wrapping the code in a do/end scope works.
B = {} do local function new() return {draw = B.draw} end function B.new() return new() end function B.draw() print("Called",0,0,7) end end |
[Please log in to post a comment]