gfx.drawCircleAtPoint(x, y, r) not callable

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

class("Foo").extends(gfx.sprite)

function Foo:init(size)
    self.size = size
    local image <const> = gfx.image.new(size, size)
    gfx.pushContext(image)
        gfx.drawCircleAtPoint(0,0,10)  -- ERROR HERE
    gfx.popContext()
    self:setImage(image)
    self:add()
end

Is throwing

Foo.lua:11: field 'drawCircleAtPoint' is not callable (a nil value)
stack traceback:
	Foo.lua:11: in field 'init'
	CoreLibs/object.lua:70: in global 'Foo'
	main.lua:8: in main chunk

However, if i replace the draw circle funation with a draw rect one (gfx.drawRect(0, 0, 10, 10)) then things run fine.

I'm sure I'm matching the signature is correct.

I'm no Lua/SDK expert, but maybe try the full callout ('playdate.graphics.drawCircleAtPoint') rather than the contraction one?

'cause 'pushContext' says:

... Pushes the current graphics state to the context stack and creates a new context...

Maybe that includes the 'gfx' too?

Well, no, that doesn't make any sense, because 'drawRect' worked.

Hmm.

Thanks @Tengu . I tried that too with the same result.

It's pickling my brain.

You must import CoreLibs/graphics to use the circle drawing functions.

See docs: https://sdk.play.date/inside-playdate/#_circle

Also, the first line of your init needs to be:

Foo.super.init(self) -- this is critical

See docs: https://sdk.play.date/inside-playdate/#C-graphics.sprite

2 Likes

This is super strange. Having given up, gone to bed and woken up it just works now.

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

class("Foo").extends(gfx.sprite)

function Foo:init(size, backgroundColor)

    self.size = size
    local image <const> = gfx.image.new(size, size, backgroundColor)
    gfx.pushContext(image)
        gfx.drawCircleAtPoint(self.size / 2, self.size / 2, self.size / 2)
        gfx.drawRect(0, 0, self.size, self.size)
    gfx.popContext()
    self:setImage(image)
    self:add()
end

function Foo:update()

end

image

However it's working without importing CoreLibs/graphics and without calling Foo.super.init(self).
I guess the missing call to super is Ok because it's parameterless?

1 Like

No, you really need to do that other 'init' call, as you're extending the sprite class and without it your subclass instance isn't completely set up, 'infrastructure' wise (higher up in the hierarchy).

1 Like

With the Sprite, I've probably got away with it because there isn't any setup in the Sprite's init().

Still odd that I don't have CoreLibs/graphics imported.