Broadcast-based game loop is SLOW... why?

Hi, coming from Scratch to Snap! for the more advanced features. I'm experimenting with a game loop, which can be summarized as


This approach works fine in Scratch, but it is causing a great loss of FPS in Snap! By using several unrelated forever loops for each sprite/clones I'm getting way better performances (60-65FPS versus 35-40FPS).

Is this to be expected? Or am I doing this the wrong way? Is there a Snap! tutorial on game loops somewhere?


snap! isn't meant for that

Could you please elaborate further? Thanks.

look; there is no snap tutorial on game loops. the devs were most likely not thinking about game (loops) when they made snap.

I am not sure if Snap! was made for making games

not specifically, at least. they do recognize that you can make games with it, but also that you can make things that aren't games with it.
Snap! is kind of like a kid-friendly newgrounds if you think about it.

I don't really know it's an issue in your specific case, but maybe it's due to broadcast [mesg] and wait being a blocking call.
It waits for each scripts being called to finish before settling to the next instruction.
I think in your experiments with unrelated forever loops, everything was done in parallel.
You could try the broadcast [mesg] block to launch scripts in a similar way.
Maybe this is wanted behavior, as it guarantees that TickGame is executed before TickPlayer and update.
But please keep in mind that this may slow down the main loop if some heavy calculation could've been done in parallel in some of the called scripts.

Another option would be to use the the send [mesg] to [object] block, as it allows to be more specific about what object is the target of your message, and to send the same message to different objects at different times, mainly.
I'm pretty sure that this may comes with some performance improvements.

With all that's said, I'm not sure whether it will help you in gaining performance or not so it's best to try & see


Do you know about the wrap block?
This is the equivalent of the Run without screen refresh option in scratch, but in a more versatile way as it permits to be used on very little specific parts of your code without creating a whole new function ("block").
Maybe it could give you a speed gain too.

Also, it may be interesting for you to look after the tell [object] to [code] as it permits to executes code as another object (you can try tell [player] to [move 10 steps] to get a feeling of what I mean.
You could execute literally any piece of code, be it several instructions, a specific function or even another tell block within the tell block!

And as I'm talking about what things Snap! permits over Scratch, don't forget that functions in Snap! can be defined globally (which means that if you have created a fall function, you could use it in your rock object, but also in your player object).

All of those thing may be interesting to consider when you're trying to design your program!

Also, I think using Snap! for games is totally relevant, even if it's not the main purpose of the language, as it permits to process audio, graphics and input in some very nice and simple ways.

Thank you all for your contribution. Still experimenting and learning :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.