Storing Songs

In the future, there could be an optional exercise at the end of this page to go back and modify their mystery song project to make it tally correct responses and perhaps even deduct points for wrong answers. --MF, 2/27/22
In this activity, you will use a variable to store a list of notes.

In the Super Short Stories lab, you began using local variables to access the inputs to a block you were editing. Those variables were only available within that block.
Super Short Story, feeling: (feeling) job: (job) action: (action 1) place: (place) action: (action 2) number: (number #) color: (color) plural animal: (animals) food: (food): {report (join words ('The') (feeling) (job) (action 1) ('through') (place) ('and') (action 2) ('over') (number) (color) (animals) ('eating') (food))}
When you need a variable to be available anywhere in your project, you can use a global variable.

: Global Variable

A global variable is a variable that is usable by all scripts in the program.

For example, you can use a global variable to store the pitches of a song.
set ('Mary Had a Little Lamb') to (list (64) (62) (60) (62) (64) (64) (64) (0) (62) (62) (62) (0) (64) (67) (67))) play song (Mary Had a Little Lamb)

    The "your latest project" below has to be changed to go along with whatever decision was made on the previous page about what project they should work in. -bh 3/3/22
  1. If your latest project isn't open already, log in to Snap!, and open it.
  2. Create a global variable to store your song. Here's how...
    Making a Global Variable
    1. Click make a variable button in the Variables palette. (It's not a block; you can't drag it into the scripting area.)
    2. Type the name for your variable. (Use the name of the song or something else to help you remember it. Don't forget that the name can have spaces in it—it doesn't have to be just one word.)
    3. Click "OK."
  3. Set your global variable to store your song. Here's how...
    Setting a Global Variable
    1. Drag the set variable block out of the Variables palette and into the scripting area.
    2. In the first input slot, select the name of your global variable by clicking the triangle (▼) to open the menu.
    3. Drag the list block with your song pitches into the second input slot.
    4. Click the set variable block to run it.
    5. Notice that a watcher containing the pitches to your song appears on the Snap! stage.
      set ('Mary Had a Little Lamb') to (list (64) (62) (60) (62) (64) (64) (64) (0) (62) (62) (62) (0) (64) (67) (67)))
  4. Use your global variable as the input to your play song block, and make sure that it works.
    play song (Mary Had a Little Lamb)
  5. Create another global variable and make it store a different song.
    set ('Twinkle Twinkle Little Star') to (list (60) (60) (67) (67) (69) (69) (67) (0) (65) (65) (64) (64) (62) (62) (60)))
  6. Try out your second variable in your play song block, and check that it works too.
    play song (Twinkle Twinkle Little Star)
  7. Your global variables will appear in watchers on the Snap! stage. You can use the show variable and hide variable blocks to control whether your variables show on the stage. Try showing and hiding your song variables.
    two watchers showing on the Snap! stage: one for 'Mary Had a Little Lamb' showing a list with the elements 64, 62, 60, ... and one for 'Twinkle Twinkle Little Star' showing a list with the elements 60, 60, 67, ...
    Recall that you can also show or hide a watcher by using its checkbox in the palette on the left of the Snap! window.
  8. Now Is a Good Time to Save
  1. Now Is a Good Time to Save
    For example...
    You can use variables to store pieces of a song and then snap several play song blocks together to play the same parts of a song more than once. Try building a more complicated song that way.

In this activity, you stored lists of pitches in variables and used your play song block to play them.