Page 1 of 1

love.update() vs love.draw()

Posted: Wed Mar 15, 2017 10:42 am
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!

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

Posted: Wed Mar 15, 2017 10:48 am
by MasterLee
love draw is only called when love.graphics.isActive() is true
see here:
https://love2d.org/wiki/love.run

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

Posted: Wed Mar 15, 2017 10:56 am
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.

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

Posted: Wed Mar 15, 2017 11:46 am
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 ^^

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

Posted: Wed Mar 15, 2017 12:14 pm
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.

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

Posted: Wed Mar 15, 2017 12:20 pm
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

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

Posted: Wed Mar 15, 2017 12:23 pm
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:

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

Posted: Wed Mar 15, 2017 1:49 pm
by knuxyl
yeah hump.gamestate was exactly what I was looking for. thanks so much!

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

Posted: Sun Mar 19, 2017 5:44 am
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".

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

Posted: Sun Mar 19, 2017 1:17 pm
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