Jump to content




Execute something in a different process


  • You cannot reply to this topic
5 replies to this topic

#1 loustak

  • Members
  • 7 posts

Posted 13 April 2015 - 10: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 !

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 April 2015 - 03: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?

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 April 2015 - 11:25 PM

Indeed, take a look at os.startTimer().

#4 loustak

  • Members
  • 7 posts

Posted 14 April 2015 - 08: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 ?

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 April 2015 - 02: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


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 April 2015 - 09: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users