Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> Ruby, how to apply changes on every other number in loops?, ex. for i in 1..20 doing only even or odd numbers.
Titanhex
post Sep 13 2011, 12:17 PM
Post #1


Guru of Water
Group Icon

Group: Revolutionary
Posts: 1,096
Type: None
RM Skill: Masterful
Rev Points: 5




I know how to do this with a loop and if statement, but not with a for iterator.

Anyone able to tell me how to do a loop that can do this?
CODE
for i in min..max
  $num1 = i
  Do Something
end
Where i is an odd or even or whatever number.

The if equivalent would be
CODE
i = 0
loop
i += 2
  $varnum = i
  if i >= 20
   break
  end
end


I'm looking for some smaller version of that code.


__________________________
Go to the top of the page
 
+Quote Post
   
Night5h4d3
post Sep 13 2011, 12:59 PM
Post #2


The past tense
Group Icon

Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed




CODE
for i in min..max
  next if (i % 2) != 0
  $num1 = i
  Do Something
end

for evens, and:
CODE
for i in min..max
  next if (i % 2) != 1
  $num1 = i
  Do Something
end

for odds. This basically jumps to the start of the loop again if i / 2 has a remainder (meaning it's odd) or not (meaning its even).

Alternately, you can do:

CODE
i = min
until i == max
  $num1 = i
  Do Something
  i += 2
end


__________________________
Got 30 minutes? Then you've enough time to play this awesome game:

- potentially promising project page
- thanks holder
My growing space of user-bars:

about me:







I made the following!





Go to the top of the page
 
+Quote Post
   
Titanhex
post Sep 14 2011, 12:20 AM
Post #3


Guru of Water
Group Icon

Group: Revolutionary
Posts: 1,096
Type: None
RM Skill: Masterful
Rev Points: 5




Awesome, thanks. That's what I wanted.

Also is there a way to check every other number without introducing a new variable?


__________________________
Go to the top of the page
 
+Quote Post
   
Night5h4d3
post Sep 14 2011, 05:34 AM
Post #4


The past tense
Group Icon

Group: +Gold Member
Posts: 1,199
Type: Scripter
RM Skill: Undisclosed




Well, with any loops you need a counter variable, like i. So basically, unless you have another variable to use as a loop counter, no. 'i' is used because it wont mess up any other local variable, and if you use it with a for loop, it's scope is only that for loop.

But either way, there's no reason why you should need to worry about introducing i as a loop counter, the act of looping takes more time then the creation of a new variable to count.

Hope that helps.


__________________________
Got 30 minutes? Then you've enough time to play this awesome game:

- potentially promising project page
- thanks holder
My growing space of user-bars:

about me:







I made the following!





Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 21st May 2013 - 07:46 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker