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

Restrict number of letters in a password.

Started by Folstaria, 20 June 2015 - 04:21 PM
Folstaria #1
Posted 20 June 2015 - 06:21 PM
Okay, I'm pretty new to Computercraft, so I apologise in advance for my lack of basic knowledge, I'm basically learning by trial and error atm.

I've made a pretty nifty program to open and close doors in my Fallout Vault (I'm a geek, sue me XD), but I'm wondering if there's a way to restrict how many letters someone can type. Here's a few screenshots of my program in use…



As you can see, the password I'm using is six letters, and I've made a little box for the digits to be written into.
However, if I type more than six digits, it starts deleting the bracket.

So, is there a way I can sort this out?

Thanks.
Creator #2
Posted 20 June 2015 - 06:50 PM

password = password:sub(1,numLetters)

That way, any additional letters get rekt.
Folstaria #3
Posted 20 June 2015 - 06:53 PM
So, how would I put this?

term.setCursorPos(1,1)
password = "pipboy"
password = password:sub(1,6)

or


term.setCursorPos(1,1)
password = "pipboy":sub(1,6)
Dog #4
Posted 20 June 2015 - 06:58 PM
It depends on what you're trying to attempt. If you just want to truncate the user's input to 6 characters (after it has been input) then Creator's suggestion is what you want. However, if you're trying to prevent people from typing past the bracket, you'll need a custom read function that can limit the number of characters accepted. I'd recommend looking at this one by theoriginalbit. I've used it in some of my projects and it works great.

For your password input, you'd replace your current use of read() and invoke bit's like this…

input = read("*", nil, 6)
Edited on 20 June 2015 - 05:06 PM
Bomb Bloke #5
Posted 21 June 2015 - 01:02 AM
If, on the other hand, you'd like to allow passwords longer than six characters without messing up your display, the window API offers a simple way to do it.

First define a window that covers the area you wish the user to be able to type into. Then redirect the terminal to that window. Call read(), then when it returns, redirect back to your original display. The read() function won't render outside the bounds of the current terminal, so using a small window forces it to start scrolling if the password gets too long.

Eg:

local curTerm = term.current()

local myTextField = window.create(curTerm, 21, 10, 6, 1)

term.redirect(myTextField)

local password = read("*")

term.redirect(curTerm)