How to make a function wait X amount time?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

How to make a function wait X amount time?

Post 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.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: How to make a function wait X amount time?

Post 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.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to make a function wait X amount time?

Post 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.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: How to make a function wait X amount time?

Post 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.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: How to make a function wait X amount time?

Post 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 :)
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: How to make a function wait X amount time?

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to make a function wait X amount time?

Post 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
When I write def I mean function.
User avatar
arthurgps2
Prole
Posts: 5
Joined: Tue Jan 31, 2017 9:12 pm
Contact:

Re: How to make a function wait X amount time?

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make a function wait X amount time?

Post by zorg »

arthurgps2 wrote:...
Congratulations for bumping a 4 year old thread with an unformatted whatever that was unneeded, this may be a record.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: How to make a function wait X amount time?

Post by Positive07 »

Record indeed... We do need that no-necro feature!
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests