Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » What can I do to optimize/reduce slowdown in my project?
- 101Corp
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
Heya! My recently published project, an accurate Geometry Dash recreation, is suffering from a big problem - lag. Lots, and lots of lag. Stuttering, choppiness, and a general lack of smoothness (compared to something like Geometry Dash v1.5 or Accurate Geometry Dash v8). I'm on a pretty good PC too. I want to know what I can change and how I could change it to get my project to not stutter and lag behind. Any remixes you make to add optimizations would also be much appreciated. Thank you!
Last edited by 101Corp (Oct. 5, 2024 03:28:32)
- 50_scratch_tabs
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
I can't debug it real hard as I'm on my phone, but you should play around with custom blocks with “run without screen refresh” checked. Just never run an infinite loop without wait blocks. Basically:
instead of
Make sure that intensive function has run without screen refresh checked, otherwise there's no point.
define intensive function
...
when I receive [update v]
intensive function
instead of
when I receive [update v]where
...
...is one iteration of a laggy loop.
Make sure that intensive function has run without screen refresh checked, otherwise there's no point.
- 101Corp
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
Ok, I’ll try this. Anybody else have any ideas? I can't debug it real hard as I'm on my phone, but you should play around with custom blocks with “run without screen refresh” checked. Just never run an infinite loop without wait blocks. Basically:define intensive function
...
when I receive [update v]
intensive function
instead ofwhen I receive [update v]where
......is one iteration of a laggy loop.
Make sure that intensive function has run without screen refresh checked, otherwise there's no point.
- awesome-llama
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
The way you are spawning tiles is very inefficient, you are constantly iterating over thousands of list items and to make it worse, thousands of times per second you are using the list contains block (this is a slow block as it just does a linear search over the entire list).
What you would want to do is ensure your lists of tiles are structured or ordered in some way, for example you could sort the items to that they appear in order of when they appear in the level. This may already be the structure, I don't know. If they are ordered like this, you do not have to iterate over the entire list. You could keep track of the region in the list to iterate over. Remember that there is a “stop this script” block, you can use it in cases where the loop has no need to run any further (or you can use a repeat until block, depends on the situation).
An alternative method would be to delete the items from the list. This could solve your need for checking if a tile has already been spawned - if it has been spawned, remove it from the list.
Another alternative is to do some sort of chunking. For example you could split the level up into slices that are say 50 tiles wide (any number works but you would have to experiment to see what is the most optimal). From there instead of checking at a per-tile level whether it is on screen or not, you can check whole chunks and exclude all the tiles within them from being handled. One of the ways that this can be done is to have a list of chunks and pointers (list indices) into the list of tiles you already have. Each chunk would also need to store the number of tiles they contain and possibly its position in the level. There are many different solutions.
What you would want to do is ensure your lists of tiles are structured or ordered in some way, for example you could sort the items to that they appear in order of when they appear in the level. This may already be the structure, I don't know. If they are ordered like this, you do not have to iterate over the entire list. You could keep track of the region in the list to iterate over. Remember that there is a “stop this script” block, you can use it in cases where the loop has no need to run any further (or you can use a repeat until block, depends on the situation).
An alternative method would be to delete the items from the list. This could solve your need for checking if a tile has already been spawned - if it has been spawned, remove it from the list.
Another alternative is to do some sort of chunking. For example you could split the level up into slices that are say 50 tiles wide (any number works but you would have to experiment to see what is the most optimal). From there instead of checking at a per-tile level whether it is on screen or not, you can check whole chunks and exclude all the tiles within them from being handled. One of the ways that this can be done is to have a list of chunks and pointers (list indices) into the list of tiles you already have. Each chunk would also need to store the number of tiles they contain and possibly its position in the level. There are many different solutions.
- 101Corp
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
Would it help to do 2 objects per loop? The way you are spawning tiles is very inefficient, you are constantly iterating over thousands of list items and to make it worse, thousands of times per second you are using the list contains block (this is a slow block as it just does a linear search over the entire list).
What you would want to do is ensure your lists of tiles are structured or ordered in some way, for example you could sort the items to that they appear in order of when they appear in the level. This may already be the structure, I don't know. If they are ordered like this, you do not have to iterate over the entire list. You could keep track of the region in the list to iterate over. Remember that there is a “stop this script” block, you can use it in cases where the loop has no need to run any further (or you can use a repeat until block, depends on the situation).
An alternative method would be to delete the items from the list. This could solve your need for checking if a tile has already been spawned - if it has been spawned, remove it from the list.
Another alternative is to do some sort of chunking. For example you could split the level up into slices that are say 50 tiles wide (any number works but you would have to experiment to see what is the most optimal). From there instead of checking at a per-tile level whether it is on screen or not, you can check whole chunks and exclude all the tiles within them from being handled. One of the ways that this can be done is to have a list of chunks and pointers (list indices) into the list of tiles you already have. Each chunk would also need to store the number of tiles they contain and possibly its position in the level. There are many different solutions.
- awesome-llama
-
1000+ posts
What can I do to optimize/reduce slowdown in my project?
If you are asking whether this is faster… Would it help to do 2 objects per loop?
repeat (1000)
...
...
end
than this…
repeat (2000)
...
end
the answer is no, they will be about the same speed.
- Discussion Forums
- » Help with Scripts
-
» What can I do to optimize/reduce slowdown in my project?