Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.
Please help!
u could create your own read function i did this once for a password lock it goes like this :I need user input in a program but I want the user to only be able to type in 44 characters maximum.
Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.
Please help!
function maxread(x)
count = 0
txt =""
repeat
id,chr = os.pullEvent()
if id == "char" then
term.write(chr)
txt = txt..chr
count = count + 1
end
if id == "key" and chr == 28 then
return txt
end
until count == x
while true do
id,chr = os.pullEvent()
if id == "key" and chr == 28 then
return txt
end
end
end
note that u cant delete text herenote that u cant delete text here
i said that my function doesn't allow that, not that its not possiblenote that u cant delete text here
You can, it's just a bit more complicated. Instead of writing each character on its on the screen, put all the characters you have received so far in a string and with each iteration of the loop, set the cursor to the beginning of the line (or where ever you want it to be) and write the whole string. If you receive a backspace delete the last character of the string.
u could create your own read function i did this once for a password lock it goes like this :I need user input in a program but I want the user to only be able to type in 44 characters maximum.
Once I do this I'll make billions on this server for being the first to make a chat program and I may also give away the code once the program is done because it's actually quite good in my opinion.
Please help!note that u cant delete text herefunction maxread(x) count = 0 txt ="" repeat id,chr = os.pullEvent() if id == "char" then term.write(chr) txt = txt..chr count = count + 1 end if id == "key" and chr == 28 then return txt end until count == x while true do id,chr = os.pullEvent() if id == "key" and chr == 28 then return txt end end end
function read( _sReplaceChar, _tHistory )
term.setCursorBlink( true )
local sLine = ""
local nHistoryPos = nil
local nPos = 0
if _sReplaceChar then
_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
end
local w, h = term.getSize()
local sx, sy = term.getCursorPos()
local function redraw()
local nScroll = 0
if sx + nPos >= w then
nScroll = (sx + nPos) - w
end
term.setCursorPos( sx, sy )
term.write( string.rep(" ", w - sx + 1) )
term.setCursorPos( sx, sy )
if _sReplaceChar then
term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
else
term.write( string.sub( sLine, nScroll + 1 ) )
end
term.setCursorPos( sx + nPos - nScroll, sy )
end
while true do
local sEvent, param = os.pullEvent()
if sLine <= 44 then
if sEvent == "char" then
sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
nPos = nPos + 1
redraw()
elseif sEvent == "key" then
if param == 28 then
-- Enter
break
elseif param == 203 then
-- Left
if nPos > 0 then
nPos = nPos - 1
redraw()
end
elseif param == 205 then
-- Right
if nPos < string.len(sLine) then
nPos = nPos + 1
redraw()
end
elseif param == 200 or param == 208 then
-- Up or down
if _tHistory then
if param == 200 then
-- Up
if nHistoryPos == nil then
if #_tHistory > 0 then
nHistoryPos = #_tHistory
end
elseif nHistoryPos > 1 then
nHistoryPos = nHistoryPos - 1
end
else
-- Down
if nHistoryPos == #_tHistory then
nHistoryPos = nil
elseif nHistoryPos ~= nil then
nHistoryPos = nHistoryPos + 1
end
end
if nHistoryPos then
sLine = _tHistory[nHistoryPos]
nPos = string.len( sLine )
else
sLine = ""
nPos = 0
end
redraw()
end
elseif param == 14 then
-- Backspace
if nPos > 0 then
sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
nPos = nPos - 1
redraw()
end
end
end
end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()
return sLine
end
My chat system didn't work with this read modification either. Is there any other ways?
Without read() modifications preferrably.
text = read()
if text:len() <= 44 then
do something
end
My chat system didn't work with this read modification either. Is there any other ways?
Without read() modifications preferrably.text = read() if text:len() <= 44 then do something end
function limitRead( nLength, cReplaceChar )
term.setCursorBlink( true )
nLength = nLength or -1 -- -1 is unlimited
sReturnString = ""
xPos, yPos = term.getCursorPos()
while true do
event, char = os.pullEvent()
if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
if event == "char" then sReturnString = sReturnString .. char
elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
elseif event == "key" and char == 14 then -- Backspace
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
end
http://pastebin.com/gAw5X9bUZzz u do read the whole thread before writing comments don't u ….The above code would allow you to input any number of characters because read is constantly receiving input until 'enter' is pressed, jumping to the next line in your program to be executed. This would mean that you would have to keep getting input over and over until the user entered something within the limit. I wrote up a quick solution that works and is hopefully what you're looking for:My chat system didn't work with this read modification either. Is there any other ways? Without read() modifications preferrably.text = read() if text:len() <= 44 then do something end
http://pastebin.com/gAw5X9bU nLength is the maximum length and cReplaceChar is the character that will act as a mask for the input. Unfortunately, there is no scrolling if you decide to enter input off of the page. Hope I helped! :)/>/>function limitRead( nLength, cReplaceChar ) term.setCursorBlink( true ) nLength = nLength or -1 -- -1 is unlimited sReturnString = "" xPos, yPos = term.getCursorPos() while true do event, char = os.pullEvent() if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check if event == "char" then sReturnString = sReturnString .. char elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter elseif event == "key" and char == 14 then -- Backspace term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end end