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

Stop timer / better read [SOLVED]

Started by unobtanium, 12 July 2013 - 05:04 AM
unobtanium #1
Posted 12 July 2013 - 07:04 AM
Hey everbody,

I think i am doing something or at least understood it wrong.
If i am creating a timer os.startTimer(2) i can let it save the returned specified code into a variable:
local refresh = os.startTimer(2)
everytime the timer gets tiggered, i reset the timer.
In my code i am having read(). I think that it pauses the timer, but i am not 100% sure on this. If it does pause it wouldnt be a problem, however if it does i would have to check, if the read() was longer then the remaining time of the timer.
So i am basically dont want to have two or more timers running, because i have to start a new timer somewhere in the code and messes up the refresh.


local refresh = os.startTimer(2)
local counter = 1
while true do
local event = {os.pullEvent()}
if event[1] == "timer" and event[2] == refresh then
  refresh = os.startTimer(2)
  counter = counter + 1
  print(counter)
elseif event[1] == "char" then
  read()
  -- do i have to add a check for the old timer and create a new one here?
end
end

I hope that was clear :S
unobtanium
Engineer #2
Posted 12 July 2013 - 07:19 AM
The read function creates its own pullEvent and infinite loop. And yes, it will stop the timer. Actually it doesnt, but it does nothing with the timer event.
This will work for your desires, at least I think it will:


local timer = os.startTimer( 2 )
local counter = 0
local s = ""

while true do
   local e = { os.pullEvent() }
   if e[1] == "timer" and e[2] == timer then
	  timer = os.startTimer( 2 )
	  counter = counter + 1
	  print( counter )
   elseif e[1] == "char" then
	  s = s .. e[2]
   elseif e[1] == "key" and e[2] == keys.enter then
	  break
   end
end

print(s)
Engineer #3
Posted 12 July 2013 - 07:39 AM

local timer = os.startTimer( 2 )
local counter = 0

local x, y  = term.getCursorPos()
local s = ""

while true do
	local e = { os.pullEvent() }
	if e[1] == "timer" and e[2] == timer then
		timer = os.startTimer( 2 )
		counter = counter + 1
		
		term.setCursorPos( x, y + 1 )
		term.clearLine()
		term.write( "Seconds past: " .. counter * 2 )
	elseif e[1] == "char" then
		s = s .. e[2]
		
		term.setCursorPos( x, y )
		term.clearLine()
		term.write( s )
	elseif e[1] == "key" then
		if e[2] == keys.enter then
			break
		elseif e[2] == keys.backspace then
			s = s:sub( 1, s:len() - 1 )
		end
	end
end

This code will behave way better, since it stays on two lines :)/>
unobtanium #4
Posted 12 July 2013 - 07:48 AM
So i am not allowed to use read()? edit: Yeah okay dumb question ^^
I thank you. I allways wanted to know how to make such kind of read() but had problems with "key" and "char" dont working together. So this is exactly what i needed :P/>
unobtanium #5
Posted 12 July 2013 - 08:11 AM
It didnt worked correctly at the first time e.g. didnt print unless i add a letter. I changed it and made it perfect for myself:


function betterRead(x,y,text)
term.setTextColor(colors.lightGray)
term.setCursorBlink(true)
local s = tostring(text)

while true do
  term.setCursorPos(x,y)
  term.write("									  ")
  term.setCursorPos(x,y)
  term.write( s )
  local e = { os.pullEvent() }
  if e[1] == "timer" and e[2] == delay then
   draw() -- or something else here
   delay = os.startTimer( 2 )
  elseif e[1] == "char" then
   s = s .. e[2]
  elseif e[1] == "key" then
   if e[2] == keys.enter then
	break
   elseif e[2] == keys.backspace then
	s = s:sub( 1, s:len() - 1 )
   end
  end
end
term.setTextColor(colors.white)
term.setCursorBlink(false)
return s -- or save it into a variable
end
theoriginalbit #6
Posted 12 July 2013 - 08:17 AM

term.setCursorPos(x,y)
term.write("									  ")
term.setCursorPos(x,y)
You should use

term.setCursorPos(x,y)
term.clearLine()
term.setCursorPos(x,y)
that way if the screen is a different size it will not cause problems.

As a side note, here is a rewrite of CC's read function that I made a month or so back, it adds a bunch of extra features and fixes a few bugs they have, and all in about the same line count… :)/>

EDIT: Oh btw you shouldn't even need the second term.setCursorPos as term.clearLine resets the cursor back to where it was after clearing the line.
Edited on 12 July 2013 - 06:22 AM
unobtanium #7
Posted 12 July 2013 - 08:24 AM
I cant clear the whole line, because i am having something like this:

TEXT: <WhatYouWrite>

If i clear the whole line i dont have the "TEXT:" written down anymore. I also dont need multiple lines, because i am working with an overview menu with selectable points like "TEXT:" underneath each other.

edit: Damn… i just realized that it cuts off at the end of the screen…
Now i have to let it just print the last 30-35 letters depending on the x position…
theoriginalbit #8
Posted 12 July 2013 - 08:42 AM
I also dont need multiple lines, because i am working with an overview menu with selectable points like "TEXT:" underneath each other.
I never said to use multiple lines…

In that case, some code that will clear the line but preserve existing text

write("Username: ")
local sw, sh = term.getSize()
local sx, sy = term.getCursorPos()

--# read loop
while true do
  --# code

  --# and where you want to clear the line
  term.setCursorPos( sx+1, sy )
  term.write( string.rep(' ', sw - sx + 1) )

  --# more code
end
unobtanium #9
Posted 12 July 2013 - 08:59 AM
Whatever… i dont mind that too much.
More important: How do i get the last e.g. 30 letters?

local w,h = getSize()
local x, y = term.getCursorPos()
local s = "abcdefghijklmnopqrstuvwxyz"
term.setCursorPos(x,y)
if s:len()+x < w then
term.write(s)
else
term.write(s:sub( s:len() - (w-x -2)))
end

edit: Yep, that works. I am fine now.
Thank you guys! :D/>

And that theoriginalB.I.T. is happy: I changed it and used string.rep() ;D
theoriginalbit #10
Posted 12 July 2013 - 09:56 AM
And that theoriginalB.I.T. is happy: I changed it and used string.rep() ;D

string.rep is the much better way to do it.

Also it's TheOriginalBIT or theoriginalbit or BIT xD :P/>
unobtanium #11
Posted 12 July 2013 - 11:52 AM
Hm… i somewhere read, that people dont wrote your name correctly and that it pissed you kinda off, which was one of the reasons you made your whole name lower case. Was the name with the points a joke so? :D/>