Question about scope
Posted: Mon Apr 20, 2020 6:19 pm
Hello! First time poster here. I've been interested in game development for some time, and I want to give LÖVE a try. I'm familiar with programming, mostly with Python and Javascript, but I'm new to Lua and LÖVE. I'm following Sheepolution's tutorial to get acquainted with the library, and after getting to the chapter about scope, I had an unexpected result. I tried the following:
I expected the program to crash, since text2 doesn't exist in the scope of love.draw(), but to my surprise, it runs fine, even if called before being declared. I tried the following:
which crashed as expected.
My question is: What's the difference between love.update() and something()? Why is the variable created within love.update() global, and can be used even before it's declared?
Code: Select all
function love.load()
text1 = "Hi!"
end
function love.draw()
love.graphics.print(text1,400,300)
love.graphics.print(text2,400,500)
end
function love.update(dt)
text2 = "Something else"
end
Code: Select all
function love.load()
text1 = "Hi!"
end
function something()
text2 = "Something else"
end
function love.draw()
love.graphics.print(text1,400,300)
love.graphics.print(text2,400,500)
end
function love.update(dt)
end
My question is: What's the difference between love.update() and something()? Why is the variable created within love.update() global, and can be used even before it's declared?