how can i do this?
while true do
draw(face1)
wait(1)
draw(face2)
end
when ever i try to use wait() it comes up with an error please help with a script in the simplest way i can do this.
wait()
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Le_juiceBOX
- Citizen
- Posts: 71
- Joined: Sat Mar 26, 2016 3:07 pm
Re: wait()
It's not recommended, but you could tryLe_juiceBOX wrote:how can i do this?
while true do
draw(face1)
wait(1)
draw(face2)
end
when ever i try to use wait() it comes up with an error please help with a script in the simplest way i can do this.
Code: Select all
while true do
draw(face1)
love.timer.sleep(1)
draw(face2)
end
Also, as a side note, when asking for help on the forums, always include the .love file so we can see the source code and better help you
Re: wait()
I wouldn't recommend using love.timer.sleep at all. It'll freeze your game for the duration of it, and it won't show the first face.
What you're probably looking for is a timer. Here's a simple way to do your example:
There are libraries that make the process a little nicer and cleaner looking, though, so you don't have to keep a bunch of timer variables and manage them yourself. A good one to look at is hump.timer, and hump comes with a lot of other goodies too. You can get it from github: https://github.com/vrld/hump
What you're probably looking for is a timer. Here's a simple way to do your example:
Code: Select all
function love.load()
-- load face images
face1 = love.graphics.newImage(...)
face2 = love.graphics.newImage(...)
-- keep track of which face we're drawing
currentFace = face1
-- this variable represents our timer
faceTimer = 0
end
function love.update(dt)
-- add to the timer every frame
faceTimer = faceTimer + dt
-- check if the timer hits the limit
if faceTimer > 1 then
-- reset the timer
faceTimer = 0
-- switch the face
if currentFace == face1 then
currentFace = face2
else
currentFace = face1
end
end
end
function love.draw()
-- draw the current face at x 100 y 100
love.graphics.draw(currentFace, 100, 100)
end
Re: wait()
Kingdaro wrote:I wouldn't recommend using love.timer.sleep at all. It'll freeze your game for the duration of it, and it won't show the first face.
What you're probably looking for is a timer. Here's a simple way to do your example:There are libraries that make the process a little nicer and cleaner looking, though, so you don't have to keep a bunch of timer variables and manage them yourself. A good one to look at is hump.timer, and hump comes with a lot of other goodies too. You can get it from github: https://github.com/vrld/humpCode: Select all
function love.load() -- load face images face1 = love.graphics.newImage(...) face2 = love.graphics.newImage(...) -- keep track of which face we're drawing currentFace = face1 -- this variable represents our timer faceTimer = 0 end function love.update(dt) -- add to the timer every frame faceTimer = faceTimer + dt -- check if the timer hits the limit if faceTimer > 1 then -- reset the timer faceTimer = 0 -- switch the face if currentFace == face1 then currentFace = face2 else currentFace = face1 end end end function love.draw() -- draw the current face at x 100 y 100 love.graphics.draw(currentFace, 100, 100) end
That's what I was gonna recommend, but OP said the simplest way, i figured love.timer.sleep() was the simplest, even if it is also the worst lol
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: wait()
Technically, it's not the same, though the OP didn't really specify what he wanted to do anyway... apart from wait not existing as a function in neither lua nor löve (maybe somewhere else?).
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.
Re: wait()
Bare with me for a second, but you can still get your own wait function in love using coroutines.
In this scenario, whenever the thread is resumed (because I used wrap, you just call it), it'll pickup where it left off with a yield call. That's what the my_wait function will be, the yield calls and a timer.
Of course, that yield function is returning how much time has passed, which it needs to get from somewhere, which is you pass it in as an argument to the thread created by coroutine.wrap
As for the draw function, that's a bit more complicated which I'll hand wave like so:
Hope this helps in a confusing way, or confuses in a helpful way!
Code: Select all
local thread = coroutine.wrap(function()
while true do
draw(face1)
my_wait(1)
draw(face2)
my_wait(1)
end
end)
Code: Select all
function my_wait(time)
while time > 0 do
time = time - coroutine.yield()
end
end
Code: Select all
function love.update(dt)
thread(dt)
end
Code: Select all
to_draw = nil
function draw(img)
to_draw = img
end
function love.draw()
-- do whatever is needed to draw the last image.
end
Re: wait()
He's probably referring to ROBLOX's wait(). ROBLOX scripts all run in a coroutine. http://wiki.roblox.com/index.php?title= ... OBLOX#Waitzorg wrote:Technically, it's not the same, though the OP didn't really specify what he wanted to do anyway... apart from wait not existing as a function in neither lua nor löve (maybe somewhere else?).
Re: wait()
Yea. It might be kinda cool to figure out how to do this in love tho.
Roblox does it by having wait() as a yield to a function, that returns time passed and delta.
However, Roblox has a different version of Lua like Kingdaro mentioned (more optimization, LuaU sidecode language, etc.)
In love, it's more complicated (more classic Lua style). In 99% of game languages, you can't just call "wait()." Efficient programming languages check code every tick, and having a wait() will interrupt the flow.
Run things off of EVENTS. An event can fire for almost anything, so give it a try
Roblox does it by having wait() as a yield to a function, that returns time passed and delta.
However, Roblox has a different version of Lua like Kingdaro mentioned (more optimization, LuaU sidecode language, etc.)
In love, it's more complicated (more classic Lua style). In 99% of game languages, you can't just call "wait()." Efficient programming languages check code every tick, and having a wait() will interrupt the flow.
Run things off of EVENTS. An event can fire for almost anything, so give it a try
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: wait()
One, welcome to the forums.iGottic wrote: ↑Mon Feb 24, 2020 12:44 pm Yea. It might be kinda cool to figure out how to do this in love tho.
Roblox does it by having wait() as a yield to a function, that returns time passed and delta.
However, Roblox has a different version of Lua like Kingdaro mentioned (more optimization, LuaU sidecode language, etc.)
In love, it's more complicated (more classic Lua style). In 99% of game languages, you can't just call "wait()." Efficient programming languages check code every tick, and having a wait() will interrupt the flow.
Run things off of EVENTS. An event can fire for almost anything, so give it a try
Two, luaJIT that löve uses, still has corouties, and if you utilize it efficiently, you can implement wait, delay, whatever with them.
Three, next time, do check the post dates; there's not much chance the OP would see a 4 year old thread.
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests