Page 1 of 2

I want to see if one number is divisible by another number

Posted: Thu Nov 10, 2011 11:56 pm
by enderspike
I haven't included a file because I believe my question is rather simple: how do I say "See if X is divisible by Y?"

I'm a noob trying to make a simple flexible timer like

love.update(dt)
timer = timer + dt
...

And then be able to make events happen at various times like

if timer //is divisible by// 3 then
thisis.happening
end

if timer //is divisible by// .5 then
thisis.moreoften
end

Is this real? Should I do this a different way? Help!

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 12:02 am
by TechnoCat
http://en.wikipedia.org/wiki/Modulo_operation

Code: Select all

if number%3==0 then --fixed because i'm stupid
  print("EVENLY DIVISIBLE BY 3!!!")
else
  print("Not evenly divisible by 3.")
end

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 12:42 am
by Ellohir
It's not the modulo operation, but for what you want to do I usually go with this:

Code: Select all

if timerA > 3 then
    A.happening -- every 3 seconds
    timerA = 0
end

if timerB > 0.5 then
    B.happening -- every half second
    timerB = 0
end
It's more code but I like having my counters split.

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 1:00 am
by josefnpat
TechnoCat wrote:http://en.wikipedia.org/wiki/Modulo_operation

Code: Select all

if number%3 then
  print("EVENLY DIVISIBLE BY 3!!!")
else
  print("Not evently divisible by 3.")
end
I think you have it backwards TC: if number%3 == 0, then it is divisible by three. But this is goofy anyway, because timer is a float, not an integer.

It doesn't make much sense why you're trying to figure out if the timer is divisible by three.
  1. If you want to have something execute every third update, then increment a temp var by one, and use TechnoCat's modulo.
  2. If you want something to happen over a period of time (let's say 1/3 of a second) then you need to keep adding up the dt's in a temp variable. When the variable is > 1/3 seconds, execute your command.

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 2:06 am
by TechnoCat
josefnpat wrote:I think you have it backwards TC: if number%3 == 0, then it is divisible by three.
Disregard me, I suck cocks. 8==> :o

In my defense I wrote it on the way out to the gym.

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 3:32 am
by Tesselode
How to find out if one number is divisible by another:

if n1/n2 = math.floor(n1/n2) then
--it is divisible

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 4:42 am
by slime
Tesselode wrote:How to find out if one number is divisible by another:

if n1/n2 = math.floor(n1/n2) then
--it is divisible

Code: Select all

 if n1 % n2 == 0 then ...
......

As was already pointed out though, the OP shouldn't be finding out if a number is divisible, in this circumstance. :P

Re: I want to see if one number is divisible by another numb

Posted: Fri Nov 11, 2011 7:31 am
by Robin
So, enderspike: the answer to your question is Technocat's updated answer, but what you really want is Ellohir's answer.

Re: I want to see if one number is divisible by another numb

Posted: Tue Nov 15, 2011 6:55 pm
by T-Bone
Also, if you want something to happen while the timer is in between a number divisible by three and the next, you can do this

Code: Select all

if math.floor(number)%3 == 0 then
    --whatever
end
number = number + dt
But it's stupid.

Re: I want to see if one number is divisible by another numb

Posted: Tue Nov 15, 2011 9:49 pm
by kikito
enderspike wrote:I'm a noob trying to make a simple flexible timer like ...
Howdy!

Some time ago I made a lib just for that - cron.lua.

Here's how you install it:

Code: Select all

local cron = require 'cron'
...
function love.update(dt)
  cron.update(dt) -- you must put this line inside love.update
end
And here is how you use it to do the two examples you gave above:

Code: Select all

cron.every(3, function() thisis.happening end)
cron.every(0.5, function() thisis.moreoften end)
These calls can come from love.update, love.load, or any other place where the cron variable is defined.

You can use regular functions instead of anonymous ones:

Code: Select all

function thisis_happening()
  -- do stuff
end

cron.every(3, thisis_happening)
And you can also pass parameters to the functions, call events only once, and cancel events. I'm very happy with the result, please give it a look.