Page 1 of 1

How do I use % again?

PostPosted: Mon Mar 25, 2013 3:20 am
by Hblade
Alright, so recently for some reason I'm unable to be satisfied with just any code. It seems like it has to be small and efficient as possible. My current goal is to add something by 1 ever 5 or 10 frames, I think theres a way to do this in 1 line using % symbol. how?

Re: How do I use % again?

PostPosted: Mon Mar 25, 2013 4:02 am
by bamby1983
What is it that you need to add by 1? An integer?

Would you settle for 2 lines of code instead of 1?

Code in the "view" actor's "Draw" event:
Code: Select all
if (frame%10==0) // This is true once every 10 frames
    x+=1; // Increments x by 1


If you absolutely must have just a single line of code, you could try the following (although it will most likely be less efficient, less understandable and hence less maintainable):

Code: Select all
x+=(int)((min(frame%10,0.5)-0.5)*(-2));


Both codes are tested and work. The attached GED file shows how the numbers stack up for each method as compared to the game's frame count.

Re: How do I use % again?

PostPosted: Mon Mar 25, 2013 6:10 am
by lcl
Hblade, here's a quick explanation of what % does. This should help you understand how it works.

What % means is the remainder of division.
For example, if we write 304%3 the result will be 1.
Why is that? Because % checks how many times the whole 3 goes to the 304,
which is 101 times, and 101 * 3 = 303, so there is one left as a reminder.
And that reminder is what a calculation with % returns.

I hope this helps, see the PM I sent you, too! :)

Re: How do I use % again?

PostPosted: Mon Mar 25, 2013 6:45 am
by Hblade
Thanks guys! :D And oh lcl xD I kind of replied before I seen this o//o