A question about For Loops

I didn’t quite know what the best place for this would be but it is pertinent to what is discussed in this lecture.

Can you add larger numbers to For Loops say:

        for(int lifeScore = 3000; lifeScore < currentScore; lifeScore += 3000)
        {
            GainLife();
        }

Once the current score hits the lifeScore it just keeps giving more lives every time the score increases. I added a Debug.Log before “GainLife” and the value of lifeScore does not change every time it loops, in fact it does not change at all. Can you not do something like this with For loops?

What I want to do is that every 3000 points you gain a life. If this is not the way to do it, what would be the best alternative?

Thanks in advance!

Hi,

Did you test your idea? Does it work? If so, it’s a solution. :slight_smile:

Generally, if the compiler does not complain when you write code, you can do [insert your idea]. If your code solves your problem, that’s a different question.

What I want to do is that every 3000 points you gain a life. If this is not the way to do it, what would be the best alternative?

I would not use a for-loop for that. Somewhere in your code, you add points to a variable. When you add points, you could directly check the value of the variable. If it is a multiple of 3000, you add a life. Use an if-statement and look up modulo on DotNetPerls. That should help you implement your idea but this will work only if the player gets exactly 3000, 6000, etc. points. If they don’t, you will have to rethink your logic to come up with a solution for your problem.


See also:

1 Like

Hey Nina,

I did try my idea, and it doesn’t complain however it doesn’t work properly either. It keeps spitting back “lifeScore = 3000” every time it loops (I used a debug log to tell me what the value of it was). So for some reason it is not adding to the value of lifeScore :confused:

If it’s not adding, then it’s likely currentScore is not being reset / adjusted as per - and the loop is not running. Right now the loop runs if lifescore (3000) is less than whatever currentScore is. My guess is currentScore isn’t being adjusted accordingly and therefore the loop isn’t running properly.

For this scenario you could use a for loop, but I’d probably just have a score counter variable - when it gets to 3000 (using an if statement), then call Gain a life, then reset the current “score” counter to 0. Much easier.

What this is actually doing is awarding you with a life for every 3000 points, you have, each time the loop is run. If this is run every frame, then your getting a life every frame once you reach 3000 points. If it’s only run when you gain points, then it checks at that time, and awares +1 life for every 3000 points you have at that instant.

lifeScore will only go up if the loop is run multiple times during the same frame.
If the output you are seeing is over multiple frames, then the loop will reset lifeScore to 3000 every time it is being run.

Overall, I think this will work depending on where you are running it from. If you run it when a player hits a checkpoint, for example, then it will award 1 life for each 3000 points every time you hit a checkpoint. So if you have 4000 points at the 1st checkpoint, 8000 at the 2nd, and 12000 at the 3rd, then you will gain +1 life at the first checkpoint, +2 lives at the second, and +4 lives at the third.

If you are checking every frame though, I don’t think this will get you what you want, as once you reach 3000 points you’ll get getting +1 life, +1 life, +1 life, +1 life, every frame. In this case, you’ll be better incrementing a variable by +3000 each time your score is higher than it, and running GainLife() when you do so. This will prevent you from gaining life again until your score is high enough for the new value.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms