love.update() vs love.draw()

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.
Post Reply
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

love.update() vs love.draw()

Post by knuxyl »

I'm using a variable to determine which part of the game to be created
status = "startup"
or
status = "menu"
or
status = "game"

Each of these status will point to a file in the functions folder like so

require("functions/menu")
if (status == "menu") then
menu()
end

and this function will create everything needed within that part of the game.

I'm having a little trouble understanding why there is a separate love.update() and love.draw(), when both are called every tick. I can understand this for simplicity in very simple games, but for complex games it would seem to just make things 2000 time more complex than it needs to be if I have to split up calculations and draw functions.

Here is my main.lua

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function love.load()
  require("functions/file")
  require("functions/menu")

  status = "menu"
  
  
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function love.update(dt) 



end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function love.draw()
  if (status == "menu") then
    menu()
  end



end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
and here is my functions/menu.lua

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

menu_check = false

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function menu()
  if (menu_check == false) then
    background = love.graphics.newImage("resources/menu/bg.jpg")
    menu_check = true
  else
    menu_main()
  end
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function menu_main()
  deltatime = love.timer.getDelta()
  love.graphics.print(deltatime,10,10)
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
I plan on just doing the entire menu within the function menu_main() and I want this to be the standard layout for all parts of the game.

So is it nessacery for me to even use the love.update() function at all? I can set the delta time easily with love.timer.getDelta() function so I see no need to put any of my code within love.update(). Again, I see the need for beginners to use it but the more complex the game gets, it seems this separation becomes unnecessary.

So can someone please enlighten me on what this is all about and if I'm looking at this the right way?

Also, I want to be able to set variables only once, not every tick, which is what the if menu_check function is for, but is there a more efficient way to do this?

Thanks!
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

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

Post by MasterLee »

love draw is only called when love.graphics.isActive() is true
see here:
https://love2d.org/wiki/love.run
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

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

Post by knuxyl »

MasterLee wrote: Wed Mar 15, 2017 10:48 am love draw is only called when love.graphics.isActive() is true
see here:
https://love2d.org/wiki/love.run
is this relevant? putting code within love.draw seems to automatically set it then because the code I posted above works, and it's in love.draw.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

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

Post by Tjakka5 »

Lets say you have a game world with a player, a floor, and a enemy that pushes the player around.

Now, let's consider updating and drawing in the same function:
First, we update the player. We listen for keyinput, move it around, apply gravity, etc.
Then we draw the player at that position.

Now we update the enemy.
He moves towards the player, and if there's a collision he pushes the player a few pixels to the side..
Now we draw the enemy.

But wait! Our enemy has just pushed the player, but the player is not drawn at that new position.
This means the player lags behind graphically, and makes the player and enemy sprite overlap.

Now imagine there's even more position influencing objects. Moving platforms, teleporters, etc.
It would get messy real quick.

Not only that, but it also way neater to just do all your calculations first, and THEN draw everything.


Also, I would suggest using something like Hump gamestate, it makes what y ou're doing a lot easier ^^
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

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

Post by MasterLee »

knuxyl wrote: Wed Mar 15, 2017 10:56 am
MasterLee wrote: Wed Mar 15, 2017 10:48 am love draw is only called when love.graphics.isActive() is true
see here:
https://love2d.org/wiki/love.run
is this relevant? putting code within love.draw seems to automatically set it then because the code I posted above works, and it's in love.draw.
That is relevant because that is one of the main differences. Yes your code works but your question was what is the difference. Also when you have an network game you would like to react on network actions and keep the game running even when the window is minimized.
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

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

Post by knuxyl »

MasterLee wrote: Wed Mar 15, 2017 12:14 pm
knuxyl wrote: Wed Mar 15, 2017 10:56 am
MasterLee wrote: Wed Mar 15, 2017 10:48 am love draw is only called when love.graphics.isActive() is true
see here:
https://love2d.org/wiki/love.run
is this relevant? putting code within love.draw seems to automatically set it then because the code I posted above works, and it's in love.draw.
That is relevant because that is one of the main differences. Yes your code works but your question was what is the difference. Also when you have an network game you would like to react on network actions and keep the game running even when the window is minimized.
Ok thanks
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

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

Post by knuxyl »

Tjakka5 wrote: Wed Mar 15, 2017 11:46 am Lets say you have a game world with a player, a floor, and a enemy that pushes the player around.

Now, let's consider updating and drawing in the same function:
First, we update the player. We listen for keyinput, move it around, apply gravity, etc.
Then we draw the player at that position.

Now we update the enemy.
He moves towards the player, and if there's a collision he pushes the player a few pixels to the side..
Now we draw the enemy.

But wait! Our enemy has just pushed the player, but the player is not drawn at that new position.
This means the player lags behind graphically, and makes the player and enemy sprite overlap.

Now imagine there's even more position influencing objects. Moving platforms, teleporters, etc.
It would get messy real quick.

Not only that, but it also way neater to just do all your calculations first, and THEN draw everything.


Also, I would suggest using something like Hump gamestate, it makes what y ou're doing a lot easier ^^
Thank you for the explanation. I'm looking into hump.gamestate. Looks like they have a bunch of libraries, thanks for the link :awesome: :awesome:
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

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

Post by knuxyl »

yeah hump.gamestate was exactly what I was looking for. thanks so much!
BOOST
Prole
Posts: 1
Joined: Sun Mar 19, 2017 5:41 am

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

Post by BOOST »

How do you implement libraries such as hump.gamestate in LOVE? hump looks like it could help a beginner like me figure out how to make multiple "screens".
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

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

Post by s-ol »

BOOST wrote: Sun Mar 19, 2017 5:44 am How do you implement libraries such as hump.gamestate in LOVE? hump looks like it could help a beginner like me figure out how to make multiple "screens".
why don't you have a look at the hump.gamestate documentation? It has examples right there: http://hump.readthedocs.io/en/latest/ga ... -gamestate

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
Post Reply

Who is online

Users browsing this forum: No registered users and 84 guests