How to read properties off of a sprite

If you're having trouble with your Playdate device please contact support directly via the form at https://help.play.date/

Tips on what to include in your question:

  1. Have a clear title
  2. Provide what platform you're using the SDK on (Mac, Windows, or Linux)
    I'm using SDK 2.5.0

I feel like I'm trying to do something really basic, but I have the order wrong and can't figure it out.

Problem:

  • I have a sprite that I'm adding properties to. But when i try to access them they're nil. But then it's not. Sounds weird right. I'll try to break it down.

I'm currently running this in the SINGLE FILE EXAMPLES inside the sdk examples folder.

Here is my sprite (it's a menu)

import "CoreLibs/nineslice"
import 'CoreLibs/sprites'

local pd_nineslice <const> = playdate.graphics.nineSlice

-- -- import the nineslice image from images folder (or anywhere from your projects folder structure)
local nineslice <const> = pd_nineslice.new("assets/gridBackground", 5, 5, 16, 16)
-- -- local renderWidth <const>, renderHeight <const> = 10, 10
local renderWidth <const>, renderHeight <const> = 100, 100
-- -- local renderWidth <const>, renderHeight <const> = 200, 100
---@class Menu
---@field x integer 
---@field y integer 
class('Menu').extends(playdate.graphics.sprite)

function Menu:init(x, y)-- y, moveTo, pdedge
    Menu.super.init(self)
    self.xAxis = 3
    self.yAxis = 3
    self.Something = 42

    self:moveTo(x, y)
    self:setSize(100,100)

    self:add()
end

function Menu:draw()
    print("Is this a thing",self.xAxis, self.yAxis, self.Something)
    nineslice:drawInRect(0, 0, renderWidth, renderHeight)
end

function Menu:addButton(button)
    -- local menuX, menuY = self:getPosition()
    -- local buttonWidth, buttonHeight = button:getSize()
    print("Is this a thing",self.xAxis, self.yAxis, self.Something)

end

Then I run it using this:

import "menu"


Menu(50,50)
Menu:draw()
Menu:addButton("")

function playdate.update()
    playdate.graphics.sprite.update()
end

Then add it to the main


-- Uncomment the import for the example you'd like to run --

-- import 'animator'
-- import 'arcs'
-- import 'audio'
-- import 'balls'
-- import 'blurdither'
-- import 'collisions'
-- import 'crank'
-- import 'drawmode'
-- import 'drawSampled'
-- import 'drawSampled2'
-- import 'fast_fade'
-- import 'file'
-- import 'gridview'
-- import 'icosohedron'
-- import 'imagesample'
import 'menuDemo'
-- import 'pachinko'
-- import 'perlin-distribution'
-- import 'perlin1'
-- import 'perlin2'
-- import 'perlin3'
-- import 'perlin4'
-- import 'perlinfield'
-- import 'sndtest'
-- import 'spritescaling'
-- import 'stencil'
-- import 'synth'
-- import 'tilemaptest'
-- import 'zorder'


if playdate.update == nil then
	playdate.update = function() end
	playdate.graphics.drawText("Please uncomment one of the import statements.", 15, 100)
end

When I do this though. I get the following in the console.
image

This is confusing O...o It's nil, but then I'm guessing after the sprite update it's not nil. Which begs the question. How am I to use it?

Before this became a demo page I had it in my game and it would just blow up because it's nil.

Thoughts?

Menu(50, 50) should return an instance of a menu that then you can call the other functions on.

Can you try

local m = Menu(50, 50)
m:draw()
m:addButton()

Dont have my PC to try this out at the moment

1 Like

OMG!!!!! You're right! T.....T

This makes sense now that you've said it. If I don't set menu to a variable it's a one and done, I can't reference that instance of menu. I'd have to set it to a variable.

Hard to believe calling Menu:button didn't blow up.

Thank you!

1 Like

I'll add that it can be useful to make tables of sprites. For instance,

-- put new entities into a table
local entities = {}
for i=1, 10 do
  local my_entity = Entity(data)
  table.insert(entities, my_entity)
end

-- when you're ready to empty the screen
-- traverse table (in reverse) and nullify
for i=#entities, 1, -1 do
  entities[i] = nil
end

Because then you don't have to track and name every entity, you can refer to them and perform whatever functions you'd like on them in groups. Depending on how complex your game is, you may want several tables for different kinds of things.