Posted 29 October 2015 - 07:31 PM
Hey i'm using code i found on the forums in my code to limit the length of an input, the code is causing bugs in my program. When i switch between the Register and Login screen then try to type something it will type multiple characters, I believe this is because it is running more that one of the custom read function. Any help is appreciated!
Read Function Code:
Program Code:
Read Function Code:
function limitRead(nLimit, replaceChar)
term.setCursorBlink(true)
local cX, cY = term.getCursorPos()
local rString = ""
if replaceChar == "" then replaceChar = nil end
repeat
local event, p1 = os.pullEvent()
if event == "char" then
-- Character event
if #rString + 1 <= nLimit then
rString = rString .. p1
write(replaceChar or p1)
end
elseif event == "key" and p1 == keys.backspace and #rString >= 1 then
-- Backspace
rString = string.sub(rString, 1, #rString-1)
xPos, yPos = term.getCursorPos()
term.setCursorPos(xPos-1, yPos)
write(" ")
term.setCursorPos(xPos-1, yPos)
end
until event == "key" and p1 == keys.enter
term.setCursorBlink(false)
print() -- Skip to the next line after clicking enter.
return rString
-- And you call it like this
--input = limitRead(10)
-- If you want to replace it with a char, like read("*") then
--password = limitRead(10, "*")
end
Program Code:
shell.run("Startup/Main")
boot(0.1)
local function LoginBoot()
clear()
paintutils.drawImage(UsernameScreen,1,1)
term.setCursorPos(17,11)
term.setBackgroundColour(colors.lightGray)
term.setTextColor(colors.white)
print("Account Username:")
term.setCursorPos(39,17)
print("Register")
term.setCursorPos(17,13)
term.setBackgroundColour(colors.white)
term.setTextColor(colors.blue)
username = limitRead(17)
sleep(0.5)
end
local function Register()
endRead = "true"
clear()
paintutils.drawImage(RegisterScreen,1,1)
term.setCursorPos(21,2)
term.setBackgroundColour(colors.lightGray)
term.setTextColor(colors.white)
print("Register")
term.setCursorPos(12,5)
print("Username")
term.setCursorPos(39,17)
print("LoginScrn")
term.setCursorPos(12,8)
print("Password")
term.setCursorPos(12,12)
print("Password")
term.setCursorPos(21,5)
term.setBackgroundColour(colors.white)
term.setTextColor(colors.blue)
username = limitRead(17)
sleep(1)
term.setCursorPos(21,8)
pass1 = limitRead(17)
sleep(1)
term.setCursorPos(21,12)
pass2 = limitRead(17)
sleep(1)
end
function UsernameScreenButtons()
local s = "false"
local event = nil
local button = nil
local x = nil
local y = nil
repeat
local event, button, x, y = os.pullEvent( "mouse_click" )
if event == "mouse_click" and x >= 38 and x <= 50 and y == 17 then
parallel.waitForAll(Register, RegisterScreenButtons)
s = "true"
end
until s == "true"
end
function RegisterScreenButtons()
local s = "false"
local event = nil
local button = nil
local x = nil
local y = nil
repeat
local event, button, x, y = os.pullEvent( "mouse_click" )
if event == "mouse_click" and x >= 38 and x <= 50 and y == 17 then
parallel.waitForAll(LoginBoot, UsernameScreenButtons)
s = "true"
end
until s == "true"
end
parallel.waitForAll(LoginBoot, UsernameScreenButtons)
Edited on 29 October 2015 - 06:39 PM