Multiplayer Treasure Grove Code Not Functioning


#1

I modified my code from Spring Thunder to prioritize coins in order of their value. This code works fine in the Spring Thunder level;

while (true) {
var item = hero.findNearestItem();
if (item.type == “coin” && item.value == 3 && hero.distanceTo(item) < 3) {
hero.moveXY(item.pos.x, item.pos.y);
} else if (item.type == “coin” && item.value == 2 && hero.distanceTo(item) < 3) {
hero.moveXY(item.pos.x, item.pos.y);
} else if (item.type == “coin”) {
hero.moveXY(item.pos.x, item.pos.y);
}
}

However when I run this exact same code on Multiplayer Treasure Grove I get a Line3 TypeError: Cannot read property ‘type’ of null. I don’t understand how the code works exclusively on one level.


#2

@supasebie

var item = hero.findNearestItem();
looks for the nearest item and if it doesn’t find any items it will return null.

Also if you no longer have this function due to a glasses upgrade in equipment then you will also get a null in the code.

So you cannot rely just on calling the function hero.findNearestItem(); you should also check to see if the variable is not null. In other words did it store the item or not?