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

Prompter with redstone activation

Started by Desslink, 15 January 2013 - 05:54 AM
Desslink #1
Posted 15 January 2013 - 06:54 AM
hello everybody, today I've make a TV studio and I want to do a prompter that the animator can act with by pressing a button.

example:
1st press will print "test1"

2nd press will print "test"

3rd press will print "test3"

……..
and this display on a monitor

I've testing a:

if redstone.getInput("left") then
print "test"
end

but I think I'm needing a loop for refresh the "getInput" or something like that

If you have a solution, please leave a reply, thanks
Zudo #2
Posted 15 January 2013 - 07:03 AM

while true do
 os.pullEvent("redstone")
 if rs.getInput("left") then
  print("test")
 elseif rs.getInput("right") then
  print("test2")
 else
  print("test3")
end

Change the sides if needed.
Desslink #3
Posted 15 January 2013 - 07:17 AM
It work but test2 did'nt print and
when i press the button it print "test" {the time of button reset} and print me "test3" (I think "getInput" capt all redstone update)

and how did you expand it ?
GopherAtl #4
Posted 15 January 2013 - 07:28 AM
That one is using separate buttons for each thing, try something more like this…


local lines={
  "first line",
  "line 2",
  "more...",
  "etc",
}

local curLine=1
 
--redirect to the monitor
local montior=peripheral.wrap("top")
term.redirect(monitor)

while true do
  --print cur line, increment to next
  print(lines[curLine])
  curLine=curLine+1
  --//if we're out of lines, break
  if curLine>#lines then
	break
  end
  --wait for next signal
  repeat
	os.pullEvent("redstone")
  until rs.getInput("left")
end

 
--undo the redirect
term.restore()
remiX #5
Posted 15 January 2013 - 07:41 AM
GopherAlt, why you incrementing the current index to use, a simple for loop would suffice:


for i = 1, #lines do
  print(lines[i])
end
GopherAtl #6
Posted 15 January 2013 - 07:46 AM
No particular reason. Does it significantly change the code either way? Just a quirk of mine, I tend not to use for loops if the body is going to be waiting for events each iteration. As I said, no particular reason, either would work just fine.

I guess it does add quite a few lines of unnecessary biolerplate code. Editing the example…

:edit: oh, wait, no. I remember why I did it. Because I had to contain the break condition anyway, or else add an extra conditional, to prevent it from waiting for a signal the last iteration when it there was no next line left. No reason to have two conditionals per iteration instead of one.
Desslink #7
Posted 17 January 2013 - 09:56 AM
Sorry for the late reply, it returns me a:
term:16: Invalid redirect object
Desslink #8
Posted 18 January 2013 - 08:04 AM
have you some fix ?
GopherAtl #9
Posted 18 January 2013 - 08:10 AM
Sorry, somehow didn't see the notice about the first post yesterday.

invalid redirect object means you passed something bad into term.redirect. Is your monitor on top? If not, change the line that says "monitor=peripheral.wrap("top")" to say whatever side it is on.

Unless it's a rather big monitor or your lines are all very small, you probably want to add "monitor.setTextScale(.5)" right after that line as well.
Desslink #10
Posted 18 January 2013 - 08:28 AM
ah, i've forgot the monitor but it returns me the exact same error
remiX #11
Posted 18 January 2013 - 08:34 AM
Is the monitor on the top of the computer?
Desslink #12
Posted 18 January 2013 - 08:35 AM
Yes he is
GopherAtl #13
Posted 18 January 2013 - 08:39 AM
:facepalm: My bad.

monitor, not montior. Line 11.

Given this was not on pastebin, I'm rather amazed that you reproduced this rather obvious typo. Copy-pasted, I guess?

Fixed version, also added monitor_touch event checking if yer running on an advanced monitor

local lines={  "first line",  "line 2",  "more...",  "etc",}
local curLine=1
--redirect to the monitor
local monitor=peripheral.wrap("top")
term.redirect(monitor)
while true do 
  --print cur line, increment to next 
  print(lines[curLine]) 
  curLine=curLine+1 
  --//if we're out of lines, break 
  if curLine>#lines then	   
  break 
  end 
  --wait for next signal 
  while true do
    local e={os.pullEvent() }
    if (e[1]=="redstone" and rs.getInput("left")) or
	   e[2]=="monitor_touch" then
	  break
    end
  end
end
--undo the redirect
term.restore()
remiX #14
Posted 18 January 2013 - 08:39 AM
Lol, if you copied Gopher's code straight, the peripheral.wrap has a spelling error:

local montior=peripheral.wrap("top")
change to
local monitor=peripheral.wrap("top")
Desslink #15
Posted 18 January 2013 - 08:49 AM
yeah, it work fine, thanks
a next thing, where can I add a line for clear monitor once I'm out of lines ?
GopherAtl #16
Posted 18 January 2013 - 08:53 AM
Is there a particular reason scrolling doesn't work for you? 'cause it'll scroll when that happens.

:instantedit: Oh! You mean at the end of the program, like, out of source lines?

just … at the end. term.clear(), right before term.restore()
Desslink #17
Posted 18 January 2013 - 09:00 AM
Right now I've

[...]
end
term.clear()
term.restore()

and my last lines is clear before she was displayed
GopherAtl #18
Posted 18 January 2013 - 09:04 AM
oh, right, sorry, it exits out as soon as it's out of lines. :sigh:

Could you at least pretend you scratched your head and thought for a second "how can I fix this?" before coming to ask how to make this very minor change? It'd make you seem 9001% less like you think I'm just your damn code monkey to boss around. <_</>

Move the "if curLine>#lines then break end" bit to after the while loop that contains os.pullEvent(). Before the last end.
Desslink #19
Posted 18 January 2013 - 11:05 AM
Yeah I know quick reply without thinking but the lua and me… I just try, when I ask you to learn and understand the logic.
I should think more, I know :rolleyes:/>

PS: thanks for the code ;)/>