Benefit of loop behavior

Hey everyone, pretty exciting to see the new features in GS 11. I was looking at the loop behavior and was trying to think of any ways it might help the efficiency of my code. I was wondering if anyone would share the benefits they have found of this code as I haven't found it much different than the rules we already have set up.
For example I don't see it much different than using a rule with a constraint:

Rule version:
If right key is pressed-
Constrain velocity.x to 100

Loop version:
While right key is pressed-
Change velocity.x to 100

This seems to achieve the same thing with first possibly being more efficient

I would love to hear people's examples of how this behavior benefited them, or made things more efficient as it could greatly help me in seeing how I may want to change some things in the game or use the loop function in the future.

Comments

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

    Loops are used when you want to repeat behavior, not when you want to constrain behavior.

    So if you need to spawn 800 actors on a scene (think tiles), you might use one or more loops to generate those actors in a single rule instead of trying to have 800 spawn actor behaviors.

    Or if you need to search through a table of data to find one word out of a dictionary of 150,000 words, you might loop through the rows of words and check each one. Again, coding that without a loop would be a nightmare.

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    Here's a demo I made a long time ago when the loop behavior was released for PROs. As @tatiang said: this demo checks to see if a word is in the table, then displays the row that the word was found in.

    http://gshelper.com/shop/gamesalad-templates/table-word-checker-loop-behavior-demo/

  • RossmanBrothersGamesRossmanBrothersGames Member Posts: 659

    Thanks guys, that makes a lot of sense. Especially the example of searching through rows.
    I think I was trying to look at how my code would benefit from it and there wasn't anything that actually would, but I could see this being something I use in the future.

    Also, I noticed sometimes threads get marked as "answered" is this something a moderator does or something the original author does? I have not seen a way of doing this.

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    If you begin your thread as a question, you will be able to choose the answers. Because this thread wasn't started as a question, there isn't a way to pick an answer.

  • HappyKat78HappyKat78 Member, BASIC Posts: 173

    Does anyone know if the new loop behavior + engine optimizations are working well for line drawing? I've struggled to find good line drawing code that works fast enough. Thanks!

  • UtopianGamesUtopianGames Member Posts: 5,692

    I posted a demo here that might interest you.

    http://forums.gamesalad.com/discussion/66124/table-row-and-column-search-demo#latest

    Darren.

  • HappyKat78HappyKat78 Member, BASIC Posts: 173

    Thanks. I couldn't work out how to open it though as I didn't see a project file (rather just the package contents). I'm on a mac. Thank you.

  • HappyKat78HappyKat78 Member, BASIC Posts: 173

    Ah, makes sense. Thanks

Sign In or Register to comment.