Nineslice error

Context: I have a demo build using nineslice. It works great. But when I copy the same code and paste it into my game build it throws an error.

Error is this here
image

The code is

-- Replace main.lua with this for list gridview

import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "CoreLibs/ui"
import "CoreLibs/nineslice"
import "CoreLibs/crank"

local pd <const> = playdate
local gfx <const> = playdate.graphics

local gridview = pd.ui.gridview.new(0, 32)

local fruits = {"apple", "banana", "mango", "kiwi", "pineapple", "cherry", "orange"}

gridview:setNumberOfRows(#fruits)
gridview:setNumberOfColumns(2)
gridview:setCellPadding(2, 2, 2, 2)

gridview.backgroundImage = gfx.nineSlice.new("images/gridBackground", 6, 6, 20, 20)
gridview:setContentInset(5, 5, 5, 5)

local gridviewSprite = gfx.sprite.new()
gridviewSprite:setCenter(0, 0)
gridviewSprite:moveTo(150, 15)
gridviewSprite:add()


local btn = gfx.sprite.new()
btn:moveTo(50,50)
btn:add()

function gridview:drawCell(section, row, column, selected, x, y, width, height)
    if selected then
        gfx.fillRoundRect(x, y, width, height, 4)
        gfx.setImageDrawMode(gfx.kDrawModeFillWhite)
    else
        gfx.setImageDrawMode(gfx.kDrawModeCopy)
    end
    local fontHeight = gfx.getSystemFont():getHeight()
    gfx.drawTextInRect(fruits[row], x, y + (height/2 - fontHeight/2) + 2, width, height, nil, nil, kTextAlignment.center)
end

local fillbtn = false

function pd.update()
    gridInputs()
    print(gridview:getSelection())

    tstButton()

    if gridview.needsDisplay then
        local gridviewImage = gfx.image.new(241, 211)
        gfx.pushContext(gridviewImage)
            gridview:drawInRect(0, 0, 241, 211)
            gridview:setCellSize(111,100)
        gfx.popContext()
        gridviewSprite:setImage(gridviewImage)
    end

    gfx.sprite.update()
    pd.timer.updateTimers()
end



function tstButton()
    local fill = 0

    if fillbtn == true then
        fill = 100
    end
    local bar_image = gfx.image.new(50,20,gfx.kColorWhite)
    print("Button")

    local _, fontHeight = playdate.graphics.getTextSize("TEST")

    gfx.pushContext(bar_image)
        gfx.setLineWidth(2)
        gfx.drawRoundRect(1, 1, 50-2, 20-2, 3)
        gfx.fillRect(2, 2, fill, 20 - 4)
        gfx.setImageDrawMode(gfx.kDrawModeNXOR)
        gfx.drawTextAligned("Back", 50/2, (20 - fontHeight)/2 + 2, kTextAlignment.center)
    gfx.popContext()

    btn:setImage(bar_image)
end



function gridInputs()
    if pd.buttonJustPressed(pd.kButtonUp) then
        gridview:selectPreviousRow(false)
    elseif pd.buttonJustPressed(pd.kButtonDown) then
        gridview:selectNextRow(false)
    end

    if pd.buttonJustPressed(pd.kButtonRight) then
        gridview:selectNextColumn(false)
    elseif pd.buttonJustPressed(pd.kButtonLeft) then
        
        gridview:selectPreviousColumn(false)
    end

    if pd.buttonJustPressed(pd.kButtonA) then
        if fillbtn == false then
            fillbtn = true
            print("TEST")
            gridview:setSelection(0,0,0)
        else
            fillbtn = false
        end
    end
end

gridBackground

I've tried debugging the nineslice.lua but I'm not able to see how it can get 0 x 6...

I'm sending

	gridview.backgroundImage = gfx.nineSlice.new("images/bg/gridBackground", 6, 6, 20, 20)

Runs great in a demo build which is the frustrating thing.
Are there any gfx settings I can reset? I only guess is that it's getting thrown off by an earlier procedure. But based off of the logic I see in the nineslice.lua it just takes my arguments and does some math, none of which should come out as 0.

I'm a little lost on what it can be, any ideas?
ButtonDemo

It's very late here, so just first impressions...

In your posted code I see:

gridview.backgroundImage = gfx.nineSlice.new("images/gridBackground", 6, 6, 20, 20)

But then you post:

gridview.backgroundImage = gfx.nineSlice.new("images/bg/gridBackground", 6, 6, 20, 20)

Note '/bg/' is in one but not the other.

I have a demo build using nineslice. It works great. But when I copy the same code and paste it into my game build it throws an error.

It's not unusual to get 'unusual'-ish errors if your images aren't aren't actually where you're telling the runtime where they actually live, and also that the demo code frameworks may have an 'import' or two that you didn't in your own code.

Also, Lua is fussy about when and how things are declared in 'just laying around' space (I don't know the right Lua term for it, it's when you 'just have data initialization just sitting there 'outside' your function(s) in any old order' - it's quite possible to multiply declare data and/or functions and think you're using one when you're using something totally different, or swear you defined a function but used it 'before' it was declared/defined and got some head-scratching issues as a result.

@Tengu, that was just a copy paste typo. Interesting enough though, I did end up fixing the issue, but not by changing any code. After the tedious bit of coping my project and removing files until it worked I found that removing my old game buids, the pdx files did it get fixed. So I must have had some residual code or "something" causing some issues.

1 Like

In my experience, it's often the case that if I make a change to only the images of my project, without touching any Lua files, that pdc sometimes doesn't actually include the changed images. When this happens, I delete the .pdx and rebuild the project, and things then turn out ok.