I'm trying to use LDtk entity referrals so that I can link a button to another entity and have it call an activate() script on that entity. To test, I just added a door that opens when Door:activate() is called, and added
elseif entityName == "Door" then
local door = Door(entityX, entityY, "door")
self.buttonEntities[door.entityIid] = door
elseif entityName == "Button" then
local button = Button(entityX, entityY, entity, self, "button", "door")
self.buttonEntities[button.entityIid] = button
end
to where I add the LDtk entities, and
self.buttonEntities = {}
at the top of the file. Then, in the button script, I have
local pd <const> = playdate.graphics
local gfx <const> = playdate.graphics
import "scripts/player"
import "scripts/gameScene"
import "CoreLibs/animator"
import "CoreLibs/graphics"
class('Button').extends(AnimatedSprite)
function Button:init(x, y, entity, gameManager, entityIid, linkedObjectIid)
self.pressed = false
self.fields = entity.fields
self.direction = self.fields.Direction
self.floor = 1
self.gameManager=gameManager;
self.entities = self.gameManager.entities
self.entityIid = entityIid
self.linkedObjectIid = linkedObjectIid
local buttonImageTable = gfx.imagetable.new("images/button-table-20-40")
Button.super.init(self, buttonImageTable)
self:addState("Default", 1, 2, {tickStep = 10})
self:addState("Pressed", 2, 6, {loop = false})
self:playAnimation()
self:setZIndex(Z_INDEXES.Button)
self:setCenter(0, 0)
self:moveTo(x, y)
self:add()
self:setTag(TAGS.Button)
self:setCollideRect(0, 0, self:getSize())
self.collisionResponse = "bounce"
end
function Button:pickUp(player)
if self.pressed == false then
local object = self.entities[self.linkedObjectIid]
if object then
object:activate()
end
self:changeState("Pressed")
self.pressed = true
self:setCollideRect(16, 0, 4, 20)
end
end
But when I build and run, I get
Table index is nil
In the line that calls
self.buttonEntities[door.entityIid] = door
(And yes, everything is set up right in LDtk)