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

My code keeps giving me problems :/

Started by ComputerCrafter, 09 February 2013 - 05:34 PM
ComputerCrafter #1
Posted 09 February 2013 - 06:34 PM
Here is my code:


    local pulses = 0
    local input = read()
	
    while true do
				    print("Welcome to my bar! Put the diamonds in the box to the right and enter your wanted beer kind! (strong or thick)")
								    os.pullEvent 'redstone'
				    if rs.getInput 'bottom' then
					  pulses = pulses + 1
					  repeat
								    os.pullEvent 'redstone'
					  until not rs.getInput 'bottom'
					  pulses = 0
				    end
	  end
    end
    function waitForKeyword()
	  while true do
	  print("Payment: " ..pulses.. "diamonds, which will buy you" ..pulses.. "beers!")
				    if input == 'strong' then
					  print("Thank you for your payment! Your beer(s) will be right out!")
					  for i=1, pulses do
								    rs.setInput('right', true)
								    sleep(0.1)
								    rs.setInput('right', false)
																    sleep(2)
																    os.shutDown()
				    elseif input == 'thick' then
								    print("Thank you for your payment! Your beer(s) will be right out!")
								    for i=1, pulses do
								    rs.setInput('left', true)
								    sleep(0.1)
								    rs.setInput('left', false)
																    os.shutDown()
					  end
				    end
	  end
    end
    parallel.waitForAny(waitForKeyword, getPulses)

I dont know what is wrong with it! It is in a program called startup (so it runs the program on every boot up) and whenever I reboot, it gives me the OS 1.4 screen, I type one of the beer kinds and it gives me the bar info, but doesnt let me type anything after that, and becomes irresponsive. I have tried many things, which didnt work. Can someone help?
bjornir90 #2
Posted 09 February 2013 - 07:07 PM
You must put the read where you want in the code the user type something. You should also put

term.clear()
term.setCursorPos(1, 1)
So the "CraftOs 1.4" won't appear :)/>
mibac138 #3
Posted 10 February 2013 - 01:44 AM

term.clear()
term.setCursorPos(1,1)

local pulses = 0
local input = read()

function getPulses()
while true do
  print("Welcome to my bar! Put the diamonds in the box to the right and enter your wanted beer kind! (strong or thick)")
  e, p1 = os.pullEvent("redstone")
  if p1 == "bottom" then
   pulses = pulses + 1
   repeat
	os.pullEvent("redstone")
   until not p1 == "bottom"		
  end
end
end

function waitForKeyword()
while true do
  print("Payment: " ..pulses.. "diamonds, which will buy you" ..pulses.. "beers!")
   if input == "strong" then
	print("Thank you for your payment! Your beer(s) will be right out!")
	for i=1, pulses do
	  rs.setInput('right', true)
	  sleep(0.1)
	  rs.setInput('right', false)
	end
	sleep(2)
	os.shutdown()
   elseif input == 'thick' then
	print("Thank you for your payment! Your beer(s) will be right out!")
	for i=1, pulses do
	  rs.setInput('left', true)
	  sleep(0.1)
	  rs.setInput('left', false)
	end
	os.shutDown()
   end
end
end

parallel.waitForAny(waitForKeyword, getPulses)
ComputerCrafter #4
Posted 10 February 2013 - 09:15 AM
Ok, I tried that, now it partially works, but everything prints over the first text… The user's response, everything. Any idea?

But I changed it a little bit:


    term.clear()
    term.setCursorPos(1,1)
	
    local pulses = 0
	
    function getPulses()
    while true do
	  print("Welcome to my bar! Put the diamonds in the box to the right and enter your wanted beer kind! (strong or thick)")
	  e, p1 = os.pullEvent("redstone")
	  if p1 == "bottom" then
	   pulses = pulses + 1
	   repeat
		    os.pullEvent("redstone")
	   until not p1 == "bottom"		   
	  end
    end
    end
	
    function waitForKeyword()
    while true do
    local input = read()
	  print("Payment: " ..pulses.. "diamonds, which will buy you" ..pulses.. "beers!")
	   if input == "strong" then
		    print("Thank you for your payment! Your beer(s) will be right out!")
		    for i=1, pulses do
			  rs.setInput('right', true)
			  sleep(0.1)
			  rs.setInput('right', false)
		    end
		    sleep(2)
		    os.shutdown()
	   elseif input == 'thick' then
		    print("Thank you for your payment! Your beer(s) will be right out!")
		    for i=1, pulses do
			  rs.setInput('left', true)
			  sleep(0.1)
			  rs.setInput('left', false)
		    end
		    os.shutDown()
	   end
    end
    end
	
    parallel.waitForAny(waitForKeyword, getPulses)
Doyle3694 #5
Posted 10 February 2013 - 03:26 PM
If it prints over the first text then you are probably using a term.setCursorPos(1,1) somewhere without a term.clear()