Looping Numbers
pHghost
London, UKMember Posts: 2,342
You can use the mod
function to loop through a sequence of integers, like 0-8. Is there any method which would do the same, but you could start at a non-zero point -- eg. 5-17, 200-230?
Comments
I think you should have an attribute (or equation) where you have a zero ?
eg. . . . mod(X,13)+5 . . . or . . . mod(X+1,13)+5 . . . etc
The '0' might be a bit confusing ?
Already deleted my post.
Both of our solutions are wrong.
Edit: more precisely, my solution was wrong, it's still wrong when you include an attribute.
Are you sure ? I'm not at my computer to check, but your approach looks fine to me ?
If X is always rising (let's imagine it's game.time), then mod(X,13)+5 should give you 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 5 - 6 - 7 - 8 . . . etc
?
Yeah, that was my initial thought too. Our solution results in 5 - 10 - 15 - 7 - 12 - ... because of the second addend.
Here's how you do it @pHghost :
For looping x between a and b -> x = mod(x-(a-1),(b-a)+1)+a
There's an example attached under the next post.
That depends on exactly how you have it set up.
For example in my suggestion above (using a rising value like game.time) the values would loop correctly.
What setup results in 5 - 10 - 15 . . etc ?
@pHghost Added Attachment.
@Socks Yes, if you're using game.time or self.time, you're right. But for a more universal solution you need to use your own attribute.
Perfect, thank you guys!
Makes sense and is quite straightforward, once you put the numbers together -- next time I need to be a bit less lazy.
The basic premise behind
mod(X,13)+5
is logically correct (which I was trying previously, but it wasn't working, which led to this thread), but is only halfway to the solution. The thing I was missing was that you need to counteract the +5, which causes a big jump. So, if you want to do +1 steps, you counteract the +5 with a -4, so you get:mod(X-4,13)+5
. The same way, you would counteract the +200 in the second example with -199.The awesome thing about this breakdown (thanks for the formula @Hoodloc!) is that it also answers the second part of my question (which I hand't asked yet). The question was to be: how can you use the same
mod
function to count down in a set range?Well, the answer is easy, you counteract the final offset with a number one higher! So, for example
mod(X-6,13)+5
will count down from 17 to 5.So yeah:
For a counting up loop between a and b ->
x = mod(x-(a-1),(b-a)+1)+a
For a counting down loop between a and b ->
x = mod(x-(a+1),(b-a)+1)+a