Creating and instancing classes with the Object API

I'm old to programming but new to Lua + the pd SDK, and I'm trying to wrap my head around how objects are handled with the Object API. I'm having no trouble creating and extending classes, adding methods and properties, or importing them. However, I'm having a weird issue where I instance and object and bind it to a variable, but that variable remains nil. For instance, I have a Bounds class (in a bounds.lua file) that looks like this:


function Bounds:init(yStartPosition)

    local boundsImage =, disp.getHeight()/4)
        gfx.fillRect(0, 0, disp.getWidth(), disp.getHeight())
        --gfx.drawRect(0, 0, hashWidth, hashHeight)
    self:moveTo(0, yStartPosition)
    self:setCenter(0, 0.5) -- anchor sprite at left edge

    -- test

function Bounds:shiftBounds()
    print('enter shiftBounds')

Then back in main.lua, I have something like this:

    boundaryTop = new Bounds(0) = "top"

The object instance works fine, and it behaves properly in the game. However, any time I try to reference the boundaryTop variable, it's nil, so doing the dot assignment throws an error, and the print statement returns nil. It's almost the constructor doesn't return an object? (By the way, the three-line snippet above is scoped to a function, but even when I move it global scope, I have the same issue.)

There must be something obvious I'm missing.

I'll give you a hint: if you add new = 1 before that code in main, then boundaryTop will be equal to 1. :wink:

If you do boundaryTop = Bounds(0) it should work as expected.

Haha, oh, thank you! Now I see that I am JavaScript brain poisoned. :melting_face:

1 Like