5 posts
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?
113 posts
Location
This page
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.
5 posts
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!
28 posts
Location
Germany
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
5 posts
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!
3057 posts
Location
United States of America
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.
5 posts
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.
28 posts
Location
Germany
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