Stuck with local variables
Posted: Mon Dec 30, 2013 11:22 pm
Hi guys,
I keep reading everywhere that local variables are a million times better than globals. But my problem is that when a project is starting to get bigger, or is designed to get bigger in the future, I just don't know how to use them anymore.
I like to declare a bunch of tables in main.lua, outside any function, just at the top. I can make them all local and they can be accessed anywhere in main.lua then. But when I add code in a separate file and require or love.filesystem.load them into main.lua, they're all useless.
For example, I have a game with a couple of different levels. Every level I have in a separate lua file, and require it into main.lua whenever needed. But then I cannot access my local player.x and player.y variables in the level files anymore. Of course I can pass these as arguments, but that's going to be a long list of arguments. In love.keypressed, I would then have something like:
Well, then I can be proud of using only locals, but I'd rather use globals than to have to code like that.
Is there no way around this? Why does Lua even behave this way? To me, this:
is equivalent to this:
But Lua disagrees. Even though I require in scope main.lua, the file contents are kept hostage in an alternate universe, with no hierarchical relation to the scope it's called in. Why?
So, what do I do? Just screw it all and use globals anyway, because they're a million times easier to use (and seem to have no negative impact on FPS either, and as I often read: don't optimise unless you have to), or bend my spine in 5 directions, because locals are 'better'?
I keep reading everywhere that local variables are a million times better than globals. But my problem is that when a project is starting to get bigger, or is designed to get bigger in the future, I just don't know how to use them anymore.
I like to declare a bunch of tables in main.lua, outside any function, just at the top. I can make them all local and they can be accessed anywhere in main.lua then. But when I add code in a separate file and require or love.filesystem.load them into main.lua, they're all useless.
For example, I have a game with a couple of different levels. Every level I have in a separate lua file, and require it into main.lua whenever needed. But then I cannot access my local player.x and player.y variables in the level files anymore. Of course I can pass these as arguments, but that's going to be a long list of arguments. In love.keypressed, I would then have something like:
Code: Select all
function love.keypressed(key, unicode)
if game.state == "level1" then
level1.keypressed(key, unicode, player, mouse, window, colour, timer, flags, fade, volume)
end
end
Is there no way around this? Why does Lua even behave this way? To me, this:
Code: Select all
local x = 5
local function doublex()
return x * 2
end
print(doublex())
Code: Select all
--main.lua
local x = 5
local doublex
require 'filewithlotsofusefulfunctions.lua'
print(doublex())
--filewithlotsofusefulfunctions.lua
function doublex()
return x * 2
end
So, what do I do? Just screw it all and use globals anyway, because they're a million times easier to use (and seem to have no negative impact on FPS either, and as I often read: don't optimise unless you have to), or bend my spine in 5 directions, because locals are 'better'?