Cloudrip Commander: Code never finished


#1

Hello,

I would like to start by saying how amazing this game is, and has taught me way more than any non-game coding tutorial.

The problem with my code is that one of the following things happens after running it:

  1. All soldiers are built and nothing else happens other than gold count slowly ticking up.

  2. Only some of the soldiers are built, freezing the game with the timer often giving a time range of NaN to Infinity.

  3. No soldiers are built, also resulting in the latter.

Both 2 and 3 give me the error message of ‘Code never finished. It’s either really slow or has an infinite loop.’

Number 1 only happens after copying the code, reloading, and then replacing all the code with the copied code.

I don’t fully understand how some parts of arrays work, so this code is the result of me looking through the other posts about this levels and trying to make sense of things. I have gotten at most one of the soldiers to go to the base point (50, 40) (always the first one built) and Anya, which means that the whole command to move the soldier chunk only ran once.

When I have gotten it to work, I got a ‘Hard execution limit of 3000000 exceeded.’ which I believe means that soldierIndex kept increasing, but the command to move the soldiers is not working.


// Each soldier costs 20 gold.
while (this.gold > this.costOf("soldier")) {
    this.summon("soldier");
}

soldiers = this.findFriends();
soldierIndex = 0;
// Add a while loop to command all the soldiers.
while (soldierIndex < soldiers.length) {
    soldier = soldiers[soldierIndex];
    this.command(soldier, "move", {x: 50, y: 40});
    soldierIndex + 1;
}
// Go join your comrades!
this.move({'x': 50, 'y':40});

Could somebody please point me in the right direction?


#2

First, remember that in JavaScript, you use the keyword var to initialize variables. This is one of two problems in your code. The second one is using the command move. move only moves your hero of the equivalent of one step. It would be better to use moveXY instead.


#3

Thank you, ChronistGilver, I had been using var a while ago but after seeing so many places where it was not used, I assumed it wasn’t needed. As for move, I had forgotten it would only be useful in this situation on loop, by itself moveXY works fine. As for beginning my initial variables with var, the code still doesn’t work.

What happens this time, is that all five soldiers are built, and one of them is directed to the base point (50, 40), then nothing else happens except the slow increase of coins and the same error message of: ‘Code never finished. It’s either really slow or has an infinite loop.’

Here is the code where I have replaced things where I thought they should go.


// Each soldier costs 20 gold.
while (this.gold > this.costOf("soldier")) {
    this.summon("soldier");
}

var soldiers = this.findFriends();
soldierIndex = 0;
// Add a while loop to command all the soldiers.
while (soldierIndex < soldiers.length) {
    var soldier = soldiers[soldierIndex];
    this.command(soldier, "move", {x: 50, y: 40});
    soldierIndex + 1;
}
// Go join your comrades!
this.moveXY(50, 40);

I’m pretty sure there is a problem with the soldier variable or the soldierIndex variable. I know that posting on the forums isn’t intended as a walkthrough, so I would be happy with some documentation on how to properly use variables and/or arrays. :smile:


#4

You still haven’t used the var keyword on soldierIndex. Also, the line soldierIndex + 1 doesn’t work. Perhaps you meant soldierIndex++;?


#5

Thank you so much, I wasn’t sure if soldierIndex counted as an array (I guess it’s only when it has square brackets around it) and I have seen the randomVariable++; and I thought it was the same as randomVariable +1; but now I know that isn’t the case, is it used when you need to increment a number in a loop?

Also, my code completely works now and the level has been completed.