Jump to content




How do I check boolean values?


  • You cannot reply to this topic
4 replies to this topic

#1 wrothmonk

  • Members
  • 33 posts

Posted 07 April 2012 - 10: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

#2 EatenAlive3

  • New Members
  • 53 posts

Posted 07 April 2012 - 10: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


#3 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 07 April 2012 - 11:42 PM

View PostEatenAlive3, on 07 April 2012 - 10:20 PM, said:

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.

#4 EatenAlive3

  • New Members
  • 53 posts

Posted 08 April 2012 - 12:55 AM

View PostAdvert, on 07 April 2012 - 11:42 PM, said:

View PostEatenAlive3, on 07 April 2012 - 10:20 PM, said:

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


#5 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 08 April 2012 - 01:01 AM

View PostEatenAlive3, on 08 April 2012 - 12:55 AM, said:

View PostAdvert, on 07 April 2012 - 11:42 PM, said:

View PostEatenAlive3, on 07 April 2012 - 10:20 PM, said:

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users