Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do I switch to a costume who's first letter is the same as a variable?
- MineBlock137
-
Scratcher
31 posts
How do I switch to a costume who's first letter is the same as a variable?
I'm trying to switch a sprite's costume to a costume who's first letter is the same as a variable that I have.
Currently, my code looks like this:
However, that script takes a while to complete as I have to cycle between 27 different costumes to find the one I intend.
So, I'm looking for a different and ideally faster way to accomplish this. Is there any way? Thank you.
Currently, my code looks like this:
when I receive [... v]All costume's names start with a different letter so there will never be any redundancy / overlap.
repeat until <(letter (1) of [(costume name)]) = (variable)>
next costume
end
However, that script takes a while to complete as I have to cycle between 27 different costumes to find the one I intend.
So, I'm looking for a different and ideally faster way to accomplish this. Is there any way? Thank you.
- medians
-
Scratcher
1000+ posts
How do I switch to a costume who's first letter is the same as a variable?
You mean like this?

You could instead have a list of the costume names of the sprite (or the starting letters of the costumes), and see which one starts with that same letter.

You could instead have a list of the costume names of the sprite (or the starting letters of the costumes), and see which one starts with that same letter.
Last edited by medians (March 30, 2023 01:22:37)
- awesome-llama
-
Scratcher
1000+ posts
How do I switch to a costume who's first letter is the same as a variable?
You can speed up scripts with custom blocks set to “run without screen refresh”. They remove the yield that scratch has for some blocks such as loops.
Your script could instead be this:
An alternative method could involve caching the first letter of each costume in a list. If the first costume started with E, the first list item would be E, and so on.
Then you can do this:
Though you would need a script set up to generate the list or you make it manually.
Your script could instead be this:
when I receive [... v]
switch to a costume starting with (variable)
define switch to a costume starting with (letter)
repeat until <(letter (1) of (costume [name v]::looks)) = (letter)>
next costume
end
An alternative method could involve caching the first letter of each costume in a list. If the first costume started with E, the first list item would be E, and so on.
Then you can do this:
switch costume to (item # of (variable) in [costume first letter v]::list)
Though you would need a script set up to generate the list or you make it manually.
Last edited by awesome-llama (March 30, 2023 01:36:39)
- MineBlock137
-
Scratcher
31 posts
How do I switch to a costume who's first letter is the same as a variable?
#3 this worked perfect, thank you very much
- vladfein
-
Scratcher
100+ posts
How do I switch to a costume who's first letter is the same as a variable?
#3 this worked perfect, thank you very much
Aren't you concerned that the sprite will iterate through the multiple costume in search for the match (and that might be visible)?
At least, hide your sprite first, and show it when you are done.
But I would go with the first suggestion (using the list of costume names). Just because it's a right thing to do

- MineBlock137
-
Scratcher
31 posts
How do I switch to a costume who's first letter is the same as a variable?
#5
Well because of the “run without screen refresh” option when creating a new block, I don't think it shows. The first suggestion seems good too but @awesome-llama's solution is working nicely for me.
Well because of the “run without screen refresh” option when creating a new block, I don't think it shows. The first suggestion seems good too but @awesome-llama's solution is working nicely for me.
- awesome-llama
-
Scratcher
1000+ posts
How do I switch to a costume who's first letter is the same as a variable?
Aren't you concerned that the sprite will iterate through the multiple costume in search for the match (and that might be visible)?That would be an issue if the loop yielded (which is when the screen gets updated) but there's no point if you use run without screen refresh as the loop will complete before then.
At least, hide your sprite first, and show it when you are done.
- vladfein
-
Scratcher
100+ posts
How do I switch to a costume who's first letter is the same as a variable?
That would be an issue if the loop yielded (which is when the screen gets updated) but there's no point if you use run without screen refresh as the loop will complete before then.Oh, I see. Thank you for the explanation!
- Discussion Forums
- » Help with Scripts
-
» How do I switch to a costume who's first letter is the same as a variable?