Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] How to collect a lightstone?

Why does my goldCollected tracker go up, and lightstoneCollected tracker does not?
It’s essentially the same code. As the player picks up a coin, I get a message: Look what I've got! gold-coin . But when a lightstone is picked up, no messages, as if there was no collect event.

.

var hero=game.spawnPlayerXY("samurai", 34, 8);

game.lightstoneCollected = 0;
ui.track(game, "lightstoneCollected");
game.goldCollected = 0;
ui.track(game, "goldCollected");

game.spawnXY("lightstone", 50, 50);
game.spawnXY("gold-coin", 60, 40);

function onCollect(event){
    var item=event.other;
    hero.say("Look what I've got! " + item.type);
    if (item.type=="lightstone"){
        game.lightstoneCollected+=1;
    }
    if (item.type=="gold-coin"){
        game.goldCollected+=1;
    }
}

hero.on("collect", onCollect);

I tested the code with an game.addCollectGoal(); and I realised that the lightstone is not an item.So do not use onCollect, because onCollect is only for items.
I also tested with “collide” instead of “collect” but it does not work either.
Do you understand?

2 Likes

Do you need more assistance?

1 Like

Thank you! I’m at Game Dev 2 Final Project, so I guess I see what you mean. I thought the item type was wrong, like treasure chest type is “gem”, not “chest”.
It should be possible to indicate that a lightstone has been picked up if the two conditions are true:

  1. lightstone has been spawn
  2. player has stepped on an x-mark located at the lightstone coordinates

I’ll try and see how it goes.

1 Like

That is how you should check if the lightstone was collected.

1 Like

Here is the solution on how I tracked if the lightstone was spawn, collected and expired. I used a munchkin defeat to spawn a lightstone with a delay. Whatever entity the lightstone is, it’s possible to set actions for it.

var hero=game.spawnPlayerXY("samurai", 10, 20);
var lightX=game.spawnXY("x-mark-bones", 30, 40);
game.spawnXY("munchkin", 20, 30);

var lightExists=false;
game.lightstoneCollected = 0;
ui.track(game, "lightstoneCollected");
var lightDuration=6;
var lightEnd=0;

function onDefeat(event){
    game.spawnXY("lightstone", 30, 40);
    lightExists=true;
}

function checkLight(event){
    while(true){
        if (lightExists === true && hero.distanceTo(lightX) < 3 && lightEnd ===0){
            game.lightstoneCollected = 1;
            hero.say("I've got Light!");
            lightEnd=game.time+lightDuration;
        }
        if (lightEnd < game.time && game.lightstoneCollected === 1) {
            lightExists=false;
            hero.say("Ooops, no Light!");
        }
    }
}

game.setActionFor("lightstone", "spawn", checkLight);
game.setActionFor("munchkin", "defeat", onDefeat);
3 Likes

Congratulations! :smiley:

2 Likes