Explain please!

I think I understand the basics of what this is doing (See attached). This is from the While Loop tutorial.

Basically the while loop is giving a random integer with a random number up to 1000. Than the condition is that it doesn’t go over has to be less than 20 total loops so it doesn’t go through infinite loops. For future use though, how does the actual integer variable play into the equation.

Is it considered the “X” in the equation i.e. whatever the math problem needs it to be? I guess obviously, but how will I know I need an integer vs a float or another variable?

Most importantly, when actually developing my own content, how would I implement while loop or looping in general? Opening doors, QTE?

A lot to unpack, so any help appreciated!

Cheers!

While developing you very rarely use loops. The most common one being “ForEach” which loops through all objects of an array because the game itself runs as a loop.
Let me explain this a bit more clearly. Whenever you code something it will do that immediately and everything you do is between frames. You can do substepping but that’s not important for now. So basically you calculate stuff (new positions and variables) then the next frame is rendered with this information and it repeats itself. So everything you could do inside of a loop is stuff which has to happen before the next frame is rendered. For example if you open your inventory you open a widget (or other UI element) and go through each item the player has to get it’s icon and name and place it there. This should happen immediately since you don’t want your inventory to fill up over time (even if it’s just one or two frames). However if you want to open a door you just wait for the next frame, check how long it was since the previous one and move it by the distance it should move per second multiplied by the number you just received to reliably move it Interdependent from your FPS.

To chose your variable type… well just think about what you will need. Is it a level? Go for an int. You won’t be level 3.2 or 3.5 or 3.8 but 3 or 4. HP in a fps with explosions? Maybe go for a float since if you have an explosion in a radius you might want to apply 3.5 damage and not 3 or 4 to be more precise. Is something true or false? Well chose a bool since this is the exact usecase for it… It really comes down to what you need.

And as far as this script goes… Well it creates those integers and then reads them. The generated integer is simply stored inside of the array and then via the “For Each” read out and printed.

I hope this helps.

Cheers :slight_smile: