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

[LUA] Need help with my program

Started by Gutawer, 11 December 2012 - 04:56 AM
Gutawer #1
Posted 11 December 2012 - 05:56 AM
Hey guys. I am pretty much new to lua and the ComputerCraft mod. I'm trying to write a program that displays text on the screen saying "Press button for coffee.". Then the user presses the button and the code moves on. However, the code does not stop at the button part and just carries on anyway.
Also I want it so that it will wait forever for the user to press the button and not just time out. Here is my code.
do
print("Press button for coffee.")
if redstone.getBundledInput("right") then ~ I'm using RedPower as well as ComputerCraft
  print("Thank you! Please wait.")
  textutils.slowPrint("---------")
  print("Here you go!")
  end
end
Help would be much appreciated (as would anything to make my code shorter!). If you can help, please do.
Sammich Lord #2
Posted 11 December 2012 - 06:08 AM
I'll try to explain it as much as possible.

print("Press any key for coffee.") --Prints the text
os.pullEvent("key") --Only waits for the key event(When the user presses a key)
print("Here is the coffee.") --Prints the text
You can use os.pullEvent to pull a lot of events that happen. If you want more details on the function the check the wiki.
Gutawer #3
Posted 11 December 2012 - 06:24 AM
Sorry I didn't make that clear enough. By button, I meant the item in Minecraft. Sorry :/
Orwell #4
Posted 11 December 2012 - 06:34 AM
Then you'd wait for an event indicating a redstone change, like this:

while true do
  print("Press button for coffee.")
  local event, side = os.pullEvent("redstone")
  if side=="right" and redstone.getBundledInput(side) then
	print("Thank you! Please wait.")
	textutils.slowPrint("---------")
	print("Here you go!")
  end
end
Sammich Lord #5
Posted 11 December 2012 - 06:35 AM
Sorry I didn't make that clear enough. By button, I meant the item in Minecraft. Sorry :/
Oh, well then.

print("Press the right button for coffee.")
local coffee --Makes a variable to tell weather the right butotn was pressed or not
while not coffee do --Starts a while loop that stops if coffee is true
  os.pullEvent("redstone") --Pulls the redstone event
  if redstone.getBundledInput("right") then --Checks if the bundled cable on the right side is true
    coffee = true --Sets coffee to true thus stopping the loop
    print("Here you go!")
  end
end
I hope it helps!