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

Execute something in a different process

Started by loustak, 13 April 2015 - 09:44 AM
loustak #1
Posted 13 April 2015 - 11:44 AM
Hello,
I just want to know if it's possible to execute something in a diffenrent process (or something like this) because I have blocking functions…
For exemple I have this unfction :

local function ennemiSpawn() -- Fait appraître un ennemi
	    os.sleep(math.random(10))
	    array[0][0] = "$"
end
Or that :

local function readKey() -- Test les entrées clavier
	    local event, key = os.pullEvent( "key" )
	    if key == keys.e then
			    print( "Vous avez appuyer sur E" )
	    end
	    if key == keys.x then
			    running = false
	    end
end

Thanks !
Lyqyd #2
Posted 13 April 2015 - 04:48 PM
This looks like it's for a game of some sort. Have you considered using a single event handling loop and using timers to perform timed actions like spawning enemies?
Bomb Bloke #3
Posted 14 April 2015 - 12:25 AM
Indeed, take a look at os.startTimer().
loustak #4
Posted 14 April 2015 - 09:27 AM
I did this for cheking event whitout blocking :

local function readKey()
	    local timeout = os.startTimer(1)
	    local event, key = os.pullEvent()
	    if key == keys.e then
			    print("E")
	    elseif key == keys.x then
			    running = false
	    elseif event == "timer" then
			    timeout = os.startTimer(1)
	    end
end

I'm right ?
Lyqyd #5
Posted 14 April 2015 - 03:43 PM
Yes, but you'll want to make sure you're getting a key event for those first two comparisons.


if event == "key" and key == keys.e then
Bomb Bloke #6
Posted 14 April 2015 - 10:54 PM
… and for the timer, you'll want to get check its ID:

elseif event == "timer" and key == timeout then

You're likely to run into more trouble if you create a new timer directly before pulling your event. Odds are you want to start one before the loop that calls this function begins, and from then on only create new ones when you pull that specific timer event from the event queue.