Trying to extend the "display" library functions, works in simulator but fails on actual devices

Hello,

Recently I’ve had the need of extending the “display” library of Corona.

The thing I was trying to do is saving the r/g/b/a values of a displayObject as properties on the displayObject itself. See code below:

local defaultR, defaultG, defaultB, defaultA = display.getDefault("fillColor") local defaultLineR, defaultLineG, defaultLineB, defaultLineA = display.getDefault("lineColor") local function extendDisplayObject(\_funcName) local cachedFunc = display[\_funcName] display[\_funcName] = function(...) local obj = cachedFunc(unpack(arg)) obj.r = \_funcName == "newLine" and defaultLineR or defaultR obj.g = \_funcName == "newLine" and defaultLineG or defaultG obj.b = \_funcName == "newLine" and defaultLineB or defaultB obj.a = \_funcName == "newLine" and defaultLineA or defaultA local cachedSetFillColor = obj.setFillColor obj.setFillColor = function(...) obj.r = arg[2] obj.g = arg[3] obj.b = arg[4] obj.a = arg[5] or obj.a cachedSetFillColor(unpack(arg)) end return obj end end extendDisplayObject("newCircle") extendDisplayObject("newContainer") extendDisplayObject("newEmbossedText") extendDisplayObject("newEmitter") extendDisplayObject("newImage") extendDisplayObject("newImageRect") extendDisplayObject("newLine") extendDisplayObject("newPolygon") extendDisplayObject("newRect") extendDisplayObject("newRoundedRect") extendDisplayObject("newSnapshot") extendDisplayObject("newSprite") extendDisplayObject("newText")  

It works in the simulator, but on devices (iOS & Android) it gives the following error:

“…display.lua:16: attempt to index local ‘obj’ (a nil value)”

Could someone point me in the right direction to look for the solution?

What version of Corona are you using?

Ex: The latest version/build is: 

2016.2879

Works for me on version 2879.

Which function is failing?

The only thing that stands out is that unpack could give you inconsistent results if arg contains nil s. But I’m hesitant, since I can’t think of any of those APIs not already balking at a nil argument. (Maybe the group?) Any better luck if you call it as

unpack(arg, 1, arg.n)

or do you get the same result?

Also, your setFillColor is broken for n = 1 or 2, assuming you intend Corona’s behavior. Off the top of my head, you probably want something like

if arg.n \<= 2 then if type(arg[1]) == "table" then -- Gradient... else obj.r, obj.g, obj.b = arg[1], arg[1], arg[1] obj.a = arg[2] or obj.a end else obj.r = arg[1] -- start at 1 obj.g = arg[2] obj.b = arg[3] obj.a = arg[4] or obj.a end

Sorry for wasting your time, but the problem was not in the extension itself, but in the code that was calling this.

As we all know devices are case-sensitive and desktops (atleast in my case) are not…

Corona has implemented a very useful functionality that warns us for case-sensitive mistakes, but as I was logging to much myself, so I overlooked.

What version of Corona are you using?

Ex: The latest version/build is: 

2016.2879

Works for me on version 2879.

Which function is failing?

The only thing that stands out is that unpack could give you inconsistent results if arg contains nil s. But I’m hesitant, since I can’t think of any of those APIs not already balking at a nil argument. (Maybe the group?) Any better luck if you call it as

unpack(arg, 1, arg.n)

or do you get the same result?

Also, your setFillColor is broken for n = 1 or 2, assuming you intend Corona’s behavior. Off the top of my head, you probably want something like

if arg.n \<= 2 then if type(arg[1]) == "table" then -- Gradient... else obj.r, obj.g, obj.b = arg[1], arg[1], arg[1] obj.a = arg[2] or obj.a end else obj.r = arg[1] -- start at 1 obj.g = arg[2] obj.b = arg[3] obj.a = arg[4] or obj.a end

Sorry for wasting your time, but the problem was not in the extension itself, but in the code that was calling this.

As we all know devices are case-sensitive and desktops (atleast in my case) are not…

Corona has implemented a very useful functionality that warns us for case-sensitive mistakes, but as I was logging to much myself, so I overlooked.