Two “or”s ruin everything...

Hi everybody!

I coded a simple game my daughter wrote and drew, and I’ve made a version that everybody can import, using only generic assets; zip file is attached.

The graphics don’t make any sense this way, but you can still play it and tell what’s happening.

It uses my old “super simple button” technique, and it all works very nicely, but I’ve been vexed by one thing.

The button function has x, y, width, and height parameters, and if width and/or height are nil, it checks a stored button table to see if there are saved values—so for instance, setting the width before doing drawing looks like this:


      width = width or storedTable[buttonName].width

But when I try to do the same thing for the x and y values, the buttons stop working—for some reason adding those two ‘or’ statements breaks the game!

If you want to see what I mean, go to the uiPieces tab and reverse the commenting of lines 15 and 16, which are;


    local x,y = buttonTable.x, buttonTable.y
    --local x,y = x or buttonTable.x, y or buttonTable.y

Can anyone tell me why, or how to fix it?

actually I was not correct about some assumptions, will get back to this once I get a clearer picture

Haven’t looked at your code, but is buttonTable a table? You might need to point to an element in the table - buttonTable[buttonidentifier].x

@West buttonTable is a single button, in the code it’s defined as local buttonTable = uiPieceHandler.buttons[name]

How do I upload .zip files? It seems hard to actually import the game from the link I posted.

@UberGoober to upload a zip, tap the little “File” button in the above compose message box. Then choose “Choose File” then select “Browse…” which will bring up the iOS file picker

@Simeon thanks! I updated it.

From what I saw your passed x,y and buttonTable x,y are very different values. That’s as far as I got, not sure why the clicks stop working

@UberGoober The two or’s aren’t ruining everything. You just have bad values for the button positions. Make the following changes for b1 and b2 in the code and you’ll see a red and green circle where the center of your button should be.

In Main, draw()

function draw()
    profiler.draw()
    currentScreen()
    
    pushStyle()
    fill(255,0,0)
    ellipse(b1.x,b1.y,50)
    fill(0,255,0)
    ellipse(b2.x,b2.y,50)
    popStyle()
end

In uiPieces, button

    --set button drawing values, using saved values if none passed in
        
    local buttonTable = uiPieceHandler.buttons[name]   
    --local x,y = buttonTable.x, buttonTable.y
    local x,y = x or buttonTable.x, y or buttonTable.y
    b1=vec2(x,y)
    b2=vec2(buttonTable.x,buttonTable.y)
        
    width = width or buttonTable.width
    height = height or buttonTable.height

omg yes!!

That actually wasn’t unexpected behavior, since the code was supposed to use x and y only if those values were actually passed in as parameters, and otherwise fall back to the stored values, so the stored values and the passed-in values should draw at different places.

…or should they?

You made me re-think that. Now I’m capturing any passed-in x and y values, and overwriting the stored values with them, and it works!

Thank you so much.

When I clean it up I’ll post a fixed .zip in case anyone wants to see it.