The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: shintashi on May 04, 2011, 05:17:29 AM

Title: [solved]Can this script be abbreviated?
Post by: shintashi on May 04, 2011, 05:17:29 AM
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.
Title: Re: Can this script be abbreviated?
Post by: TDS on May 04, 2011, 05:30:25 AM
%= 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.
Title: Re: Can this script be abbreviated?
Post by: brewmeister on May 04, 2011, 01:02:55 PM
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
Title: Re: Can this script be abbreviated?
Post by: ForeverZero on May 05, 2011, 12:24:15 AM
(0..20).each {|i| value = (i % 6) + 1 }
Title: Re: Can this script be abbreviated?
Post by: shintashi on May 05, 2011, 08:34:18 PM
whats the 0..20 for?
Title: Re: Can this script be abbreviated?
Post by: 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
Title: Re: Can this script be abbreviated?
Post by: shintashi on May 05, 2011, 09:56:13 PM
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.
Title: Re: Can this script be abbreviated?
Post by: modern algebra on May 05, 2011, 10:49:11 PM
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