Code: Select all
local number = 0
local vision = true
function love.load()
end
..blah blah...
Code: Select all
local number = 0
local vision = true
function love.load()
end
..blah blah...
Code: Select all
-- FILE NAMED main.lua
print(foo) -- This will print nil because foo is not set
local foo = "bar" -- I am declaring foo locally - it can be accessed anywhere in the main.lua file
print(foo) -- This will print bar
function somecode()
print(foo) -- This will also print bar when I call the function
end
somecode() -- This will print bar
require "include" -- This will print nil because the local variable doesn't carry over to the other file.
-- NEW FILE named include.lua
print(foo) -- This will print nil when I require "include", because foo is not set here.
Code: Select all
-- FILE NAMED main.lua
print(foo) -- This will print nil because foo is not set
function somecode()
local foo = "bar" -- This sets foo only inside the function or any subfunction that I make of this
print(foo) -- This will print bar when I call the function
end
somecode() -- This will print nil because the local variable doesn't carry outside the file
require "include" -- This will print nil because the local variable doesn't carry over to the other file.
-- NEW FILE named include.lua
print(foo) -- This will print nil when I require "include", because foo is not set here.
Code: Select all
-- FILE NAMED main.lua
foo="bar" -- If there's no local, then it's global. I could put this anywere, even inside a function, and it would have the same effect
print(foo) -- This will print bar
function somecode()
print(foo) -- This will print bar when I call the function
end
somecode() -- This will print bar
require "include" -- This will print bar because the global variable carries over.
print(_G.foo) -- Bonus: If you declare a variable locally, it will be accessible in the _G table, so this will print bar
print(_G["foo"]) -- Same as the previous line
-- NEW FILE named include.lua
print(foo) -- This will print bar when I require "include", because foo is set here.
Ohhh. Okay. Thank you for your help!iggyvolz wrote:Yes, what you're doing is completely correct.
What I meant with the _G stuff is that anything you define as a "global" will automatically be put into the _G table for you.
Code: Select all
local nilValueToDetermineLater -- That's it. You can do "= nil" if you like, though.
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest