#1
Posted 10 April 2014 - 04:01 PM
So I guess coroutines may be the only thing left relevant. I've been doing much Google searching but to no avail-can someone help me out?
#2
Posted 10 April 2014 - 04:16 PM
Thegameboy, on 10 April 2014 - 04:01 PM, said:
So I guess coroutines may be the only thing left relevant. I've been doing much Google searching but to no avail-can someone help me out?
#3
Posted 10 April 2014 - 04:24 PM
Apemanzilla, on 10 April 2014 - 04:16 PM, said:
Thegameboy, on 10 April 2014 - 04:01 PM, said:
So I guess coroutines may be the only thing left relevant. I've been doing much Google searching but to no avail-can someone help me out?
No, if I were to use parallel.waitForAll then I would have to wait until the timer was done before it could read my next key. And that's what I meant by not wanting them to wait for each other.
#4
Posted 10 April 2014 - 04:32 PM
#5
Posted 10 April 2014 - 05:21 PM
Lyqyd, on 10 April 2014 - 04:32 PM, said:
I'm trying to implement a gravity and movement system. The problem is that you can only move 1 pixel in midair per pixel it falls. Here's the whole code:
x,y = 10,3
--first number is x, next is y
wallpos = {10,15,11,15,12,15,13,15,14,15,15,15,16,15,17,15,18,15,19,15,20,15,12,17}
local minmomentum = .5
local maxmomentum = .1
local momentum = .5
local onGround = false
term.setBackgroundColor(2048)
term.clear()
while true do
onGround = false
parallel.waitForAll(
function ()
newy,newx = 0,0
local possibleStopGravity = false
event,key = os.pullEvent()
for i=-1,#wallpos,2 do
if (event == "key" and key == 203) then
if (x-1 ~= wallpos[i] or y ~= wallpos[i+1]) then
paintutils.drawPixel(x,y,2048)
newx = -1
if (y+1 == wallpos[i+1] and newx+x == wallpos[i]) then
possibleStopGravity = true
end
if (onGround == false) then
paintutils.drawPixel(x-1,y,16)
end
else
newx = 0
possibleStopGravity = false
break
end
end
if (event == "key" and key == 205) then
if (x+1 ~= wallpos[i] or y ~= wallpos[i+1]) then
paintutils.drawPixel(x,y,2048)
newx = 1
if (y+1 == wallpos[i+1] and newx+x == wallpos[i]) then
possibleStopGravity = true
end
if (onGround == false) then
paintutils.drawPixel(x+1,y,16)
end
else
newx = 0
possibleStopGravity = false
break
end
end
--if (event == "key" and key == 208) then
--if (y+1 ~= wallpos[i+1] or x ~= wallpos[i]) then
--paintutils.drawPixel(x,y,2048)
--newy = 1
--else
--newy = 0
--redstone.setOutput("left", true)
--break
--end
--end
if (event == "key" and key == 200) then
if (onGround == true) then
paintutils.drawPixel(x,y,2048)
newy = -3
end
end
end
x = x+newx
y = y+newy
if (possibleStopGravity == true) then
onGround = true
end
end,
function ()
for i=0,#wallpos,2 do
if (y+1 == wallpos[i] and x == wallpos[i-1]) then
onGround = true
momentum = minmomentum
break
end
end
if (onGround == false) then
local timer = os.startTimer(momentum)
while true do
local event,timerID = os.pullEvent("timer")
if (timerID == timer) then
if not onGround then
paintutils.drawPixel(x,y,2048)
y = y+1
if (momentum - .1 >= maxmomentum) then
momentum = momentum - .1
end
break
else
momentum = minmomentum
break
end
end
end
end
end
)
paintutils.drawPixel(x,y,16)
for i=1,#wallpos,2 do
paintutils.drawPixel(wallpos[i],wallpos[i+1],128)
end
end
Edited by Thegameboy, 10 April 2014 - 05:45 PM.
#6
Posted 10 April 2014 - 10:39 PM
But in your key checking function, you check one event and that's it. The function's finished, and won't restart until parallel.waitForAll() is called again. I suspect what you want to do is stick a "while true do" loop in that function (so that it can't finish on its own), and switch to parallel.waitForAny().
Or, you could merge your event checks using code along the lines of:
event,arg1 = os.pullEvent()
if event == "key" then
for i=-1,#wallpos,2 do
if arg1 == 203 then
***etc***
end
end
elseif event == "timer" and not onGround then
if arg1 == timer then
if not onGround then
***etc***
end
end
end
#7
Posted 11 April 2014 - 03:17 PM
Bomb Bloke, on 10 April 2014 - 10:39 PM, said:
But in your key checking function, you check one event and that's it. The function's finished, and won't restart until parallel.waitForAll() is called again. I suspect what you want to do is stick a "while true do" loop in that function (so that it can't finish on its own), and switch to parallel.waitForAny().
Or, you could merge your event checks using code along the lines of:
event,arg1 = os.pullEvent() if event == "key" then for i=-1,#wallpos,2 do if arg1 == 203 then ***etc*** end end elseif event == "timer" and not onGround then if arg1 == timer then if not onGround then ***etc*** end end end
Wow, I can't believe I didn't catch that. Thanks. But how do you actually multitask (like multiple os.pullEvents at a time) without the parallel API? Because I know that you can't pull an os.pullEvent in a coroutine because it's practically the exact same thing as a coroutine.yield(). I could look in the actual code of the parallel API but it looks a bit daunting and I'll do that as a last resort. I want to know because I don't want to always have to rely on the API. Any suggestions?
#8
Posted 11 April 2014 - 04:24 PM
#9
Posted 11 April 2014 - 11:40 PM
Pull an event (any event), if it's one of the ones you want, act on it, if it's not, ignore it. Loop as desired.
#10
Posted 12 April 2014 - 04:49 PM
CometWolf, on 11 April 2014 - 04:24 PM, said:
Bomb Bloke, on 11 April 2014 - 11:40 PM, said:
Pull an event (any event), if it's one of the ones you want, act on it, if it's not, ignore it. Loop as desired.
Thanks for the help everyone.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











