I have been completely mind baffled by this for quite some time, could someone briefly explain as simply as they can what they mean and do?
local
k, v in ipairs
Programming in Lua
Programming in Lua
Programming in Lua
Lua 5.1 Reference Manual
Thankyou!
I'll quickly explain for anyone that searches and ends up here (Somehow)
Putting local behind a variable before defying it will turn it into a LOCAL variable, which means that it will be used only inside the context it was set in. If it was set inside an "if" statement then it will be usable only inside there, if it's set inside of a script in the first line, then anywhere in that script you can use it, but not outside of that script.
Here's some examples:
local thingy = "Hey! I'm a local variable!"
if(true) then
print(thingy)
end
This defines the variable "thingy" before an if statement, and inside of it we call the variable. This will print: "Hey! I'm a local variable!"
if(true) then
local thingy = "Hey! I'm a local variable!"
print(thingy)
end
print(thingy)
This here defines it inside of that "if" statement and as a local variable it will be usable only inside of it. The first print will print: "Hey! I'm a local variable!" but the second print will return an error because it couldn't find what the variable "thingy" is.
if(true) then
thingy = "Hey! I'm a world variable!"
end
print(thingy)
Here we didn't put the local behind it when defying the variable, meaning it's now a world variable. This means it can be used ANYWHERE, even in other scripts, which is a bad thing. If you use common names like "ply" or "hp" for world variable names, then it's possible other people named them like that and it will interfere with each other. If you run this script it will print "Hey! I'm a world variable!" even if you did define it inside of an "if" statement.
For the second one, that one is used with tables. Tables are variables that contain other variables, usually we use "for k,v in pairs" to go through all the variables inside of the table. In fact, that K stands for "key" and that V stands for "value" (or "variable"). They are LOCAL variables that we can use only inside of the "for" loop we're about to call. It's called a "loop" because it keeps repeating the code you put inside of it for every variable inside the table.
local table = {"hello", "my", "name", "is", "doodlebob"}
for k,v in pairs(table) do
print(k)
print(v)
end
This will print like this:
1
hello
2
my
3
name
4
is
5
doodlebob
All in one frame.
We print the K which represents the Key, which is the position the V is inside of the table (starting from 1) and then we print the V which is the Variable defined inside of the table.
One last thing: people are used to doing k,v but you can use any at all, some people prefer naming the Key value _ (For some reason?). Here's an example of the same script as before, but with different names.
for _,theString in pairs(table) do
print(_)
print(theString)
end
I was wondering about this too. I found that _ is common in lua, which is used to basically ignore variables. For instance
local _,theVariableIWant = sciencyFunction()
If the first variable of the function is to be ignored, the _ syntax makes sense. If the second variable is to be ignored, then it's better to just set the first. E.G.
-- Don't do this:
local blah, _ = SomeFunction()
-- Do this:
local blah = SomeFunction()
To add on that: Don't be afraid to omit variables when applicable.
Hooks
(As seen above)
Pairs loops
For the longest time I thought I had to include every single argument. It took me a while, and I started using underscores. Then I just omitted the variables entirely. I might just be an idiot or a psychopath but I think it may have improved my performance. I think I'm just crazy.
Sorry, you need to Log In to post a reply to this thread.