Hey fellow developers, I’ve got an issue. I can’t understand another piece of code, seems like that happens everyday now… Anyway, where is the argument is coming from in this function, how does the game know that the argument directly represents color? I am so confused… While yes, I do get the fact that the function loops through the “fruitTable” and returns everysingle fruit name, but the argument for the function and the function itself, like how does it run? There is no “getFruitByColor()”. So ye…
Here is the script:
local fruitName = "Chocolate"
local fruitTable = {
Lemon = "Yellow",
Apple = "Red",
Orange = "Orange"
}
local function getFruitByColor(color) -- Can't understand where this comes from...
local fruitName
for key, value in fruitTable do
if value == color then
fruitName = key
end
end
return fruitName
end
local fruit = getFruitByColor("Yellow")
print(fruit) -- Lemon
print(fruit .. ", not " .. fruitName) -- Lemon, not Chocolate
It doesn’t, you are supplying an argument to the function when you call it with its name. it sees the argument as a string, not a colour. Then it will try and match it up with the value in your dict. That’s all it really does
As for scope, I’ll put it to you like this
Imagine a variables inside your script like this
local x = 2
The local is declaring that x = 2 can be used in that section of the script (ie. the whole script)
Now imagine you have a function called prin()
local function prin()
local x = 5
print(x)
The local in the function is declaring that x = 5 can be used in the scope of that function, which is just its body, the code you can find in it
print(x) — 2
prin() — 5
When you print the x in the scope of the script in the scope of the script, it prints 2 because that is its value. When you print x in the scope of the function, it prints 5 because the scope of that x is in the function, not the script (ie. the local in the function is sort of declaring a new x, something that’s overriding the previous declaration)
Ok, but the local function can still use the “local x = 2” variable right? I believe we call that an upvalue? Anyways, what if you make the function global, not local, then it won’t work and the function will just assign a new value to x?
Like i said earlier, the function is sort of declaring a new x when you use local. If you didn’t, then you would be referencing the x defined at the top of the script. The local variable literally sets a rule for that variable to be used in the scope of the body that it’s in
Correct. The “Yellow” argument is just a piece of information being supplied to the function. If not defined anywhere else, it is to be used only in the scope of the function
O I understand now where the “color” argument comes from! It’s from this line:
local fruit = getFruitByColor("Yellow")
the fruit variable holds the returned item aka lemon and you can see that in the parenthesys it feeds the “yellow” and then the “yellow” is mentioned as “color”. OH, that makes sense, so the script does work it’s that I’m just blind. Anyway, thanks for helping!