Discuss Scratch

Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

I have a project that uses about 120 sprites and the frame rate plummets when I use the “set costume” block. All the other “Looks” blocks seem fine and don't have this performance drop.

Why is “Set Costume” so slow? Can it be made faster with ease Scratch Team? I realise this is an educational tool so performance isn't top of your to-do list.
OmnipotentPotato
Scratcher
1000+ posts

"Set Costume" is slow

Why do you need 120 sprites? That's the reason for your lag right there I bet. Have you looked into cloning?
AonymousGuy
Scratcher
1000+ posts

"Set Costume" is slow

It's because Scratch doesn't keep the assets loaded, thus whenever you switch costumes it has to re-load the image.
Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

@OmnipotentPotato I've made a texture mapped 3D ray casting engine with 120 rays cast hence 120 sprites. I can do other operations on all of them like “set size” without any lag issues, it's ONLY “set costume” that is a problem. And yes they're already clones.

@AonymousGuy Ah I suspected something like that, thanks. Seeing as they don't change at run time is there a reason these assets can't be loaded at start up and just kept hanging around until needed? (you're going to say ‘to keep memory down’ aren't you)
djdolphin
Scratcher
1000+ posts

"Set Costume" is slow

Finlay_Cool wrote:

@AonymousGuy Ah I suspected something like that, thanks. Seeing as they don't change at run time is there a reason these assets can't be loaded at start up and just kept hanging around until needed? (you're going to say ‘to keep memory down’ aren't you)
Everything is loaded on start up and kept until needed. AonymousGuy was wrong about that.
AonymousGuy
Scratcher
1000+ posts

"Set Costume" is slow

djdolphin wrote:

Finlay_Cool wrote:

@AonymousGuy Ah I suspected something like that, thanks. Seeing as they don't change at run time is there a reason these assets can't be loaded at start up and just kept hanging around until needed? (you're going to say ‘to keep memory down’ aren't you)
Everything is loaded on start up and kept until needed. AonymousGuy was wrong about that.
Huh. Sorry. I guess I was told incorrectly.

Ignore what I said then.
WeezerMusic
New Scratcher
2 posts

"Set Costume" is slow

The way I deal with this in my projects is by writing code that doesn't wait for the block to finish before continuing. I can explain more if you want.
Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

@WeezerMusic yes a sort of ‘parallel processing’. I tried that out but it didn't gain me any speed.

@djdolphin so if it's not a caching problem any clues as to what it might be?
TheLogFather
Scratcher
1000+ posts

"Set Costume" is slow

I can't say I really understand why you need any costumes at all for ray casting - can you explain in more detail exactly what you're using them for? Also, why you actually need to switch costume…?

There's not really any such thing as ‘parallel processing’ in Scratch, unfortunately - everything is done in serial, even if all the work is ‘interleaved’ so the different things appear to be happening in parallel.

To explain a bit more about ‘set costume’… how slow it is will depend on the actual costumes. If you have a completely blank costume then it will be extremely quick. A single dot is also quick. A vector costume that has lots of elements will be slow. Also, if your sprite has effects and/or rotations applied when you do the switch, that will slow it down *a lot*.

If you could share an example project that shows the issue, then it's possible we may be able to advise how to improve it…


EDIT: Ah, is it your Wolfenstein project… OK, I see - trying to use costumes for the walls… hmmm, interesting… will take a look…

Last edited by TheLogFather (Oct. 13, 2014 10:47:04)

Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

First of all thanks for the help. I really appreciate it.

My objective, when I started my project, was to create a texture mapped 3D ray casting engine. There's plenty of examples that use the pen to render the walls but I've chosen to use cloned sprites to draw each of the vertical strips (one for each ray cast, 120 in my case). These sprites will have their scale and costume set depending on how far away they are from the camera and which vertical part of the texture needs to be visible (I've got over 1000 costumes assigned to my cloned ‘Wall’ sprite).

My project is here. It starts at the lowest detail level but it you press ‘T’ you can change through the different detail methods until you get to level 4. The ONLY difference between the 4 methods is the frequency the costumes change. Level 1 hardly changes at all, in fact only the doors show as different. Level 4 will effectively change costume on all 120 sprites every frame because it's unlikely you'll see the same vertical strip of texture between frames. So yeah it runs at about 4 fps on my machine.

I had considered using pen and manually texturing the vertical strip myself but I'd assumed that would be slower than changing costume.
TheLogFather
Scratcher
1000+ posts

"Set Costume" is slow

Yes, switch costume will be way too slow to do what you want - there's really not much hope of making it fast enough for this kind of technique. (There is a reason why it's been done with pen, rather than like this, y'know…)

However, I have been considering a way to do a costume-textured raycaster using a roughly similar idea, but with stamping - as long as there are several sizeable chunks of the wall that use the same costume, then I think it might just work. I don't have time to put it together at the moment, unfortunately.

Last edited by TheLogFather (Oct. 13, 2014 11:04:04)

Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

Ok thanks for your help. I can only presume costumes are cached in this, relatively, expensive way (rather that all cached at load time) is to keep memory down?
TheLogFather
Scratcher
1000+ posts

"Set Costume" is slow

I suspect set costume is working about as efficiently as it can, but what you're asking it to do is actually much more than that…

In your render maze script you do this:

1) change the size of the existing costume - this means it has to take the original (cached) bitmap for the current costume, and interpolate the pixels to create a new bitmap at the specified size.
2) change the costume - this means it has to take the original (cached) bitmap for the new costume, and then also resize it to the size that is currently set.

There's quite a lot of work to do there (two sets of pixel interpolation, not just getting the actual costume), so don't expect it to be quick!

Last edited by TheLogFather (Oct. 13, 2014 11:26:23)

Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

Ok thanks for your help
Finlay_Cool
Scratcher
31 posts

"Set Costume" is slow

Ok I've managed to increase fps from 2-4 to 4-12 (on my machine at least).

So the underlying problem is changing costumes is slow so I needed to minimise the number of costumes changed on each game frame. This has been done by implementing a caching system or “remember and reuse”.

I have around 200 sprites now, each assigned to a costume. At any point in time only 120 of these sprites are actually visible (one for each ray cast). The rest are available to improve caching. For each ray cast I check to see if the new costume I need is already assigned to a sprite, and if so, i just reuse that sprite in its new position and scale. If however I don't find a matching costume available I grab the oldest sprite (the sprite that was on screen the longest time ago) and assign it the new costume.

You can see the results here. Make sure you press ‘T’ until detail level is set to 4.

If anyone wants further details on anything just ask.

Last edited by Finlay_Cool (Oct. 17, 2014 07:16:45)

AdelineRose
Scratcher
2 posts

"Set Costume" is slow

Hello! I just wanted to know if the more costumes you have in a sprite cause more lag.
nolbarry
Scratcher
100+ posts

"Set Costume" is slow

AdelineRose wrote:

Hello! I just wanted to know if the more costumes you have in a sprite cause more lag.
Hi! It's probably best if you create your topic for a question like this - bumping up old topics by replying to them years later is frowned upon in the forums as it's hard to stay on topic. I'd also include some more information in your post - extra costumes might cause more editor lag but it shouldn't significantly affect project performance unless you have thousands of costumes (even then, I couldn't tell you for sure that that would make a difference). Performance would also be affected by the complexity of the costumes, whether they are bitmap/vector, etc.

Powered by DjangoBB