I am a super-newbie, so hopefully I can articulate this question properly, but I am running into an area involving collisions between objects in my test project. Basically, I am using Windfield so help with collisions in a top-down game, and I have a "player.lua" file with a "player" table, and another file, "yarrow.lua" that has a table for an object that the player, in theory, will be able to interact with.
Basically, I am using a function inside player.lua for "interacting" with objects and am checking for collisions with "queryCircleArea" (relevant area of function bolded below):
Code: Select all
function playerInteract(x,y)
local harvestables = world:queryCircleArea(x, y, 10, {"Harvestable"})
[b]if #harvestables > 0 then
for key,value in ipairs(harvestables) do
--CALL FUNCTION IN COLLIDED OBJECT
value:harvested()
end
else[/b]
--nothing
end
end
Code: Select all
function spawnYarrow (x, y, width, height)
local yarrow = {}
if width > 0 and height > 0 then
yarrow.collider = world:newRectangleCollider(x, y, width, height, {collision_class = "Harvestable"})
yarrow.yarrowSheet = yarrowSprites.yarrowSheet
yarrow.yarrowSheet:setFilter("nearest", "nearest")
yarrow.collider:setFixedRotation(true)
yarrowX = x
yarrowY = y
yarrow.harvested = 0
yarrow.collider:setType('static')
yarrow.animation = yarrowAnimations.unharvested
[b]function yarrow:harvested()
if yarrow.harvested == 0 then
yarrow.harvested = 1
end
end[/b]
table.insert(yarrows, yarrow)
end
end
Right now, when the "player" queryCircleArea() gets to the point where it tries to call the "value:harvested()" function, it returns the error: "Attempt to call method 'harvested' (a nil value)."
I was under the impression that "queryCircleArea()" returned a table containing all collided objects, which according to my understanding would be the "yarrow" object containing "yarrow:harvested()" but this is clearly not the case, and I am not entirely sure what the next step to resolve this would be. I am sort of at an "I don't know what I don't know" point when it comes to this issue, so any guidance would be appreciated.
Apologies if this doesn't make sense, I am more than happy to clarify anything above. Thanks in advance.