Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can ı use thread concept? — Gideros Forum

How can ı use thread concept?

tugbakayatugbakaya Member
edited February 2013 in General questions
Hello everbody. I am trying to implement thread concept in gideros. I have searched lua structures. There is coroutine basics. But ı cannot be sure about this refers to thread. Can anybody help me about using the thread concepts in gideros?
Tagged:

Comments

  • Why dont you use enterframe instead of tread?

    I think you will use threads for animations or to chance something on the scene after sometime. In these languages like as3 and lua, they have enterframe event instead of thread. You can have a look at the examples on the starting page of the gideros studio. Then you can easily understand enterframe event.

    For animations , You can use movieclips also. In the tutorials you will able to find its examples.

    However , if you want to use thread for other things , there isn't such a code in lua. But you can also create it by the calling a method in the enterframe. for example;

    counter=0
    self:addEventListener(EVENT.ENTER_FRAME,onEnterFrame)
    function onEnterFrame()
    if play then



    end

    thread(100)

    end

    function thread(time)

    if play then

    play=false
    counter=0
    else

    counter=counter+1

    if counter==time then
    play=true
    end

    end

    end

    It should be something like that. I didnt try it on Gideros. Actually , it is not same as real thread.

    BTW , using thread is not good for programming. You should use timers or (as i said before) Enterframe event (Fps).

    Mert
  • You can not use threads in Gideros. And sub routines are still not executed parallel.

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • bowerandybowerandy Guru
    edited February 2013
    @tugbakaya, do you mean native threads or coroutines? @MikeHart is right that you can't use native (operating system) threads in Gideros. At least you can't do this unless you code them down in a plugin and even then they would be of limited use since callbacks into Lua would not be possible since the interpreter is not thread safe.

    However, it is possible to use co-routines fairly easily from Lua if you take a little care. Coroutines are like parallel processes except that they aren't pre-emptive. This means that the OS doesn't allocate a time slice to each one or allow a higher priority process to take over automatically from a lower priority one.

    So what you have to do is run your process (coroutine) for a short time and then it must ask to yield() itself. This must be done inside an ENTER_FRAME event (which is triggered 60 times a second) and the "short time" must be very short if you don't want to cause your animations to stutter. Basically, while your coroutine is running, all Gideros animations will pause.

    I have used this approach in a game that I'm working on to allow animations to continue while an AI Bot does some background working out. Basically, I resume the coroutine in the ENTER_FRAME handler and make a note of the time. As the coroutine works it continuous monitors the time and when 1/30 sec has elapsed it yields itself. This causes the frame rate to drop from 60fps to 30fps while the Bot is working. The animations look slightly "sticky" but are nowhere near as bad as if I didn't use the coroutine at all.

    Here's an example of my onEnterFrame() handler:
    function CockpitBot:onEnterFrame()
    	if self.searchFuncTask then
    		local status=coroutine.status(self.searchFuncTask)
    		if status=="dead" then
    			-- When search is complete coroutine terminates and we can
    			-- make the bot's move.
    			--
    			self:moveCar()
    			self.searchFuncTask=nil
    		else
    			-- Search continuing
    			coSliceTime0=os.clock()
    			coroutine.resume(self.searchFuncTask)
    		end
    	end
    end
    self.searchFuncTask is a coroutine created from a function that performs the AI search.

    best regards

    Likes: phongtt

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.