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

Updating values on screen while using os.pullEvent("mouse_click")

Started by jandcando, 26 January 2015 - 10:56 AM
jandcando #1
Posted 26 January 2015 - 11:56 AM
How can I make it so I have clickable buttons on the screen but still have values updating every cycle while the screen is waiting to be clicked? So far the functionality of os.pullEvent freezes the code and waits for an input. Is there a way to put a timeout on this?
Quintuple Agent #2
Posted 26 January 2015 - 07:07 PM
os.startTimer(seconds) will allow you to use os.pullEvent and still update every second.


os.startTimer(1)
while true do
local eve=os.pullEvent()
if eve=="mouse_click" then
--stuff
end
--other code
os.startTimer(1)
end

os.startTimer also returns an id for that timer in case you need to check to make sure it is the right timer setting off the event, but if you just need to update every second this is not really needed.
jandcando #3
Posted 26 January 2015 - 08:06 PM
os.startTimer(seconds) will allow you to use os.pullEvent and still update every second.


os.startTimer(1)
while true do
local eve=os.pullEvent()
if eve=="mouse_click" then
--stuff
end
--other code
os.startTimer(1)
end

os.startTimer also returns an id for that timer in case you need to check to make sure it is the right timer setting off the event, but if you just need to update every second this is not really needed.

Thanks for pointing this out. I'm going to start experimenting with it now!
Possseidon #4
Posted 26 January 2015 - 08:19 PM
os.startTimer(seconds) will allow you to use os.pullEvent and still update every second.


os.startTimer(1)
while true do
local eve=os.pullEvent()
if eve=="mouse_click" then
--stuff
end
--other code
os.startTimer(1)
end

os.startTimer also returns an id for that timer in case you need to check to make sure it is the right timer setting off the event, but if you just need to update every second this is not really needed.

the second point you made is actually pretty important!
only start a new timer when the event is a timer AND if the handle returned by os.startTimer(timeout) is equal to the one the os.pullEvent() gave you!
otherwise it will start a second timer even if you only pressed your mouse/any other random event occured
when too many timers get started wich obviously will restart themselves thats not really what you want to do, and your program is gonna lag as hell ^^

sth like this if you want a code example:


local t = os.startTimer(1)
while true do
  local event, p1, p2, p3 = os.pullEvent() -- of course you can also use local event = {os.pullEvent} and get everything at once with event[1] event[2] etc :)/>/>
  if event == "mouse_click" then
	-- do stuff that happens when mouse is clicked
	-- p1 - p3 (or event[2-4] since event[1] is the eventType) are your mousebutton, X and Y coordinate
  elseif event == "timer" and p1 == t then
	t = os.startTimer(1)
	-- do your updating that has to be done every fixed amount (1 second here) of time here
  end
end

Edit: oups :P/>
Edited on 27 January 2015 - 06:24 PM
jandcando #5
Posted 26 January 2015 - 08:57 PM
os.startTimer(seconds) will allow you to use os.pullEvent and still update every second.


os.startTimer(1)
while true do
local eve=os.pullEvent()
if eve=="mouse_click" then
--stuff
end
--other code
os.startTimer(1)
end

os.startTimer also returns an id for that timer in case you need to check to make sure it is the right timer setting off the event, but if you just need to update every second this is not really needed.

the second point you made is actually pretty important!
only start a new timer when the event is a timer AND if the handle returned by os.startTimer(timeout) is equal to the one the os.pullEvent() gave you!
otherwise it will start a second timer even if you only pressed your mouse/any other random event occured
when too many timers get started wich obviously will restart themselves thats not really what you want to do, and your program is gonna lag as hell ^^

sth like this if you want a code example:


local t = os.startTimer(1)
while true do
  local event, p1, p2, p3 = os.pullEvent() -- of course you can also use local event = {os.pullEvent} and get everything at once with event[1] event[2] etc :)/>/>
  if event == "mouse_click" then
	-- do stuff that happens when mouse is clicked
	-- p1 - p3 (or event[2-4] since event[1] is the eventType) are your mousebutton, X and Y coordinate
  elseif event == "timer" and handle == t then
	t = os.startTimer(1)
	-- do your updating that has to be done every fixed amount (1 second here) of time here
  end
end

Thanks for the help!
KingofGamesYami #6
Posted 26 January 2015 - 11:04 PM
@Possseidon: Your code is flawed. You compare t to handle, which is nil, thus it will never equal t.
jandcando #7
Posted 27 January 2015 - 01:43 AM
@Possseidon: Your code is flawed. You compare t to handle, which is nil, thus it will never equal t.
I was still able to understand what he meant by it (event[1]), so it's all good. But you are right, though.
Possseidon #8
Posted 27 January 2015 - 07:27 PM
@KingofGamesYami fixed it… kinda mixed my written text and code up xD
Edited on 27 January 2015 - 06:27 PM