Orge Gorge Gouger now function not working?

Hi

I’ve been having some issues with the self.now function in this level, here’s my code:

loop:
    
    items = self.findItems()
    
   
    while self.now() < 20:
       for item in items:
            pos = item.pos
            x = pos.x
            y = pos.y
            self.moveXY(x, y)
   
    
    while self.now() > 20:
        self.moveXY(14, 37)
        self.buildXY("fence", 21, 37)

I’ve been trying to simplify it as much as possible / not worrying abot the gold, but whatever I do my char nevers breaks out of the first while loop, even after the now function returns a time greater than 20.

Any ideas where I’m going wrong?

You have a loop in a loop. Your for loop will go to every item in items even if the timer on while has expired. A while loop doesn’t end a process in it if the condition is False; it just doesn’t repeat.

Just axe the for loop and keep the while
(you will have to edit your code to make this work)

The level will also work better if you use self.move(item.pos)

Ahh magic! Thanks very much… Now for some questions that will take newb to whole new levels of face palming:

My new code, which works looks like this:

while self.now() < 20:
    item = self.findNearest(self.findItems())
    coins = self.findItems()
    coinIndex = 0
    coin = coins[coinIndex]
    self.move(coin.pos)

Why does this work? When I try to run it without;

    coinIndex = 0
    coin = coins[coinIndex]

My char just sits there, waiting for the orges to come screaming down on em. What is the array coinIndex doing that allows the code to cycle through the coin poisitions?

Without defining coinIndex the value of coinIndex will be undefined and coins[coinIndex] will be undefined too, as the code will try to look up the element in coins with the index of undefined.

Dont you receive some error message when you use the broken code? At least coin.pos should produce an error.

I understand why I have to define coinIndex for the array to work, my question is why do I need the array?

Without it my char sits still, or sometimes moves in a straightline to the edge of the map and just keeps trying to walk.

When you remove the array, where does the variable coin receive his value from? If there is no one assign it a value, then coin will hold the value of undefined, or whatever has been assigned to it before.

But if I remove the array, my char is still getting a position to move to from somewhere, for example:

while self.now() < 20:
    item = self.findNearest(self.findItems())
    coin = self.findItems()
    #coinIndex = 0
    #coin = coins[coinIndex]
    self.move(coin.pos)

Just causes the char to walk in a straight line to the edge of the map. Why? Why isn’t the char moving to the position of the nearest coin, which has been assigned in the find items etc?

I am not sure what the Python to JS transpiller generates here, but my guess is that coin.pos will be interpreted as a vector to {0, 0} when coin is undefined. I am only guessing because I work mainly with JS.

But to your second question, replace coin with item inside of self.move, then it might just walk to the nearest coin. You already search for the nearest item by doing:

item = self.findNearest(self.findItems())

If I remove the coin var then my char collects items, but for some reason just stands still once their out of the first while loop and doesn’t move on to the second (to escape and build the fence):

while self.now() < 20:
    item = self.findNearest(self.findItems())
    #coin = self.findItems()
    #coinIndex = 0
    #coin = coins[coinIndex]
    self.move(item.pos)
   
    
while self.now() > 20:
    self.moveXY(14, 37)
    self.buildXY("fence", 21, 37)

Appreciate you trying to help and sorry if it seems like I’m trying to be difficult, I’m just trying to understand the why of it, instead of just blindly typing it in, if that makes sense.

self.now() returns the count of seconds since the start of the level.

At what time does your first loop end, and at what time does your second loop start?

You leave the first while loop when self.now is >= 20 which may be exactly 20. The programm will then step down and check the condition of the second while loop. Which may be false because the time is exactly 20 and with that its not greater 20. So the programm execution will skip your while loop.

You can completely remove the second while and just call the moveXY and buildXY methods. As the program execution will remain in the first while loop until 20 seconds are reached.

Even if your time is greater than 20 seconds when the programm execution checks the second while loop. The while loop will be an endless loop as the timer will never be below 20 seconds after this point :wink: (except for when the value overflows, but thats far after your life time)

If I change the loops to end at < 19 and start at > 20 , without the array I’m still having the same issue.

However if I un-comment the array even when they both end / start at 20 it works fine.

Edit: aware of the <= and >=, but my question is still about the array. Following Marrk’s suggestion works perfectly, but I’m still trying to understand how it is that the array is allowing the original code:

while self.now() < 20:
    item = self.findNearest(self.findItems())
    coin = self.findItems()
    coinIndex = 0
    coin = coins[coinIndex]
    self.move(coin.pos)

To work and why that code doesn’t work without it?

x < 19 = 18.99
x > 20 = 20.01

Do you know about the >= and <= operators?

Edit: Your code is compiling to an error. Can you please fix it and clarify what the problem is? I may be seeing it on my end, but I want to verify.

What is your full code that is causing the problem?

So my original code that works is:

loop:    
       
    while self.now() < 20:
        item = self.findNearest(self.findItems())
        coins = self.findItems()
        coinIndex = 0
        coin = coins[coinIndex]
        self.move(coin.pos)
   
    

    while self.now () > 20:
        self.moveXY(14, 37)
        self.buildXY("fence", 21, 37)

However if I remove:

        coinIndex = 0
        coin = coins[coinIndex]

So I’m using:

    while self.now() < 20:
        item = self.findNearest(self.findItems())
        coin = self.findItems()
       # coinIndex = 0
       # coin = coins[coinIndex]
        self.move(coin.pos)

Then my characater will just run straight to the left of the screen and keep going. My question(s) is / are why does the array allow the code to work? What is the array doing?

I did already answer this, at least I tried. What exactly is still unclear to you about that?

Hi, so, we checked the code and verified the problem.

When passing undefined (in Javascript) or None (in Python), the movement code would still try to move somewhere.

We have implemented a fix in the form of an error, so if you reload the page it should inform you that you are passing undefined.

Ah, so it was an error with the level as much as anything else.

Marrrk, understand that you answered earlier, but I was trying to work out why the char was still moving / what the array was doing.

I can see now that the char shouldn’t have moved at all, instead the array is what should have been allowing the var coin to work in the first place.

Thanks to you both very much :smile: