love.update() and love.draw()

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

love.update() and love.draw()

Post by Dr.Tyler O. »

This is a question that has been nagging at my curiosity for ages. I want to know if there are any cons in using love.draw for things that require updating instead of love.update.

For example:

Code: Select all

foo = math.random(0, 10)
Let's say I have a variable that needs to be updated to a random number every frame. Are there any cons in putting that variable update inside love.draw instead of love.update besides the idea of keeping your code organized?
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: love.update() and love.draw()

Post by markgo »

Your game should work normally without drawing anything. If you put your random code or game logic inside of love.draw, then you are not longer separating concerns.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: love.update() and love.draw()

Post by Positive07 »

love.update has dt passed to it which is an advantage, otherwise you would need to use love.timer.getDelta which implies calling a function and doing some other stuff, this is not really a problem is not like your game will freeze because you call this function.

The other benefit as you pointed out is being organized with your code

For anything else.... they are the same... if you have more doubts on it I recommend you take a look at [wiki]love.run[/wiki]
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: love.update() and love.draw()

Post by micha »

In case you didn't know: You can have a look at LÖVE's game loop in love.run.

Sticking to the update/draw separation is good practice and LÖVE's game loop has built it in by default so that beginners learn that right away. If you have a good reason to not separate updating and drawing, go ahead and do it! You may even change the overall game loop. Besides that, there is no 'hidden' disadvantage of mixing up updating and drawing.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: love.update() and love.draw()

Post by s-ol »

Dr.Tyler O. wrote:This is a question that has been nagging at my curiosity for ages. I want to know if there are any cons in using love.draw for things that require updating instead of love.update.

For example:

Code: Select all

foo = math.random(0, 10)
Let's say I have a variable that needs to be updated to a random number every frame. Are there any cons in putting that variable update inside love.draw instead of love.update besides the idea of keeping your code organized?
No delta time.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: love.update() and love.draw()

Post by nfey »

Well, this is really interesting. I didn't stumble upon that wiki page until now.
I have one question tho: what's the purpose of the sleep() call at the end of the while loop?

Code: Select all

if love.timer then love.timer.sleep(0.001) end
Doesn't this pretty much hardcode a thousandth of a second's worth of lag in each frame?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.update() and love.draw()

Post by zorg »

nfey wrote:...Doesn't this pretty much hardcode a thousandth of a second's worth of lag in each frame?
yes, but more importantly, it will prevent your love game from eating a whole cpu core just by doing nothing;
though you can indeed optimize that, and if i recall correctly, the vsync being turned off or on has some effects as well; there have been at least one other thread about this.
Last edited by zorg on Fri Jan 23, 2015 7:40 pm, edited 1 time in total.
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.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: love.update() and love.draw()

Post by Muris »

Without the sleep, the love-game would use 100% cpu.
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: love.update() and love.draw()

Post by nfey »

Interesting. I'll try to dig up the other thread Zorg is telling us about.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.update() and love.draw()

Post by zorg »

Sorry for not linking it earlier, was busy
here's the one i was thinking about
and here's another
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests