Love2D: what is the difference between creating variables inside love.load instead of the top of main.lua?

If you look at the docs for love.load , then it says

This function is called exactly once at the beginning of the game.

and nothing else. It also has one parameter, which is a command line argument.

So, if you are not using arguments, what is the difference between:

x = 5

-- rest of code

and

function love.load()
  x = 5
end

-- rest of code

The biggest advantage to prevent love.loadis that you can do xlocal instead . Are there any benefits to using love.load?

+4
source share
3 answers

, - (, , ), , love.graphics , love.load, , .

+1

main.lua Love love.run, . love.run - love.load(...), ... , .

, love.load main.lua, love.draw, love.update, .

, .

+1

Like you yourself, love.load () is run only once at the beginning and is therefore ideal for initializing variables. Anything installed outside this function may load more often.

Try creating a conf.lua file and activating the console. Then type () something outside of love.load () and you will see.

-- conf.lua
function love.conf(t)
  t.console = true
end

-- main.lua
print("Hello World!")
function love.load()
end

For me, he always printed Hello World! twice when starting the game.

As Pavel Kulchenko said, this may not affect simple values, but you will never know ...;)

0
source

Source: https://habr.com/ru/post/1658027/


All Articles