Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

[SOLVED ]Variable Not Updating, Ogre Gorge Gouger


#1

Hi there! help me with the level please. I figured that my variable doesn’t update for some reason. I don’t understand why. I want the hero to spot the nearest coin, determine if its worth is > 1, and if so, pick it up. If not, get the next coin. After my hero picks up a good coin, he continues to think that the nearest and worthy coin is the one he has already picked up. The “nearest” variable doesn’t update. Here’s my code. Thanks to everyone in advance.

while (hero.time < 20) {
    var coins = hero.findItems();
    var coinIndex = 0;
    if (coins) {
        while (coinIndex < coins.length) {
            var nearest = hero.findNearest(coins);
            hero.say(nearest);
            if (nearest && nearest.value > 1) {
                while (hero.distanceTo(nearest) > 1) {
                hero.move(nearest.pos);
                }
            } else {
                break;
            }
            nearest = null;
            coinIndex++;
        }
    }
}

#2

Hi,
This isn’t a problem of your variable not updating, it’s just that the variable is always the same, you’re always looking for the same coin, your nearest one:

That will never change since you never move anywhere.
If you want to select only gold and silver coins go back to this level and practice using and manipulating arrays:


:lion: :lion: :lion:


#3

Hi Deadpool, I want to clarify this: You say that the variable is always the same. How? If I pick it up, it should dissapear. So the next time the loop runs the coin I have already picked up should be disregarded. However in my case it isn’t, the hero keeps thinking the one he has just picked up is the nearest one.
Also you say I never move anywhere. I actually do, my hero does move to the coin and pick it up.
I really hope for help. I’ve been stuck here for too long…


#4

Variables won’t disappear. It always contains something once you put information into it. Right now, you’re putting a type object in the variable. In order to update every time, you have to loop it to keep finding the next possible nearest coin. But it will return the same object if you haven’t collected the previous “nearest” coin unless there’s another coin that’s closer than the previous nearest coin.


#5

Chaboi, exactly! You say

and my var nearest is in the while loop. So why doesn’t it get updated? The new while loops starts --> the program reads that the var nearest should be assigned to hero.findNearest(coins);, so the program has to find a new nearest.

Then, you’re saying:

But my hero does collect it, the amound of gold i have increases and he does move to it so i know he picked it up.

i hope for more replies please
:pray::pray::pray:


#6

You forgot to indent there


#7

This still won’t work, what I’m about to say is basically this:

Just as an example with your code.
Example:
What if you’re nearest coin is a bronze coin?
The code will just get stuck:

1 while (hero.time < 20) {
2     var coins = hero.findItems(); 
3     var coinIndex = 0;
4     if (coins) {
5         while (coinIndex < coins.length) {
6             var nearest = hero.findNearest(coins); # you're finding you're nearest coin, say for example, a bronze one.
7             hero.say(nearest); # "Bronze Coin"
8             if (nearest && nearest.value > 1) { # this if statement will return False meaning the code will run on to line 12
9                 while (hero.distanceTo(nearest) > 1) {                                                       
10                hero.move(nearest.pos);
11                }
12        } else {
13            break; # now the code will break from the "while (coinIndex < coins.length) {" loop and return to line 4, the process will repeat again and your hero will never move anywhere, or get any coins.
14            }
15            nearest = null;
16            coinIndex++;
17        }
18    }
19}

:lion: :lion: :lion:


#8

Hey Deadpool.

First off, thanks a ton for such a detailed explanation. I see what you mean here with an example with a Bronze Coin. But what if there is a silver one?
In this case the if (nearest && nearest.value > 1) condition will run, and hero will pick up the Silver Coin. Then the while (coinIndex < coins.length) loop runs again, and what happens? Thsi line var nearest = hero.findNearest(coins); runs, which should mean he finds another nearest(not the Silver one he has already picked up). But my hero keeps finding the coin he has already picked up. This is the problem.Or is there a different logic behind this which I don’t get yet?

Thank you in advance.


#9

I don’t think it’s that your hero thinks his nearest is the one he’s already picked up.
Say, like you said, the first coin is silver. Your hero will collect it, now what if your next nearest coin isn’t silver or gold?
The same thing as the bronze coin example will happen.
You’ll have to change this code to make it work.
:lion: :lion: :lion:


#10

Agree, the code will get stuck, but in this case he wouldn’t keep repeating that his nearest is Silver, correct?

This line. I put it there to know what hero thinks the nearest is. I know the other code would get stuck but why this line repeats about Silver? All I wanna know right now. Thanks!


#11

i’m still looking for help


#12

Sorry, but I can’t really help you with that. Why do you want to see what’s wrong with this code, which won’t work without quite a lot of changes being made.
Like maybe an array of coins which you iterate through and find your nearest gold or silver one? That would work well and has been covered in previous levels. Especially the ones I mentioned earlier.
:lion: :lion: :lion:


#13

Okay. I did an experiment where I called an array of all coins , and then made hero say it,like that:

var coins = hero.findItems;
hero.say(coins);

Then I picked up a first coin from the array, and then called the hero.say(coins) again and my picked up coin was still in the array. I then made my hero move to the position of that coin that I already picked up, and he did it (although, obviously, the coin wasn’t there).

Does it mean that you have to call the array every time , aka var coins = hero.findItems;, in order to get the updated array?

Thanks.


#14

With or without coins the expression

if (coins) // will be always true

check this code in the level, you know there aren’t griffin riders:

var griffinRiders = hero.findByType('griffin-rider');
if (griffinRiders)
   hero.say('I need someone to help me!');
hero.wait(2);
if ( griffinRiders.length === 0)
   hero.say("But there's no one!");

Usually one use:

while (coinIndex < coins.length) // to make something with
     // var coin = coins[coinIndex];
     // you can compare coin with some nearest coin
     // and then go to coin
     // but this is way too complicated

I cannot grasp the logic of your while loop.
If you want to have some priority gold > silver > bronze in this case you’ll be loosing time.
I implemented this algorithm and it isn’t efficient - 36s collecting all coins.
When using the simplest method - 30.5s.
You can see it yourself:
Ogre Gorge Gouger
To admins: it will be good if we can upload videos.