Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Writing a delay function — Gideros Forum

Writing a delay function

DudDud Member
edited April 2012 in Building a team?
ok I'm a total noob at Lua so if someone can point out my stupid mistake I'd be grateful!

I wrote a simple delay function to basically do-nothing for a given period of time. I've placed a call to my delay function in between 3 different function calls. But it doesn't seem to delay in between each function - instead it still seems to call all 3 functions AFTER waiting the combined amount of delay of time. Here is my delay function:


function delay(seconds)

local startTime = os.timer()

while (os.timer() - startTime) < seconds do
--Nothing
end

end

And here is how I am trying to use it:

callFunction1()
delay(5)
callFunction2()
delay(5)
callFunction3()

But what seems to happen is the system waits 10 seconds and THEN calls all 3 functions.

Is there a better way of doing this? I know about timers but they seem to be set up to call the SAME function at set intervals which isn't quite what I want.

Thanks

Comments

  • avoavo Member
    I'm not sure but I think the while loop is locking up the program, since it tries to execute everything at the same time.

    Anyway I think delayedCall is perfect for what you are looking for? http://www.giderosmobile.com/documentation/reference_manual.html#Timer.delayedCall
  • Here are some ways to manage what you are after
    1. Have a timer and chain all of your functions to run one after the other
    2. Create a queue that processes/chains your functions one after the other
    3. Have a blocking call that waits till that time is up, but the problem with that is everything else freezes.
    4. Once we get sockets, use a socket timeout to delay execution for that duration.
    5. calling the delayed call as suggested by @avo

    Please note that the order of the suggestions is not in any particular order, it's just my approach to how I would have tried to resolve this.

    The way lua works, there is no loop for ever and a doEvents() to catch your breath. The closest thing to that is coroutines, but then you will need a near endless loop to keep that going.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • DudDud Member
    Thanks for the reply guys. I had considered DelayedCall until I realised I would have to chain functions together to get the effect I wanted and chaining seemed a bit of a 'hard-coded' approach.

    However now that you have confirmed that I wasn't doing something crazy I shall reconsider DelayedCall.

    Many thanks.
  • techdojotechdojo Guru
    edited April 2012
    Looking at the problem from a different perspective, what I tend to do is run a single onEnterFrame listener for my whole project and then use a finite state machine to control what the app is doing at any given time (granted this is a little more awkward in lua seeing as it doesn't have the equivalent of the C switch statement - but a workaround can be done with tables - like most things in Lua :) )

    To implement a delay function like you requested I'd just have a "delay" state which when entered would set a counter for the number of frames to "delay" and then when that number of frames have passed, I'd move to the next state (again this would normally be setup prior to entering the delay state).

    If you need something more flexible you could probably use a table like a kind of timeline script, where by you could have a delay counter and then a reference the function that you call next which you could then walk through in some kind of master loop timeline parsing function.

    Hope I haven't confused you too much (it's still early here and I haven't had a cuppa yet! :) )
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • DudDud Member
    techdojo - yes this is the approach I will have in place eventually although what I am doing is simply trying to add some buttons to a menu class with a slight delay between each button being added.

    It is such a small feature I wouldn't want to "pollute" my main finite state machine with a state just for adding buttons so I was hoping for a more elegant approach that was self-contained within my Menu class
  • I know it's a bit of a hack but what about running a tween on an object for the given time and then calling your function in the "onTweenComplete" event?, you could even "hide" the tween by using it on an object that is alpha'd out or not on screen.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • DudDud Member
    I have it working now. I use a GTween to bring my menu on screen, then when that completes it calls the first of my other functions. Each of those functions has a DelayedCall to the next function and so on. So basically chaining the functions together. Seems to work quite well :)
Sign In or Register to comment.