Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

is this a global or local variable?

Asked by 4 years ago
Edited 4 years ago

So I understand global and local variables, but if I do this

local function a()
    b = 5
end

print(b) --prints nil

b doesn't have a local before it, but it isn't available outside of the function. Does that mean that variables that are declared inside of a function, even if they don't have a local before them, are local?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You have just defined the function telling it what to do, but you never called the function. Also yes, it is a global variable. Functions never run automatically, you have to call them your self. Once you tell the function what to do, call it so that it executes.

local function a()
    b = 5
end

a() -- Calls the function

print(b) --prints 5
1
Oh I see what you're saying. So basically the variable is waiting to be defined which happens once you call the function, thanks! JustHerezForQuestion 7 — 4y
Ad
Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

So local variables are variables that are local to the function. Global Variables are variables that can be used in multiple functions in the script.

So in this example:

local GlobalVariableNameBlah


local function RandomFunctionName()
    local LocalVariableNameBlah
end

GlobalVariableNameBlah is a global variable and can be changed from anywhere in the script. However LocalVariableNameBlah is only alive as long as the function is running. And only things inside that function can access that variable.

So for example:

If I wanted to change the Global Variables value I could do it inside the function. Also note that I could have made it a string or a bool. I just chose an integer.

local GlobalVariableNameBlah


local function RandomFunctionName()
    local LocalVariableNameBlah
    GlobalVariableNameBlah = 20
end

I can't change the Local variable outside of this function in another function. So for example

local GlobalVariableNameBlah


local function RandomFunctionName()
    local LocalVariableNameBlah
    GlobalVariableNameBlah = 20
end

local function OtherFunctionName()
    -- This would be wrong because it only exists in RandomFunctionName
    -- And is deleted as soon as the RandomFunctionName ends.
    -- This would crash it.
    LocalVariableNameBlah = 20 

    -- I can call the global variable though
    GlobalVariableNameBlah = 30
end

It's also important to note that these functions would not run unless I say below them

RandomFunctionName()
OtherFunctionName()

If they were non local functionsI could call them above them like so:

local GlobalVariableNameBlah

RandomFunctionName()
OtherFunctionName()

function RandomFunctionName()
    local LocalVariableNameBlah
    GlobalVariableNameBlah = 20
end

function OtherFunctionName()
    -- This would be wrong because it only exists in RandomFunctionName
    -- And is deleted as soon as the RandomFunctionName ends.
    LocalVariableNameBlah = 20 

    -- I can call the global variable though
    GlobalVariableNameBlah = 30
end

Anything declared inside a function is local yes. Hope this answered your question.

0
Thank you a lot for taking your time to write this! Although not entierly correct from my understanding. Global variables aren't just accessed by everwhere from the script, they are also a member of _G. in your script "local GlobalVariableNameBlah" is not actually a global, it's true that it's accessed everywhere, but it's not a global since it is prefixed with a local, it is technically local to JustHerezForQuestion 7 — 4y
0
the main chunk. You can try printing _G.GlobalVariableNameBlah and you'll see that it's nil. This is a good explanation although it doesn't answer my question, I was wondering if b in my example was a global variable or not. It is not prefixed with a local but it can only be accessed inside of that function. I wish I can upvote your answer but sadly I can't! JustHerezForQuestion 7 — 4y
0
Yeah I didn't mean global as in _G that's different lol. SethHeinzman 284 — 4y
0
It's a global variable in the script, I was going to say it's kind of local to the script but i just figured nah. Glad it helped alittle. "b" is not a global variable. And I don't think it would even work since you need local in front of it. Not sure though, in other languages you don't but lua is weird. SethHeinzman 284 — 4y

Answer this question