Page 1 of 2
How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 1:40 am
by Jeeper
I am very new to programming and coming from a "custom map" background in games like SC2. I am currently trying to make a platformer game in Love2d. But I wonder how I can make something wait X amount of seconds before doing the next thing.
Say I want to make the protagonist immortal for 5 seconds, how should that code look like ?
Immortal = true
????????????????
Immortal = false
As I have understood there is no built in wait in Lua nor Love2d.
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 1:52 am
by Qcode
Code: Select all
function love.load()
immortal = true
immortaltimer=0
end
function love.update(dt)
immortaltimer = immortaltimer + dt
if immortaltimer > 5 then
immortal = false
end
end
That should work. You'd adapt it to your own code, of course.
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 8:22 am
by miko
Jeeper wrote:I am very new to programming and coming from a "custom map" background in games like SC2. I am currently trying to make a platformer game in Love2d. But I wonder how I can make something wait X amount of seconds before doing the next thing.
Simple answer: you don't want to stop the lua code execution (so no real sleep()), because love loop has to run at 60 FPS or so, otherwise everything will be frozen and not responding.
If you really want your code to look like this:
Code: Select all
Immortal = true
sleep(5)
Immortal = false
, you could use coroutines for that, like in this
turtle example.
But if you are new to programming, the coroutine concept might be hard to you at start.
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 12:10 pm
by adnzzzzZ
I've created a library called
chrono that can do just that!
To do what you asked with it:
Code: Select all
require 'chrono'
function love.load()
...
immortal = false
chrono = Chrono() -- create main chrono timer object
end
function love.update(dt)
...
chrono:update(dt)
end
function love.keypressed(key)
-- immortal for 5 seconds on p key press
if key == 'p' then
chrono:after(0, function() immortal = true end):after(5, function() immortal = false end)
-- same as this:
-- immortal = true
-- chrono:after(5, function() immortal = false end)
end
end
Other libraries that provide similar functionality are
kikito's cron and
vrld's hump.timer.
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 2:56 pm
by Jeeper
adnzzzzZ wrote:I've created a library called
chrono that can do just that!
To do what you asked with it:
Code: Select all
require 'chrono'
function love.load()
...
immortal = false
chrono = Chrono() -- create main chrono timer object
end
function love.update(dt)
...
chrono:update(dt)
end
function love.keypressed(key)
-- immortal for 5 seconds on p key press
if key == 'p' then
chrono:after(0, function() immortal = true end):after(5, function() immortal = false end)
-- same as this:
-- immortal = true
-- chrono:after(5, function() immortal = false end)
end
end
Other libraries that provide similar functionality are
kikito's cron and
vrld's hump.timer.
Wow, thanks a lot man! Your library is just what I was looking for. Really awesome
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 6:40 pm
by Inny
As an alternative, coroutines can help.
Code: Select all
function love.load()
run = coroutine.wrap(function()
local clock = 5
while clock > 0 do
clock = clock - coroutine.yield(true)
end
-- waited 5 seconds, do your thing here.
end)
end
function love.update(dt)
run(dt)
end
More on coroutines in Programming In Lua, Chapter 9.
http://www.lua.org/pil/9.html
Re: How to make a function wait X amount time?
Posted: Tue Mar 19, 2013 9:30 pm
by kikito
The idea is that you "remember" how much invincivility time the player has accumulated, until it's greater than the limit.
Code: Select all
local player = {isInvincible = false};
function startInvincibility()
player.accumulatedInvincibilityTime = 0;
player.isInvincible = true;
end
function updatePlayer(dt)
if player.isInvincible then
player.accumulatedInvincibilityTime = player.accumulatedInvincibilityTime + dt;
if player.accumulatedInvincibilityTime > 5 then
player.isInvincible = false
end
end
end
function love.update(dt)
updatePayer(dt)
end
function love.draw()
love.graphics.draw("Player invincible: " .. player.isInvincible);
end
function love.onkeypress()
startInvincibility()
end
Notice that this code can be simplified quite a lot if you use a time management library. I have created one called
cron.lua. The code above would become this:
Code: Select all
local cron = require 'cron'
local player = {isInvincible = false};
function startInvincibility()
cron.cancel(player.timer);
player.isInvincible = true;
player.timer = cron.after(5, function() player.isInvincible = false end)
end
function love.update(dt)
cron.update(dt)
end
function love.draw()
love.graphics.draw("Player invincible: " .. player.isInvincible);
end
function love.onkeypress()
startInvincibility()
end
Re: How to make a function wait X amount time?
Posted: Tue Jan 31, 2017 9:46 pm
by arthurgps2
Well,i use [hump.timer] for it,there's a lot of ways to a function wait a x amount of time. I'm gonna show you two of them
First way
timer=require'hump.timer'
timer.after(x,function()
--Action
end)
Second way
timer=require'hump.timer'
timer.script(function(wait)
wait(x)
--Action
end)
So thats it,hope i helped ya ;D
Re: How to make a function wait X amount time?
Posted: Wed Feb 01, 2017 4:50 am
by zorg
arthurgps2 wrote:...
Congratulations for bumping a 4 year old thread with an unformatted whatever that was unneeded, this may be a record.
Re: How to make a function wait X amount time?
Posted: Wed Feb 01, 2017 7:43 pm
by Positive07
Record indeed... We do need that no-necro feature!