Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with laggy scripts.
- lunaluvgood
- Scratcher
100+ posts
Help with laggy scripts.
I was trying to make a “spray paint” project. However, it was either really laggy, or just stopped making clones. Please take a look, I put a custom block so it ran without screen refresh. http://scratch.mit.edu/projects/10314520/
- DadOfMrLog
- Scratcher
1000+ posts
Help with laggy scripts.
One thing to avoid is any kind of “wait” inside a no-screen-refresh block.
Remember that nothing else can happen while such a block is running, so it will lock up the flash player until it finishes - and it'll then be way behind on other ‘bookwork’ that it wants to do, and will never get chance to catch up again - hence the lag.
Similarly, note that “broadcast and wait” within a non-refresh block will bring about the same result - because it's waiting for the receiving script to finish, but that can't even get started until the non-refresh script is finished (although there is a time-limit of ~1sec for non-refresh, but it still brings everything to a crashing halt if it gets that far…)
Hope that helps!
Remember that nothing else can happen while such a block is running, so it will lock up the flash player until it finishes - and it'll then be way behind on other ‘bookwork’ that it wants to do, and will never get chance to catch up again - hence the lag.
Similarly, note that “broadcast and wait” within a non-refresh block will bring about the same result - because it's waiting for the receiving script to finish, but that can't even get started until the non-refresh script is finished (although there is a time-limit of ~1sec for non-refresh, but it still brings everything to a crashing halt if it gets that far…)
Hope that helps!
Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…
- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -
- dracae
- Scratcher
1000+ posts
Help with laggy scripts.
The reason I think it's really laggy is…
~Not because of the amount of clones.
~Not because of the amount of wait time.
The reason it's laggy is because (I think) of the sheer amount of broadcasting.
I'm fairly sure broadcasting is very lag-inducing, so you could fix the project by using, say, variables.
Hope this helps!
(If you're stuck, or want to see my version (less lag), clicky!
(EDIT: My project still has a few glitches, but it fixed the lag problem! )
(EDIT: And it lessened the script amount. I fixed all of the things I listed above, so don't quote me! )
~Not because of the amount of clones.
~Not because of the amount of wait time.
The reason it's laggy is because (I think) of the sheer amount of broadcasting.
I'm fairly sure broadcasting is very lag-inducing, so you could fix the project by using, say, variables.
Hope this helps!
(If you're stuck, or want to see my version (less lag), clicky!
(EDIT: My project still has a few glitches, but it fixed the lag problem! )
(EDIT: And it lessened the script amount. I fixed all of the things I listed above, so don't quote me! )
Last edited by dracae (May 19, 2013 02:59:13)
- DadOfMrLog
- Scratcher
1000+ posts
Help with laggy scripts.
OK, yes, I also noticed you've fallen for the ‘clone explosion on broadcast’ problem…
When creating clones you should be aware that all clones receive and act upon broadcasts. So if you have a receive broadcast that creates another clone, *all* of the existing clones will do that, not just the original sprite.
The way to deal with it is to have the sprite set a local variable (say “iamaclone”) to zero, and the first thing a clone does is set it to one (because local vars are also local to each clone meaning they can take independent values per clone), so you can always get a script (typically from a receiving broadcast) to check if it's being run within a clone or not.
I fell into this trap a couple of times myself…
However, I think you can probably rewrite your scripts so that broadcasts are not really necessary at all, so I'd suggest you have a go at that.
Anyway, I've remixed an example for you to look at - I think it might be along the lines of what you're after? (And I've included a slider so you can increase the spray scatter area). Plus comments to explain what it's doing.
However, I switched to using ‘stamp’ instead of clones, since it's considerably faster, and they don't have to be deleted (though maybe you have a reason for doing that…?)
You can switch back to clones using the “Clones” switch top-left.
Hope that helps!
When creating clones you should be aware that all clones receive and act upon broadcasts. So if you have a receive broadcast that creates another clone, *all* of the existing clones will do that, not just the original sprite.
The way to deal with it is to have the sprite set a local variable (say “iamaclone”) to zero, and the first thing a clone does is set it to one (because local vars are also local to each clone meaning they can take independent values per clone), so you can always get a script (typically from a receiving broadcast) to check if it's being run within a clone or not.
I fell into this trap a couple of times myself…
However, I think you can probably rewrite your scripts so that broadcasts are not really necessary at all, so I'd suggest you have a go at that.
Anyway, I've remixed an example for you to look at - I think it might be along the lines of what you're after? (And I've included a slider so you can increase the spray scatter area). Plus comments to explain what it's doing.
However, I switched to using ‘stamp’ instead of clones, since it's considerably faster, and they don't have to be deleted (though maybe you have a reason for doing that…?)
You can switch back to clones using the “Clones” switch top-left.
Hope that helps!
Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…
- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -
- dracae
- Scratcher
1000+ posts
Help with laggy scripts.
Anyway, I've remixed an example for you to look at - I think it might be along the lines of what you're after? (And I've included a slider so you can increase the spray scatter area). Plus comments to explain what it's doing.
However, I switched to using ‘stamp’ instead of clones, since it's considerably faster, and they don't have to be deleted (though maybe you have a reason for doing that…?)
You can switch back to clones using the “Clones” switch top-left.
Hope that helps!
Nice remix!
- Bob_MAD_FNF
- Scratcher
7 posts
Help with laggy scripts.
define (l)(a)(g)(g)(y)
move (lag) steps
move (gy) steps
Last edited by Bob_MAD_FNF (Nov. 13, 2021 17:59:22)
- Discussion Forums
- » Help with Scripts
- » Help with laggy scripts.