Main Menu
  • Welcome to The RPG Maker Resource Kit.

[solved]Can this script be abbreviated?

Started by shintashi, May 04, 2011, 05:17:29 AM

0 Members and 1 Guest are viewing this topic.

shintashi

So I wrote a simple circle that loops +1 from 1 to 6 and starts back at six, nothing special, but it seems to take a lot of space.

@chant_window.fragment = @chant_window.fragment > 5 ? 1 : @chant_window.fragment + 1

I'm just trying to say

if @chant_window.fragment > 5
@chant_window.fragment += 1
else
@chant_window.fragment = 1
end


in as few lines as possible.

TDS

#1
%= should do it, but in your case you might have to add a bit of extra code since it loops back at 0 not to 1.

Something like this.


value = 0
for i in 0...20
  value += 1
  value %= 7
  value = [value, 1].max
  p value
end


Of course you would have to apply it to your own example.


@chant_window.fragment += 1
@chant_window.fragment %= 7
@chant_window.fragment = [@chant_window.fragment, 1].max


The extra code at the end is because your minimum value is 1.

I hope this helps you out.

Have a nice day.

brewmeister

If you want to cycle from 1-6, just change the order...

value = 0
for i in 0..20
  value %= 6
  value += 1
  p value
end

ForeverZero

(0..20).each {|i| value = (i % 6) + 1 }

shintashi


ForeverZero

I was just following the convention of the above posts. Its just an example. You weren't really specific on how many times you need to loop. If you're not looping, and just want to increment by one, and roll over to 1 after hitting six:

@chant_window.fragment = ((1 + @chant_window.fragment) % 6) + 1

shintashi

Quote from: ForeverZero on May 05, 2011, 09:38:32 PM
I was just following the convention of the above posts. Its just an example. You weren't really specific on how many times you need to loop. If you're not looping, and just want to increment by one, and roll over to 1 after hitting six:

@chant_window.fragment = ((1 + @chant_window.fragment) % 6) + 1

that script actually produces a really weird result where every other window is highlighted.

modern algebra

Change it to what FZ had in his first answer and take out the first "1 +". Ie:


@chant_window.fragment = (@chant_window.fragment % 6) + 1