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

How do I check boolean values?

Started by wrothmonk, 07 April 2012 - 09:10 PM
wrothmonk #1
Posted 07 April 2012 - 11:10 PM
I want to create a redstone-activated computer. Its meant to run a program when a redstone signal is received from the front side of the computer. However I can't seem to figure out the proper way to tell if the boolean value that is returned is true or false.

Example of code I'm using (for this post I'm just naming it "Example"):

while true do
redstone = redstone.getInput("front")
if redstone == true then
shell.run("Program_X")
else
end

error code that pops up: Example:2: attempt to index ? (a boolean value)

what I want to know is how do I do a if statement to detect a boolean value when true/false
EatenAlive3 #2
Posted 07 April 2012 - 11:20 PM
You're doing it correctly, although because of the way CC is set up, you cannot assign the value of redstone to anything and expect your programs to work properly. Use something like:

state = redstone.getInput("front")
if state == true then
  shell.run("program_Y")
else
  os.explode()
end

An interesting thing you can do with boolean values though:

state = redstone.getInput("front")
if state then
  shell.run("program_Z")
elseif not state then
  shell.run("away")
else
  print("This line will never trigger. Ever.")
end
Advert #3
Posted 08 April 2012 - 12:42 AM
You're doing it correctly, although because of the way CC is set up, you cannot assign the value of redstone to anything and expect your programs to work properly. Use something like:

state = redstone.getInput("front")
if state == true then
  shell.run("program_Y")
else
  os.explode()
end

An interesting thing you can do with boolean values though:

state = redstone.getInput("front")
if state then
  shell.run("program_Z")
elseif not state then
  shell.run("away")
else
  print("This line will never trigger. Ever.")
end

You can do that with other stuff, too; the exception being that:

if 0 then
 print("0 is true")
end
if "" then
 print("<empty string> is true")
end

However, most functions return nil if they don't have anything to return.
EatenAlive3 #4
Posted 08 April 2012 - 01:55 AM
You're doing it correctly, although because of the way CC is set up, you cannot assign the value of redstone to anything and expect your programs to work properly. Use something like:

state = redstone.getInput("front")
if state == true then
  shell.run("program_Y")
else
  os.explode()
end

An interesting thing you can do with boolean values though:

state = redstone.getInput("front")
if state then
  shell.run("program_Z")
elseif not state then
  shell.run("away")
else
  print("This line will never trigger. Ever.")
end

You can do that with other stuff, too; the exception being that:

if 0 then
print("0 is true")
end
if "" then
print("<empty string> is true")
end

However, most functions return nil if they don't have anything to return.

Yes, I use those quite frequently in my code. A good example is like this:

function add(x,y,z)
  if not x then x = 0 end -- if a number is not specified,
  if not y then y = 0 end -- then it gets a default value of 0.
  if not z then z = 0 end
  return x + y + z
end
Advert #5
Posted 08 April 2012 - 02:01 AM
You're doing it correctly, although because of the way CC is set up, you cannot assign the value of redstone to anything and expect your programs to work properly. Use something like:

state = redstone.getInput("front")
if state == true then
  shell.run("program_Y")
else
  os.explode()
end

An interesting thing you can do with boolean values though:

state = redstone.getInput("front")
if state then
  shell.run("program_Z")
elseif not state then
  shell.run("away")
else
  print("This line will never trigger. Ever.")
end

You can do that with other stuff, too; the exception being that:

if 0 then
print("0 is true")
end
if "" then
print("<empty string> is true")
end

However, most functions return nil if they don't have anything to return.

Yes, I use those quite frequently in my code. A good example is like this:

function add(x,y,z)
  if not x then x = 0 end -- if a number is not specified,
  if not y then y = 0 end -- then it gets a default value of 0.
  if not z then z = 0 end
  return x + y + z
end

You can shorten that:

function add(x, y, z)
 x = x or 0
 y = y or 0
 z = z or 0
 return x + y + z
end