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.
How to make a function wait X amount time?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How to make a function wait X amount time?
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
Re: How to make a function wait X amount time?
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.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.
If you really want your code to look like this:
Code: Select all
Immortal = true
sleep(5)
Immortal = false
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
Re: How to make a function wait X amount time?
I've created a library called chrono that can do just that!
To do what you asked with it:
Other libraries that provide similar functionality are kikito's cron and vrld's hump.timer.
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
Re: How to make a function wait X amount time?
Wow, thanks a lot man! Your library is just what I was looking for. Really awesomeadnzzzzZ wrote:I've created a library called chrono that can do just that!
To do what you asked with it:
Other libraries that provide similar functionality are kikito's cron and vrld's hump.timer.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
Re: How to make a function wait X amount time?
As an alternative, coroutines can help.
More on coroutines in Programming In Lua, Chapter 9. http://www.lua.org/pil/9.html
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
- 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?
The idea is that you "remember" how much invincivility time the player has accumulated, until it's greater than the limit.
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 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
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.
- arthurgps2
- Prole
- Posts: 5
- Joined: Tue Jan 31, 2017 9:12 pm
- Contact:
Re: How to make a function wait X amount time?
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
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
- 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?
Congratulations for bumping a 4 year old thread with an unformatted whatever that was unneeded, this may be a record.arthurgps2 wrote:...
Me and my stuff True 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.
- 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?
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Who is online
Users browsing this forum: No registered users and 2 guests