GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HyperLoops - Loop to your hearts content

domeniusdomenius Posts: 108Member
edited March 2013 in Working with GS (Mac)
Greetings GS community! I come to you today with a new method of looping that breaks through the traditional barriers placed on GameSalad loops, allowing GameSalad developers to perform an unprecedented number of loops within a short timespan. Introducing HyperLoops; the latest tool in your arsenal of GameSalad tricks. Along with this post which outlines how HyperLoops work, I have created a demo project which allows you to see and experience the power of HyperLoops for yourself. Look for the link near the bottom of this post.

Traditionally, we think of GameSalad as having a limit of performing rules only once every frame. Traditional loop types most programmers are used to, such as do-until or while loops, have no equivalent in the GameSalad command set. This imposes a major limitation, especially if you've ever tried to search for a specific element in a Table when its row is unknown. HyperLoops allow you to perform an unlimited number of consecutive instructions, all within a single frame of execution.

When a HyperLoop is performed, the application comes to a full stop until the HyperLoops are done processing. With iterations of around 100 or less (depending on the target platform), the pause is fairly short. However, if we need to perform more loops, the hang in processing will be visible to the user. In some cases, such a pause in execution doesn't matter. In other cases, it may reduce the quality of the user experience. To combat this effect, and to speed up processing, HyperLoops provide the option to spread the calculation over multiple frames. This allows large sets of data to be searched or constructed with very little impact on the user experience.

To create a HyperLoop, we make use of two actors and five attributes. The attributes involved and their usage is outlined below:
totalIteration(integer) - The number of times the HyperLoop has been performed thus far
maxIteration(integer) - The total number of times to run the HyperLoop operation
currFrameIteration(integer) - The number of loops we have performed this frame
maxFrameIteration(integer) - The number of times the HyperLoop should be run each frame
currLoopActive(boolean) - True if the loop is running, false otherwise

The main actor is the HyperLoop itself. When a HyperLoop is spawned, it first increments the totalIteration and currFrameIteration values. Next, we insert the code to be run with every loop. This could be anything from a table lookup to a mathematical computation. After we have performed our needed action, we compare our totalIteration and currFrameIteration values against the maxIteration and maxFrameIteration values. If both have not been reached, we spawn a new HyperLoop actor directly behind the existing one, then destroy our current HyperLoop. If the maxFrameIteration has been reached, we simply destroy the HyperLoop and do nothing. If maxIteration has been reached, we switch our currLoopActive boolean to false and destroy the HyperLoop. When setting your maxFrameIteration value, try and pick a value that maxFrameIteration is evenly divisible by when possible. This ensures optimal processing times.

The second actor is an invisible controller actor. This actor (named ctrlrBottom in the accompanying project) should always be the last actor in the layer list. HyperLoops halt game execution while they are running. This means that an actor placed on the very bottom of the game layers is guaranteed to be run after all the HyperLoops have been processed. If the currLoopActive boolean is flagged true, the bottom controller resets the currFrameIteration to 0 and spawns a fresh HyperLoop actor in the front of the layer. This HyperLoop is not run this frame, as we are now at the very bottom of the layer. The frame is allowed to advance before the freshly spawned HyperLoop is executed.

When utilizing HyperLoops, it is highly recommended to take advantage of the chunking system, even with smaller sets of data. The actor spawn and destroy system in GameSalad seems to impose a compound overhead until the end of a frame is reached, which vastly affects processing speeds. If you chunk the processing into two equal parts, you are still guaranteed to complete all the processing before execution is allowed to complete one entire loop. While the two parts are technically in two different loop executions, the second is performed at the very top of the layer, ensuring it is processed before anything else. This allows GameSalad to properly clear the destroyed actors and start fresh. With large datasets, chunking them into smaller parts will almost always yield better performance. Using the demo provided, you can adjust the maxIteration and maxFrameIteration and see the difference in completion speed as you do so. Fine-tuning the number of chunks used is an easy way to speed up a potentially troublesome calculation.

At long last, here is the link to the demo project itself:

http://www.dblostudios.com/hyperloop/HyperLoop_v2.gameproj.zip

The project is well documented and includes notes explaining the code in further detail than this post. In addition to performing HyperLoops, the demo keeps track of how long each loop takes, as well as other real-time statistics on the HyperLoop operation. It's a great place to start when considering HyperLoops in your own project. HyperLoops aren't always the solution to your looping problems, but they are a unique and interesting way to consider handling large sets of data. My own testing on my macbook allows 60 iterations to process at 30 per frame in ~0.03 seconds and 1000 iterations at 250 per frame in ~0.83 seconds. While the processing surely isn't quite that fast on mobile devices, its leaps and bounds ahead of alternative methods.

Let me know if you have any comments, questions or concerns; I'm happy to help.

Domenius

Comments

  • SnapFireStudiosSnapFireStudios Posts: 1,603Member
    Awesome!! I will certainly check it out B-)
    - Thomas

    Ignore females. Acquire currency.

  • tatiangtatiang Posts: 11,893Member, Sous Chef, PRO, Senior Sous-Chef
    Fantastic! I really appreciate that you took the time to explain it all and to post the improvements you've made to (and the flexibility of) this method of looping.

    Some initial benchmarks (60/30 refers to 60 iterations over 30 frames):

    60/30
    GS Creator Preview (iMac): 0.03 seconds
    iOS Viewer (iPad 2): 0.07 seconds

    500/100
    GS Creator Preview (iMac): 0.5 seconds
    iOS Viewer (iPad 2): 1.5 seconds

    5000/100
    GS Creator Preview (iMac): ~7 seconds
    iOS Viewer (iPad 2): ~20 seconds

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Posts: 2,836Member, Sous Chef, PRO
    @domenius -- this is really great stuff! Thanks for sharing this with the community.
  • SocksSocks London, UK.Posts: 12,822Member
    @domenius

    Brilliant !!!

    :)>-
  • ShaunEleryShaunElery Posts: 148Member, PRO
    Very cool
  • domeniusdomenius Posts: 108Member
    Thank you all, especially tatiang for your benchmark results and assistance during the final stages of creating this method! The results you posted above definitely showcase the power of HyperLoops.

    I've been doing some testing of my own on larger iteration values to determine optimal values for iterations each frame. Using a total of 2000 iterations, the loop completion times (recorded using a stopwatch to avoid miscalculation) for various maxFrameIteration values are outlined below:

    50 - 2.719 seconds

    100 - 1.875 seconds

    150 - 2.094 seconds

    200 - 1.875 seconds

    250 - 2.962 seconds

    500 - 5.438 seconds

    As you can see, maxFrameIteration values of 100-200 seem to provide the best results. Past these values, we see a sharp decrease in performance. I assume this is caused by the GameSalad spawn/destroy system being overloaded and creating slowdowns.
  • SocksSocks London, UK.Posts: 12,822Member
    edited March 2013
    @domenius

    Cheers for the additional info . . . . so if I understand this correctly, when maxFrameIteration is set to 100 or 200 the process will iterate 2,000 times in 1.857" ?

    I was doing my own - somewhat less scientific - tests yesterday, and even going fairly heavy on the thing that was being iterated (spawned actors with several rules) it was still blisteringly fast !

    There should be a community badge for 'bright ideas' or innovation - both this and RThurman's home grown interpolations would get my vote.
  • tatiangtatiang Posts: 11,893Member, Sous Chef, PRO, Senior Sous-Chef
    @domenius you're welcome! But you did the heavy lifting. :)

    @Socks I think those times are Preview times and not device times. Still awesome but something to keep in mind.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • beefy_clyrobeefy_clyro Posts: 5,390Member
  • SocksSocks London, UK.Posts: 12,822Member
    edited March 2013
    tatiang said:

    @Socks I think those times are Preview times and not device times. Still awesome but something to keep in mind.

    Ah ! Of course ! Thanks for the clarification . . . Looking at the numbers in your post above can we presume that device times will be around x2-x3 ? (but like you say still insanely fast !).
  • tatiangtatiang Posts: 11,893Member, Sous Chef, PRO, Senior Sous-Chef
    Socks said:

    Looking at the numbers in your post above can we presume that device times will be around x2-x3 ?

    I would say yes although domenius has probably tested it a lot more than I have.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • domeniusdomenius Posts: 108Member
    The times I mentioned are from the viewer. I need to renew my apple developer license to get times off device! I need to do that shortly one way or another so once I do I'll build it out ad-hoc and do some serious testing. In the meantime, if anyone does testing of their own I'd be glad to hear the results!
  • KingChaosKingChaos Posts: 9Member
    @domenius, is there another location that the HyperLoops project might be located? I went to dblostudios.com but my browsers say the site has a virus and or the zip file can't be found. (firefox, chrome, safari, ie). Thanks!
  • fox2fox2 Posts: 67Member
    @domenius this link could not connect..I want a get the this example.. always thanks ..
    http://www.dblostudios.com/hyperloop/HyperLoop_v2.gameproj.zip
  • tatiangtatiang Posts: 11,893Member, Sous Chef, PRO, Senior Sous-Chef
    edited March 2016
    @fox2

    [this attachment isn't working... see post below for new attachment]

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • fox2fox2 Posts: 67Member
    @tatiang Thanks a lot!
  • louistrudellouistrudel Posts: 15Member, BASIC

    Awesome post! i love the timers are for chumps too! is there any chance to get a link to the template? it seems to be broken on this post

  • tatiangtatiang Posts: 11,893Member, Sous Chef, PRO, Senior Sous-Chef

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Sign In or Register to comment.