While loops are possible in Hopscotch!


#1

While loops are possible in Hopscotch! Read below to find out what they are and how to use them.

What are while loops?
While loops are used to repeat an event until something is true, or until a certain thing happens. It’s like ‘repeat forever check if’ but it doesn’t repeat forever. This can be used for a variety of types of projects, and it is really useful.

How do you make a while loop?
While loops can be made by putting abilities inside of abilities.

First, create a new ability.


Next create an if else statement for whatever event you want to repeat.

If [the event hasn’t happened yet] then [keep repeating the same steps]
Else [do the end result]

This code is saying the monkey will continue to move forward until its x value is greater than or equal to 700. Once that happens, jump and flip.

The monkey continues to move forward because the ability is placed at the end of the ‘if self x < 700’

Let me know if you have any questions and I hope this is helpful!
-Madi-


#2

I wish you didn’t have to make an ability to utilize it. :( It adds clutter to the abilitiies area in the block palette every time you add a while loop, and woth a bunch of them it can add up. Plus, there can be a lot of lag with a constant ticking event (something in the loop that keeps on happening super fast, like every 0.01 seconds or some fast speed like that), especially since abilities may or may not be slower than normal blocks from what I remember (?).

It’d be a useful thing to add to the app instead of having to use a forever loop and an if statement though. Those cause so much extra lag in a project…! Honestly, probably the number one reason why major lag in projects can happen - so this definitely has a practical use. :)


#3

I’ve used these while loops in Hopscotch before.


#4

These are some great points, thanks!


#5

Cool concept! I would really like to see true “while”-loops integrated in Hopscotch though. They are really useful.


#6

Hi @Madi_Hopscotch_
Thank you for sharing with the community.

If I may offer some constructive feedbac k, I would suggest a better implementation for Do While or Do until loops is not to use a Check If Else loop but rather a Check Once If.

Using your example, like this:

This segregates all of the “Else” actions so they’re outside the “Do While” custom ability. This allows for cleaner code as the subsequent blocks have nothing to do with the “Do While” code. Also, this allows the “Do While” ability to be reused in other places where the code that follows is different.


@Rawrbear
Using custom abilities and loop recursions doesn’t create any lag. It’s all in how the programer chooses to use the code and in how well the programer understands how the code is executed.

In the 2 isolated code snippets shown above there is a very minor difference in how the Player will execute the code.

Do you see it?

First off, I’ll mention that while in the loop the Change X by 100 block executes every frame until the loop is exited. During the intra-frame execution, the ability “title block” continues directly to the first code block. In this case it’s a Check If, which evaluates the comparison and if true continues directly to the first code block within. In this case it’s a Change X By block. So, while the comparison is true, the Change X By is executed every frame with no “lag” penalty.

Now, in Madi’s example, when the Check If Else is false it continues directly to the first Else block (within the same frame). In my example, when the Check Once If evaluates to false it does not immediately continue. The first block after the Check Once If is executed during the next frame.

Because of that delay, there’s no way to make my example execute exactly the same as Madi’s. My version would be similar to Madi’s if it was written like this with an extra Wait 0 block:

So technically the method I showed does introduce 1 frame of “lag”, but in general I still think it’s a better practice. :slightly_smiling_face:


#7

I see it. It should only be the case when using two sets of loops. If three or more, should we use a Check Once If block still or use a Check If Else one?


#8

What about creating a new object value and change the rule to when: (self) loop = 0 and then put the check once if else block but in the else, put a set value block that’s sets loop to whatever value you want. There are many ways to do the while loop.


#9

Yes, that would absolutely work. However, it has been tried and proven that Repeat Forever blocks are less laggier than Rules.


#10

When did I say repeat forever?


#11

@Madi_Hopscotch_, can you try making a while loop using If Pressed?


#12

You said Rules, though. They’re the purple block.


#13

That’s different. Yes, you can create a loop using purple Whens. But those can’t be embedded within other sequences of code.


#14

Actually, they can. You’ll just need to set/increase the (self) loop = number.
This way, you can even choose the loop you would like to use!


#15

Precisely. This is the method I use.


#16

@Yusamac205, @DECODECO, et al

Here’s a few points to consider. Many Hopscotchers have caught on that a really easy way to exponentially (very quickly) create clones is like this:

However, every Rule (When) we use creates some load on the Player Rule Engine. So the above rule quickly creates clones, but this continues to be evaluated by the Rules Engine every frame during the project (even though it can never again be true). That additional load may or may not be noticeable. It’s up to the programer to decide if it matters for their particular project.

However, let’s say that we do care. How can the same exponential cloning be coded without a When rule that has to be continually evaluated? It can be done with a “Do While” recursion like this:

That’s definitely more complicated, right? But more efficient code frequently means more complex code.

Now, to the real point. Let’s say you wanted to create clones of object#2 only after clones of object#1 were created (this matters for advanced projects with specific timing requirements). How can we do that? Adding another Do While recursion before the cloning recursion offers an easy, efficient solution:

Now in this example from my in-process Text Adventure project, all of the clones for the Message object will be created after the clones of the Items object.


#17

Yes, of course, you can code that way. But it doesn’t accomplish the same result in an easy to follow sequential manner.

However, there may be times when it’s better to write the code with multiple When rules instead of sequential recursive abilities. Your point of being able to select which loop runs is a great example.

There’s no “one right way” that always is best. That’s the fun part about programming. You have to decide the best way to write the code for every unique application. Recursive Do While loops are just another “tool in the toolkit”, that every advanced Hopscotchers should be familiar with.


#18

Another way to create clones really fast, is to add a parameter to the create a clone block, like this:


This seems to create a large amount of clones in one frame (I tested up to a thousand I think)

There’s an example, with 300.


#19

I saw some posts about the new blocks you found. Pretty cool, but since most Hopscotchers don’t have access to these blocks, we probably shouldn’t muddy the water in more general contexts :wink: