What does the success mean?
I hope you understand me.
- Roflbobl
local success, data = pcall(peripheral.getNames)
local example = true --# this is only available in this file
--#-----------------
if condition then
local example = true --# this is only available in this if block
end
example = true --# this will create a new variable in the global namespace
--#-----------------
if condition then
local example = true --# this is only available in this if block
end
local example = true --# again different variable than the one in the if, but now I'm not polluting the global namespace
--#---------------
while condition then
local example = true --# this works in loops too
example = 3 --# you only use local when declaring the variable, usage is as normal
print(tostring(example))
end
If it's the local that is confusing you then I'll try to explain:
When you create a variable without declaring it local it is available to every program on the computer, unless it is needed by another program this considered messy code.
However, the local keyword restricts the variable to the current scope. You can read about scope here but I'll summarise with an example:local example = true --# this is only available in this file --#----------------- if condition then local example = true --# this is only available in this if block end example = true --# this will create a new variable in the global namespace --#----------------- if condition then local example = true --# this is only available in this if block end local example = true --# again different variable than the one in the if, but now I'm not polluting the global namespace --#--------------- while condition then local example = true --# this works in loops too example = 3 --# you only use local when declaring the variable, usage is as normal print(tostring(example)) end
A snippet of example code would go a long way in explaining what you mean.
Do you mean something like this?local success, data = pcall(peripheral.getNames)
If that's what you are referring to, both success and data are variables (each capturing different data being returned by the call…in this case a protected call). In the example I gave above the pcall would return true or false AND the data from peripheral.getNames or an error message, so two variables are used (success captures the true/false and data captures the returned data or error message).
The success means if the code worked or not and will return a bootlean (true/false). If the current code in the pcall would work, such as a simple:
"1+1"
arhhh okay.. I used turtle.detectDown() and it gives two sets of data. the first one being "true/false" which is given the variable of "succes", and then the blackname/metadata which is given the variable of "data". Thanks a lot!
The success means if the code worked or not and will return a bootlean (true/false). If the current code in the pcall would work, such as a simple:
"1+1"
That's too simple - you can't define a function like that. :P/>arhhh okay.. I used turtle.detectDown() and it gives two sets of data. the first one being "true/false" which is given the variable of "succes", and then the blackname/metadata which is given the variable of "data". Thanks a lot!
So if there's a block underneath the turtle, "success" will be true and "data" will point to a table holding information about it. If there isn't (eg there's air there instead), "success" will be false and "data" will be nil.