Code: Select all
function love.update(dt)
-- Move currentSquare every X seconds
currentSquare = Rectangle:new(math.random(0, love.graphics.getWidth() - 25),math.random(0,love.graphics.getHeight()-25),25,25)
end
Code: Select all
function love.update(dt)
-- Move currentSquare every X seconds
currentSquare = Rectangle:new(math.random(0, love.graphics.getWidth() - 25),math.random(0,love.graphics.getHeight()-25),25,25)
end
Code: Select all
function love.load()
count = 0
updateDelay = ... --set it to the desired delay
end
function love.update(dt)
count = count + dt -- dt is in seconds
if count > updateDelay then
---update logic code goes here
count = 0
end
end
Code: Select all
local currentSquare
timeRemaining = 0.0
TimePerSquare = 0.75
function love.update(dt)
if timeRemaining == 0.0 then
currentSquare = Rectangle:new(math.random(0, love.graphics.getWidth() - 25),math.random(0,love.graphics.getHeight()-25),25,25)
timeRemaining = TimePerSquare
end
timeRemaining = findMax({0,timeRemaining-dt})
end
Code: Select all
function love.load()
count = 0
updateDelay = ... --set it to the desired delay
end
function love.update(dt)
count = count + dt -- dt is in seconds
while count > updateDelay do
---update logic code at fixed rate of "updateDelay"
count = count - updateDelay
end
end
Code: Select all
function love.draw()
-- draw objects at interpolated positions/angles:
-- position = velocity * count
end
Code: Select all
queue = {}
queuetime = 0
function updatequeue(dt)
queuetime = queuetime + dt
for i =#queue,1,-1 do
if queue[i] and (queue[i].time < queuetime) then
queue[i].func()
table.remove(queue,i)
end
end
end
function delay(func,time)
--execute the function "func" after time "time"
table.insert(queue,{time=queuetime+time,func=func})
end
function love.update(dt)
updatequeue(dt)
--add rest of love.update here
end
function love.keypressed()
--example
delay(function()
print("Awesome sauce")
end,5)
--the string "Awesome sauce" will be printed after 5 seconds
end
Thanks for the wonderful advices . I didn't thought of this too much, because I was porting the code as is from a C# implementation.Roland_Yonaba wrote:Hmm..
Well, I don't know what the code behind Rectangle:new()
But, actually, if this is supposed to return a new rectangle each frame, then that's too bad, cause it'll end up eating memory quickly.
Create this rectangle once, then act on his parameters to update it.
Unless the point of your game is too generate a lot of objects.
And use precalculated values. If you don't ever change the game's window height and width, no need to compute love.graphics.getWidth()-25 every update cycle, set it once, then use it.
What's findMax is supposed to do ? Find the maximum value between 0 and timeRemaining-dt ?
Then why passing them as a table ? It'll be safe to use math.max
I didn't account for velocity. This is worth a try, I would try this.ivan wrote:Just a small modification to Roland's example.
You may need to update the game logic multiple times per 'love.update' in order to avoid slowdown.Also, drawing objects becomes a bit more complicated.Code: Select all
function love.load() count = 0 updateDelay = ... --set it to the desired delay end function love.update(dt) count = count + dt -- dt is in seconds while count > updateDelay do ---update logic code at fixed rate of "updateDelay" count = count - updateDelay end end
To avoid stuttering, you have to compensate for the accumulated delta in "count":Code: Select all
function love.draw() -- draw objects at interpolated positions/angles: -- position = velocity * count end
I will definitely check that out.kexisse wrote:The hump library has a Timer class that is perfect for this.
This would be great for a wider scale implementation.T-Bone wrote:You can easily make a timing queue of sorts that can process any number of events at given times. Something like this
Code: Select all
queue = {} queuetime = 0 function updatequeue(dt) queuetime = queuetime + dt for i =#queue,1,-1 do if queue[i] and (queue[i].time < queuetime) then queue[i].func() table.remove(queue,i) end end end function delay(func,time) --execute the function "func" after time "time" table.insert(queue,{time=queuetime+time,func=func}) end function love.update(dt) updatequeue(dt) --add rest of love.update here end function love.keypressed() --example delay(function() print("Awesome sauce") end,5) --the string "Awesome sauce" will be printed after 5 seconds end
Users browsing this forum: Ahrefs [Bot] and 10 guests