How do I use % again?

Non-platform specific questions.

How do I use % again?

Postby Hblade » Mon Mar 25, 2013 3:20 am

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?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How do I use % again?

Postby bamby1983 » Mon Mar 25, 2013 4:02 am

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.
Attachments
Increment.ged
(1.97 KiB) Downloaded 99 times
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: How do I use % again?

Postby lcl » Mon Mar 25, 2013 6:10 am

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! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: How do I use % again?

Postby Hblade » Mon Mar 25, 2013 6:45 am

Thanks guys! :D And oh lcl xD I kind of replied before I seen this o//o
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron