Tracking Items Collected Not Working Game Dev 2

On Game Dev 2, it is not working. I am trying to track and increase the number of items collected. However, it is not working.

#Goals

game.addMoveGoalXY(28,28)
game.addMoveGoalXY(62, 11)
game.addMoveGoalXY(11,60)
game.addMoveGoalXY(61, 60)
game.addDefeatGoal(10)
game.addCollectGoal(5)
game.addSurviveGoal()

#Player

player = game.spawnPlayerXY("knight", 45, 15)
player.maxHealth=500
player.attackDamage=150
player.maxSpeed=1

#Setting

game.spawnMaze("forest", 4)
game.spawnXY("forest", 60, 42)
game.spawnXY("forest", 28, 20)

#Enemies

generator=game.spawnXY("generator", 60, 30)
generator.health=9001
generator.spawnType = "munchkin"

unit = player.findNearestEnemy
unit.maxSpeed=100
unit.attackDamage= 100

#Objects to Collect
game.spawnXY("gold-coin", 35, 12)
game.spawnXY("gold-coin", 60, 50)
game.spawnXY("gold-coin", 61, 18)
game.spawnXY("gold-coin", 60, 32)
game.spawnXY("mushroom", 13,40)

#track

ui.track(game, "defeated")
ui.track(game, "collected")

#Tracking

def onCollect(event):
    item=player.findNearestItem
    if item.type == "gold-coin":
        game.collected += 1

game.on("collect", onCollect)

you might want to add parentheses at the end of this

1 Like

Also I would check what’s the item was collected instead of “searching”.
If I remember correctly event.target should refer to the collected item.

Also should it be

avatar.on('collect', onCollect)

In this case event.target refer “who collected”, event.other to “what collected”. It’s better to check docs for that.

Avatar does not work. However, I used a different discussion with the same solution to solve the issue. The collecting works now. I used this solution:

ui.track(game, "defeated")
ui.track(game, "collected")
hero.on("defeat", onDefeat)
def onDefeat (event):
    game.defeated = 0
    unit = event.target
    item = event.other
    if unit.type == "ogre" or unit.type == "munckin" :
        game.defeated += 1
game.collected = 0
hero.on("collect", onCollect)
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "gold-coin":
        game.collected += 1

However, the defeated property is not working. This is my new code.

#Goals

game.addMoveGoalXY(28,28)
game.addMoveGoalXY(62, 11)
game.addMoveGoalXY(11,60)
game.addMoveGoalXY(61, 60)
game.addDefeatGoal(10)
game.addCollectGoal(5)
game.addSurviveGoal()

#Player

player = game.spawnPlayerXY("knight", 45, 15)
player.maxHealth=500
player.attackDamage=150
player.maxSpeed=10

#Setting

game.spawnMaze("forest", 4)
game.spawnXY("forest", 60, 42)
game.spawnXY("forest", 28, 20)

#Enemies

generator=game.spawnXY("generator", 60, 30)
generator.health=9001
generator.spawnType = "munchkin"

unit = player.findNearestEnemy
unit.maxSpeed=100
unit.attackDamage= 100

#Objects to Collect
game.spawnXY("gold-coin", 35, 12)
game.spawnXY("gold-coin", 60, 50)
game.spawnXY("gold-coin", 61, 18)
game.spawnXY("gold-coin", 60, 32)
game.spawnXY("mushroom", 13,40)

#track

ui.track(game, "defeated")
ui.track(game, "collected")

#Tracking

game.defeated=0

def onDefeat (event):
    enemy = event.target
    enemy = event.other
    if unit.type == "munckin" :
        game.defeated += 1

game.collected = 0

def onCollect(event):
    item = event.target
    item = event.other
    if item.type == "gold-coin":
        game.collected += 1

player.on("defeat", onDefeat)
player.on("collect", onCollect)

avatar I mean you player or who collects items.

Oh ok. Can you help me with the defeating tracker issue please?

What is unit here? If you want to track defeat event you need to assign it on units which will be defeated. Have you played levels before that? If I remember correctly it should be something like setActionFor('munchkin',...)
I can’t check docs now, will be able to do it on Monday only.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.