This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
raflnatorr's profile picture

Small question about "local success" meaning?

Started by raflnatorr, 23 August 2016 - 08:14 PM
raflnatorr #1
Posted 23 August 2016 - 10:14 PM
I think the title says it all, but i do not understand the - local success, and then a variable -
What does the success mean?
I hope you understand me.

- Roflbobl
Dog #2
Posted 24 August 2016 - 12:12 AM
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).
Bomb Bloke #3
Posted 24 August 2016 - 02:00 AM
http://lua-users.org/wiki/AssignmentTutorial
Lupus590 #4
Posted 24 August 2016 - 03:42 AM
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
Edited on 24 August 2016 - 01:48 AM
raflnatorr #5
Posted 24 August 2016 - 09:01 AM
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

Thanks a lot!
TheRockettek #6
Posted 24 August 2016 - 09:01 AM
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"
Then success will be true as there are no errors in thw code. However if i put in the pcall:
"1+a"
Then success will be false as you cant add a to 1 (Unless you have a variable called a with a number then it will work) so it returns false.


Quick ez summary 4 noobs:
Success means if it works.
It can either be true or false
raflnatorr #7
Posted 24 August 2016 - 09:04 AM
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).

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!
Bomb Bloke #8
Posted 24 August 2016 - 09:55 AM
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.
raflnatorr #9
Posted 24 August 2016 - 02:31 PM
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.

Thanks! I'm really new to programming and lua :)/>