5 posts
Posted 20 November 2014 - 11:52 PM
i am trying to have a little cursor that i can move around the screen using os.pullevent but i also want to have the screen be updated with other things as well such as an enemy for you to fight and i dont want to have to wait 10 seconds for it to update every time; please help.
Edited on 20 November 2014 - 10:53 PM
1023 posts
Posted 21 November 2014 - 12:00 AM
I can think of two options, although there are likely many more that are also better, for solving this problem.
You could use the parellel api to run two functions, one for the os.pullEvent, and another for updating the screen.
However, because of my dislike of the parallel api, I would create a timer using os.startTimer(), and check the os.pullEvent for whether it was the timer firing or whatever you are using to update the cursor.
these are only the first two ways that I thought of doing this, and I am assuming that you have a general idea of how these two things work. If not I would be happy to explain them.
1847 posts
Location
/home/dannysmc95
Posted 21 November 2014 - 10:28 AM
/\ That seems a lot more work.
Use this:
while true do
local args == { os.pullEvent() }
--This defines the event name:
if args[1] == "char" then
print("You pressed a keyboard character")
print("You pressed: "..args[2])
elseif args[1] == "mouse_click" then
print("You clicked the terminal at:")
print("X: "..args[3].." - Y: "..args[4].." - Button: "..args[2])
elseif args[1] == "timer" then
--Do timer stuff - like a time...
print("Time to update the clock!")
end
end
Doing the above means that you save all possible return arguments in a table, and then you can use them, having a mouse is a little more challenging but you can use the above code to get multiple inputs without leaving the loop, until you run another function or use:
break
Here is the os.pullEvent() list of events:
(Scroll to the event types:)
http://computercraft.info/wiki/Os.pullEvent
1023 posts
Posted 21 November 2014 - 11:59 AM
/\ That seems a lot more work.
Use this:
while true do
local args == { os.pullEvent() }
--This defines the event name:
if args[1] == "char" then
print("You pressed a keyboard character")
print("You pressed: "..args[2])
elseif args[1] == "mouse_click" then
print("You clicked the terminal at:")
print("X: "..args[3].." - Y: "..args[4].." - Button: "..args[2])
elseif args[1] == "timer" then
--Do timer stuff - like a time...
print("Time to update the clock!")
end
end
Doing the above means that you save all possible return arguments in a table, and then you can use them, having a mouse is a little more challenging but you can use the above code to get multiple inputs without leaving the loop, until you run another function or use:
break
Here is the os.pullEvent() list of events:
(Scroll to the event types:)
http://computercraft.info/wiki/Os.pullEvent
Actually… This is exactly what I described. No table was needed in either of the methods I described using.
He is creating a game, and his problem is when he calls os.pullEvent it causes the program to stall until there is input or 10 seconds pass. So you would either have to break the pull event using a timer or do stuff while waiting for the event to fire (parallel API)
Edited on 21 November 2014 - 11:04 AM