Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

"For" loop questions

Im stillvery new to scripting, so Im sorry for such a newbish question. Ive looked over a number of tutorials and read a lot of posts here but I cant quite find anything that explains the "for" loop command in a way that I understand its use or purpose..

I was hopeing someone could really give me a barney-style explaination, maybe with a simple example or two
 
Try skipping directly to my example; I think being shown makes more sense than being told. I am not liable if I suck at explaining things. :P

For loops generally repeat a segment of code multiple times, increasing a variable each time. Basically it's six parts. For example:

Code:
 1  2  3 4 5     6
for i in 0...array.size

for - You obviously need this to tell the program that it's a for loop.

i - This is the variable that is being increased.

in - Just put it. It's necessary.

0 - The number that i (the variable being increased) should start at.

... - '...' means go up to but not including the number following. '..' means go up to and include the number following.

array.size - This is just an example, but this should be a number that is the maximum (i.e. where the variable should stop being increased).

--- EXAMPLE ---

Say you have some fruits in a bag. In RGSS you'd represent that as an array. Something like:

Code:
         fruits[0] fruits[1] fruits[2]     fruits[3]
fruits = ["Apple", "Orange", "Cantaloupe", "Banana"]

But now we want to make them all plural (i.e. "Apples", "Oranges", etc.) To do that to one item, we would do something like:

Code:
fruits[0] += "s"

But if we just repeat that segment of code, it will just keep adding "s" to the first item in the list, so we'd end up with "Applessss" and the rest would still be singular. :(

So we need a for loop. We need to tell it to do it for item 0, 1, 2, and 3. But instead of writing each line individually, we can do this:

Code:
for i in 0...fruits.size
    fruits[i] += "s"
end

That tells it to do it for fruits where i is any number from 0 up to (but not including) the size of the array (which is 4, since there are 4 fruits).

This is much more helpful if you either have a lot of items (we don't want to write 100 lines of code to add an "s" to our fruits! Cookie to anyone that can actually name 100 fruits. :|) or if you don't know the size of the array.

I hope that made sense.
 
that does help, very clear explaination.

I also dont know arrays and hashes well yet, thats actually what Ive been working on getting down, so thats probobly why "for" was confusing me.

thanks again
 
You don't have to use an array, any range at all will work, and what you do in between can be anything as well, although it is mainly used for arrays.
Code:
for i in 1..5
  print i
end

If you put that somewhere, you'd get a window saying "1", then if you clicked ok you'd get another one saying "2" etc. up to 5.
 
Geminel said:
I also dont know arrays and hashes well yet
Array are basically like a list. For example:

Code:
array = ["Dog", "Cat", "Elephant"]
p array[0] # Shows "Dog"
p array[1] # Shows "Cat"
p array[2] # Shows "Elephant

Array's indexes start at 0. Use array 
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top