Jump to content




[Lua][Error]open:19: attempt to call nil


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

#1 LemonyLime

  • Members
  • 11 posts
  • LocationOregon

Posted 08 April 2013 - 09:24 AM

state = 0
active = 1

while ( active == 1 ) do
  if ( state == 1 ) then
    io.write( "Door is Open.\n" )
    else
	  io.write( "Door is Closed.\n" )
  end
 
  io.write( "Enter command(-1 to exit): " )

  state = read()
 
  if ( state == -1 ) then
    active = 0
    else
      redstone.setoutput( "right", state ~= 0 )
  end

end


This is just a simple program that sends a redstone signal to open a piston door. 0 = door shut, -1 = close program, anything else = door open.
it doesn't think that redstone.setoutput() is an actual function... I checked the apis folder of the computer, and redstone wasn't there, but the default program redpulse works, using the exact same function I am. What's going on?

#2 SadKingBilly

  • Members
  • 160 posts

Posted 08 April 2013 - 09:26 AM

"redstone.setOutput()"?

Lua does not differentiate between equal and identical like PHP and JavaScript, so doing "string == int" will always return false. And Lua does not enclose conditions in brackets. Anyway, here is the fixed code:
state = 0
active = 1
while active == 1 do
	if state == 1 then
		io.write( "Door is Open.\n" )
	else
		io.write( "Door is Closed.\n" )
	end
	io.write( "Enter command(-1 to exit): " )
	state = read()
	if state == "-1" then
		active = 0
	else
		redstone.setOutput("right", state ~= "0")
	end
end


#3 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 08 April 2013 - 09:26 AM

its rs.setOutput also

#4 Kilobyte

  • Members
  • 122 posts

Posted 08 April 2013 - 10:42 AM

View PostTheCoryKid, on 08 April 2013 - 09:26 AM, said:

And Lua does not enclose conditions in brackets.

Yes, however its still valid syntax. (at least for if or while)

#5 QuantumGrav

  • Members
  • 36 posts
  • LocationUnited States

Posted 08 April 2013 - 11:26 AM

I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:
if ( state == -1 ) then
    active = 0
    elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!

#6 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 08 April 2013 - 11:29 AM

View PostQuantumGrav, on 08 April 2013 - 11:26 AM, said:

I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:
if ( state == -1 ) then
	active = 0
	elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!
he used strings for numbers, this wont work unless you quote the numbers
and why are you setting it to state?
anything nonfalse and nonnil will be true .-.

#7 SadKingBilly

  • Members
  • 160 posts

Posted 08 April 2013 - 11:35 AM

View PostPixelToast, on 08 April 2013 - 11:29 AM, said:

View PostQuantumGrav, on 08 April 2013 - 11:26 AM, said:

I think you're problem is with putting 'state ~= 0' in your redstone.setOutput call. I don't think that should work, but what would work and still do what I think you're trying to to do would be:
if ( state == -1 ) then
	active = 0
	elseif state ~= 0 then
	  rs.setOutput( "right", state )
  end
I might be misunderstanding what you want your code to do, so this may or may not suit you.
I would also try capitalizing the O in redstone.setoutput. That might also fix it.

Try those things and it should work!
he used strings for numbers, this wont work unless you quote the numbers
and why are you setting it to state?
anything nonfalse and nonnil will be true .-.
He probably just doesn't realize that the second argument of redstone.setOutput() is a bool. He'd be entirely right if the second argument were a string.

#8 LemonyLime

  • Members
  • 11 posts
  • LocationOregon

Posted 08 April 2013 - 03:55 PM

I had a little more time with it, and settled on this as the final product. Works as intended. Thanks for the help.

state = "close"
active = true

while active do
	if state == "open" then
		io.write( "Door is Open.\n" )
		else
			io.write( "Door is Closed.\n" )
	end
	
	io.write( "Enter command(open, close, exit): " )
	state = read()
	
	if state == "exit" then
		 active = false
		 state = "close"
	end
	redstone.setOutput("right", state == "open")
end



The thing that was actually giving me that error was not capitalizing the O in output, but I had plenty of other things to fix after that.

View PostTheCoryKid, on 08 April 2013 - 09:26 AM, said:

Lua does not differentiate between equal and identical like PHP and JavaScript, so doing "string == int" will always return false.
I thought everything was an int, but I'm guessing read only returns a string, even if you enter a number?

#9 QuantumGrav

  • Members
  • 36 posts
  • LocationUnited States

Posted 08 April 2013 - 04:17 PM

Yep! The command read() or io.read() will give you a string no matter what is typed into it. The command 'tonumber(string)' is really helpful for this. If you use 'tonumber(read())' that will turn whatever is inputted into a number as soon as you get it. Pretty useful for a lot of things!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users