Image table "nil value" issue and workarounds

My 4x4 image table (one PNG, 24x26 px per cell) works fine… but when I expand it to 4x5 or 4x6, I can’t :getImage past row 4… The Simulator says “attempt to index a nil value” once that image is requested. (I only need 5 rows, but I tried adding a dummy 6th—still can’t access Y=5, nor the new dummy Y=6).

So (4, 4) works… (4, 5) does not. I have verified the pixel dimensions and naming of the PNG: scifi-walls-dark-table-24-26.png, overall dimensions 96x130.

Are image tables limited to 4 rows? Or is the image’s previous 4x4 size perhaps cached somewhere and needs to be refreshed? (But the image contents do refresh just fine when I make edits. And making a copy of the PNG with a new name and using that fails the same way.)

Any ideas or workarounds? If it's a bug I'll report it there.

Weirdly, I CAN :getImage on (5, 1) or (5, 4), etc.—anything in column 5… even though there IS no column 5! Any access of the phantom column 5 returns the image from (1, 1). So nonexistent X=5 does work (sort of) but the actually existing Y=5 fails.

I'm using an image table with 1,980 cells of 38x38. That's 22 rows of 90. No problems.

Though I'd have to check if I use imagetable:getImage(n) or imagetable:getImage(x,y) I can't remember.

If you think it's a bug then please post a reduced test case.

1 Like

I made a tiny example to test this. It worked fine for me. It’s possible that something else could trigger a bug, I suppose.

Archive.zip (3.5 KB)

2 Likes

Many thanks for the test file! It's looking more like a bug maybe? Your 6x5 table works, but when I crop it to 4x5 it fails as follows:

• getImage(5,4) SHOULD fail, but it delivers getImage (1,5) instead.

• getImage(4,5) should work, but fails ("attempt to index a nil value").

FWIW, here’s a project containing both your original image and a 4x5 version that fails: https://webview.link/playdate/4x5-Image-Table-Test-Adams-Immersive.zip

For now I have my workaround: use a bigger table than I need, and it works. I'm now using an 8x10 table and it's fine. I only need 4x5, so most of the image is empty--but it's a useful workaround if this happens to othere.

Under the hood imagetables are stored in a one-dimensional array, but we track how many columns were in the original so that we can convert an (x,y) position into an array index. So here getImage(5,4) works because I'm not checking if the column > the original width, just doing n = (x-1) + (y-1)*width to get the index (the -1s to deal with Lua's 1-based indexing). So (5,1) would be the same as (1,2), (5,2) is the same as (1,3), etc.

And (4,5) doesn't work at all because I did check bounds on the row, but I have it checking against the width instead of the count divided by the width--i.e., the height. :man_facepalming:

Easy fixes, though! I'll get that in now.

2 Likes

That all makes sense! Easy to work around in the meantime. (Neven suggested another workaround on Discord: use the 1D count instead of 2D coordinates.)

Great spot @AdamsImmersive !

1 Like

I just checked, I don't use either! :flushed:

I do:

imagetable = gfx.imagetable.new("table-anim")

and:

sprite = gfx.sprite.new()
sprite:setImage( imagetable[n] ) 

So I guess imagetable[n] is synonymous with imagetable:getImage(n) ?

1 Like

yes, you can access images from an imagetable with getImage() to simply with its index.

https://sdk.play.date/1.9.0/#m-graphics.imagetable.__index

2 Likes