Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Efficient Way of Choosing a List
- UltraJordan
-
6 posts
Efficient Way of Choosing a List
In a project I am developing, which is working with one sprite, I have it set so each level of the game's costumes are stored in a list. The list they're stored in is corresponding to which level it is. For example, level 1's costumes numbers are stored in a list called “objectsLevel1”, so they can be easily picked out when the game is displaying them later on. Same with level 2's costumes being stored in the “objectsLevel2” list, and all the other levels having their own list.
A problem I have come across is that whenever I want to tell Scratch what level has been chosen, I have to manually tell it which level corresponds to a list. So if the player picked level 5, the code checks "if equals 1, add 's costumes into objectsLevelPlaying". "if equals 2 add 's costumes into objectsLevelPlaying…" “if level equals 3… etc.”
This works, but it doesn't feel efficient. I have thirteen lists since there are thirteen levels. This is an example of the code I am using.

I have wondered if it's possible to construct some code that is more efficient to what I have done (Maybe using a custom block?). It would be nice if I was able to manipulate the contents of a list by selecting a list based on the value of a variable. Example: “add (variable) to (variable2)”, where variable2 is the list's name. Although that code isn't possible, there may be an alternative to it. Basically, telling Scratch what list I want without having to manually check for every single level in the game.
I don't ever use forums, so I apologise if this isn't the best place to pose this question or if my explanation is confusing. Thanks for reading.
A problem I have come across is that whenever I want to tell Scratch what level has been chosen, I have to manually tell it which level corresponds to a list. So if the player picked level 5, the code checks "if equals 1, add 's costumes into objectsLevelPlaying". "if equals 2 add 's costumes into objectsLevelPlaying…" “if level equals 3… etc.”
This works, but it doesn't feel efficient. I have thirteen lists since there are thirteen levels. This is an example of the code I am using.

I have wondered if it's possible to construct some code that is more efficient to what I have done (Maybe using a custom block?). It would be nice if I was able to manipulate the contents of a list by selecting a list based on the value of a variable. Example: “add (variable) to (variable2)”, where variable2 is the list's name. Although that code isn't possible, there may be an alternative to it. Basically, telling Scratch what list I want without having to manually check for every single level in the game.
I don't ever use forums, so I apologise if this isn't the best place to pose this question or if my explanation is confusing. Thanks for reading.
- footsocktoe
-
1000+ posts
Efficient Way of Choosing a List
There is probably more than one way of solving this problem. Here is my first idea.
Instead of having 13 lists, have one list.
Let's call the list DATA. Create it first by making a list of 1400 items filled with a null or meaningless character.
To simplify the decoding, skip over the first 100 items, then start the data at item 101.
Item 101 is the first object in list 1. Item 102 is the second object in list 1.
Item 201 is the first object in list 2. Item 202 is the second object in list 2.
Item 1201 is the first object of list 12.
Item 1301 is the first object of list 13.
You would use the “replace” block to put the objects in the list. That way all the unused slots remain filled with the null or meaningless character.
When you read the list, you have a detector for the null character to allow you to skip over those items.
So if you are on level 7 and you want object 22, you just choose item 722 from the DATA list.
That's my first idea. Maybe there is a better way.
Instead of having 13 lists, have one list.
Let's call the list DATA. Create it first by making a list of 1400 items filled with a null or meaningless character.
To simplify the decoding, skip over the first 100 items, then start the data at item 101.
Item 101 is the first object in list 1. Item 102 is the second object in list 1.
Item 201 is the first object in list 2. Item 202 is the second object in list 2.
Item 1201 is the first object of list 12.
Item 1301 is the first object of list 13.
You would use the “replace” block to put the objects in the list. That way all the unused slots remain filled with the null or meaningless character.
When you read the list, you have a detector for the null character to allow you to skip over those items.
So if you are on level 7 and you want object 22, you just choose item 722 from the DATA list.
That's my first idea. Maybe there is a better way.
Last edited by footsocktoe (Aug. 1, 2017 16:26:41)
- AwesomeSmilee
-
500+ posts
Efficient Way of Choosing a List
I have a similar idea to footsocktoe's, but instead of using I would use something like ;. The ; is a parsing character - it means it separates from . Same if you had something like ;;: the ; would separate from and from .
- Discussion Forums
- » Help with Scripts
-
» Efficient Way of Choosing a List