Jump to content




Displaying user input on a monitor.


45 replies to this topic

#1 boudragon

  • Members
  • 107 posts

Posted 28 February 2013 - 08:58 AM

Ok so first let me start with yes I am new to this forum, LUA and ComputerCraft. Apologizes ahead of time for being a noob! :) I do have some knowledge of Quick Basic but obviously it's not quite the same. Now for my question... Ive set up a simple password program that asks for the user to input a word and if it matches the variable then it unlocks the system... simple stuff. The problem I have is trying to get it to display the input as the user is typing it in the computer on the monitor. The best I can get is after they type in the password and hit enter it displays what they typed on the monitor. How can I get the monitor to display what the user is typing AS they are typing it on the monitor as well as the computer? Thanx in advance!

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 28 February 2013 - 09:07 AM

Split into new topic.

#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 28 February 2013 - 10:34 AM

You need to redirect the monitor.

term.redirect(peripheral.wrap("top"))
local yourInput = read()


#4 JokerRH

  • Members
  • 147 posts

Posted 28 February 2013 - 10:40 AM

the read function cannot display on two monitors at the same time. So you'll have to write your own like this:

function getInput(side, x, y)
	m = peripheral.wrap(side)
	local input = ""
	term.setCursoros(x, y)
	m.setCursorPos(x, y)

	while true do
		event, param = os.pullEvent()
		if event == "char" then
			input = input..param

			--write should continue where it left off
			term.write(param)
			m.write(param)
		elseif event == "key" then
			if param == keys.delete then
				input = string.sub(input, 1, #input - 1)

				--don't forget the .." " -> that should remove the removed letter from the screen
				term.setCursorPos(x, y)
				term.write(input.." ")

				m.setCursorPos(x, y)
				m.write(input.." ")
			elseif param == keys.enter then
				return input
			end
		end
	end
end

Note: This is not tested, it's a little late here...But it should actually work... ;D

EDIT: This program should write your input to the terminal and the monitor. Of course term.redirect is faster, but then you won't see what you just typed in as long as you look at the teminal...
You could use metatables and modify the term table to refer to multiple monitors, but I wouldn't recommend that as long as you are still a beginner in lua :)

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 28 February 2013 - 10:43 AM

Do you have ANY code you can show us so we can help you further?

#6 boudragon

  • Members
  • 107 posts

Posted 28 February 2013 - 11:04 AM

Quote

Do you have ANY code you can show us so we can help you further?

Id have to retype all I have so far... I probably should have written it in a program outside of Minecraft first.

But basically it prints a line of text then asks for a password. If you type in the right one it prints another line of text saying it was accepted and if its wrong it rejects the password and shuts down the system.

#7 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 28 February 2013 - 11:07 AM

View Postboudragon, on 28 February 2013 - 11:04 AM, said:

Quote

Do you have ANY code you can show us so we can help you further?

Id have to retype all I have so far... I probably should have written it in a program outside of Minecraft first.

But basically it prints a line of text then asks for a password. If you type in the right one it prints another line of text saying it was accepted and if its wrong it rejects the password and shuts down the system.
I'm going to guess you would want to capture their keystrokes using an event in a loop, then return "yes" or "no" whenever they hit enter. Printing the keys as they type of course.

#8 boudragon

  • Members
  • 107 posts

Posted 28 February 2013 - 11:12 AM

Ok I managed to write a portion of the program to text to give you guys an idea of the noob I am LOL! Again... VERY sloppy but keep in mind Im also doing all this just in MC. Also thanx so far for all the help!

mon.setCursorPos (1,4)
mon.write ("Password: ")
write "Password: "
input = read("*")
mon.setCursorPos (10,4)
mon.write (input)
if input == password then
mon.setCursorPos (1,5)
mon.write (" ")
mon.setCursorPos (1,6)
print " "
print "Password Accepted.")
mon.write ("Password Accepted")
sleep (2)
term.clear()
mon.clear()
mon.setCursorPos (1,1)
else

::EDIT::
Was wondering too... is it possible to write a program that, at startup, makes it so the monitor ALWAYS displays what is being typed in the computer (computer also displays) instead of having to add it into each program?

#9 JokerRH

  • Members
  • 147 posts

Posted 01 March 2013 - 02:55 AM

View Postboudragon, on 28 February 2013 - 11:12 AM, said:

::EDIT::
Was wondering too... is it possible to write a program that, at startup, makes it so the monitor ALWAYS displays what is being typed in the computer (computer also displays) instead of having to add it into each program?

Yes, it is. As I already said you'll have to use metatables for it. It is maybe a little complicated...I might wright an easy tutorial about this in the future, but for now, this is how it has to be in that case: (tested)


m = peripheral.wrap("left")

function set(ind)
  term[ind] = setmetatable({term = term[ind], monitor = m[ind]}, {
    __call = function(self, ...)
      self.term(...)
      self.monitor(...)
    end
  })
end

set("write")
set("setCursorPos")

This will modifie the wright and setCursorPos functions.
Just test it... ;D

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 March 2013 - 03:12 AM

View Postboudragon, on 28 February 2013 - 11:12 AM, said:

Ok I managed to write a portion of the program to text to give you guys an idea of the noob I am LOL! Again... VERY sloppy but keep in mind Im also doing all this just in MC. Also thanx so far for all the help!
If you save the file you do have access to that on your computer (or the server's files if you can get to them).
The location is
<minecraft-dir>/saves/<world-name>/computer/<computer-id>

to get the comptuer id type 'id' into the computers console and press enter.

the files in these folders are your programs and you can then edit them with something nicer like Sublime Text 2, which has some awesome support for ComputerCraft added via these awesome people
GravityScore's ComputerCraft Syntaxer and NeverCast's Auto-Lua Selector Thingy (this one is default to Lua, but on one of the last few pages in the first link I describe how you can make it work with GravityScores Plugin)

#11 boudragon

  • Members
  • 107 posts

Posted 01 March 2013 - 05:31 AM

Hey thanx JokerRH but I have NO IDEA what to do with what you posted. Do I add it as its own file? add to the startup file? Why couldnt this have been programmed in QB Id have been done by now LOL! Funny thing is this is just a BASIC thing I still got a lot more to program! Grrr

#12 JokerRH

  • Members
  • 147 posts

Posted 01 March 2013 - 07:21 AM

Metatables are not that basic, so everything is normal if you don't understand them as a beginner...:D
But anyway, you'll just have to run that code and it will change the term table.
That means you can put it into it's own file and load it with os.load(), you could start it with shell.run(), or you can put it anywhere in your existing programm, it just has to be run once. (obviously before you want to use it)

#13 boudragon

  • Members
  • 107 posts

Posted 01 March 2013 - 11:14 AM

So just say add those lines to the beginning of the startup file and all should be good to go?

::Edit:: btw Joker as a beginner NOTHING is basic! LOL! Just meant that the whole monitor thing really isn't even a necessary part of what Im trying to put together...

#14 boudragon

  • Members
  • 107 posts

Posted 01 March 2013 - 03:53 PM

Ok so Joker it works GREAT... I think the only issue I have now is the spacing on the monitor is off. Its actually making it to where if there is text in line 1 and 2 on the computer it shows up as line 1 and 3 on the monitor. Other than that this is PERFECT!

::EDIT::

Ok so for some reason its working fine now. One question I have though. Lets say I do EDIT STARTUP and it loads up whatever is already in the startup file. once I enter it doesnt set the cursor to the right position and when I exit it doesnt clear the screen... how can that be fixed since the problem technically lies within the edit program itself? Also when typing any input it displays what is being typed like this:

If I type the word HELLO it will display : HHEHELHELLHELLO

#15 JokerRH

  • Members
  • 147 posts

Posted 02 March 2013 - 08:41 AM

View Postboudragon, on 01 March 2013 - 03:53 PM, said:

once I enter it doesnt set the cursor to the right position and when I exit it doesnt clear the screen...

simply add this to the end of the code:
set("clear")
term.setCursorPos(term.getCursorPos())

#1 Only these 3 functions that are passed to the set() function will work for both monitors. If you need anotherone, just add it :D
#2 It didn't know the previous cursorposition. The last line of code 3 lines above should fix that...

#16 boudragon

  • Members
  • 107 posts

Posted 02 March 2013 - 09:50 AM

Ok I will try it out as soon as I can and get back to you :) Also when it comes to entering and exiting a program my screen needs to always clear and reset the cursor to (1,1). keeps it clean... even though my coding is sloppy! LOL!

::EDIT::

Ok so I tried it and it didn't seem to fix the issue... its still displaying user input like this:
HHEHELHELLHELLO

and when editing a file it displays but its not really showing an edit screen its just displays what the file contains then writes over what was displayed when I type. And for some reason when I add several print lines instead of displaying them one on top of another it displays all in one line unless I tell it a specific cursor position.

#17 JokerRH

  • Members
  • 147 posts

Posted 03 March 2013 - 02:32 AM

I don't think there is a setCursorBlink for monitors, therefor you cannot make it blink in the editor on both devices.
To the HHEHELHELL...I had the same thing as long as I didn't have the term.setCursorPos for both monitors. So maybe check if it is typed correctly.
If you still can't find it, just write the exact thing you do when this occurs, because I cannot replicate it.

#18 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 March 2013 - 02:37 AM

View PostJokerRH, on 03 March 2013 - 02:32 AM, said:

I don't think there is a setCursorBlink for monitors, therefor you cannot make it blink in the editor on both devices.
There is indeed setCursorBlink for monitors..... http://puu.sh/2aMxt

for k,v in pairs( peripheral.getMethods('right') ) do
  print(v)
end


#19 JokerRH

  • Members
  • 147 posts

Posted 03 March 2013 - 03:22 AM

View PostTheOriginalBIT, on 03 March 2013 - 02:37 AM, said:

There is indeed setCursorBlink for monitors..... http://puu.sh/2aMxt

Thanks! So he just has to add a set("setCursorBlink") to the code.
I tried to make it copy all the functions, but there are some that won't work for monitors...:(

#20 boudragon

  • Members
  • 107 posts

Posted 03 March 2013 - 08:50 AM

:P Ok... if you could post what my code should look like now I can double check and see if it's right so far and also see where I may have went wrong cause some things still aren't working quite right. This way I can see if it's what I added or maybe an error I made typing in the code you've helped me with so far.

::EDIT::

Ok so I found the problem! Turns out my set("set.CursorPos") was typed as set("setCUrsorPos). That tiny mistake made a world of difference! LOL! Now everything seems to be working right. I do have one more question though and I think it may be similar to this question... let's say I have a variable that I want to toggle between "On" and "Off" but I want that variable to remain either "On" or "Off even if the system shuts down... how would I do that? I got it to toggle and display that it toggled but it doesn't seem to store it for future use...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users