This article explains methods of improving scripts for compaction and efficiency.

Reasons to Keep Scripts Compact and Efficient

Besides improving your scripts being a professional practice, some reasons why one should compact and further work on scripts include:

  • To reduce project slowness due to cluttered or inefficient scripts
  • To make them easier to handle, so you can easily change something without having to change many scripts, and so people can make remixes easier, by simply changing the pattern.

For example, using the methods stated later in this tutorial, this long, cluttered script:

when gf clicked
set [upgrade v] to [0]
set [cost v] to [5]
forever
  wait until <<mouse down?> and <touching [mouse pointer v]?>>
    if <(upgrade) = [0]> then
      change [attack v] by (10)
      set [cost v] to [8]
      change [upgrade v] by (1)
      switch costume to [costume2 v]
    end
    if <(upgrade) = [1]> then //and so on until upgrade = 10
     change [attack v] by (20)
     set [cost v] to [12]
     change [upgrade v] by (1)
     switch costume to [costume3 v]
   end
wait until <not <mouse down?>>

...can be transformed into this:

when gf clicked
set [upgrade v] to [0]
set [cost v] to [5]
forever
  if <<mouse down?> and <touching [mouse pointer v]?>> then
    if <(upgrade) < [11]> then
      change [attack v] by (((upgrades) + (1)) * (10))
      set [cost v] to (round ((cost) * (1.1)))
      change [upgrade v] by (1)
      next costume
    else
      say [Sorry, this upgrade is maxed out.]
    end
  end

Though this is a very simple example, this is often all you need, whereas the previous script would have 10 if statements in it.

As you can see, handling the second one would be much easier.

How to do it

There are two main things to look for in inefficient scripts:

  1. Scripts that can be combined
  2. Patterns with numbers that can be made into loops

Combining Scripts

You should see if scripts can be ran together.

Here's a good example of some scripts that can be combined:

when gf clicked
show
set [HP v] to (100)

when gf clicked
broadcast [gameStart v]

As you probably can see, they can easily be combined. There are much more complex instances of such, but this should make the point clear.

The two scripts do not need to be on different hat blocks as they can easily be combined.

Here is a more complex example:

when gf clicked
switch costume to [cat v]
wait (1) secs
switch costume to [dog v]
wait (1) secs
switch costume to [cat v]

when gf clicked
say [I am a cat!] for (1) secs
wait (1) secs
say [Now I am a dog!] for (1) secs
wait (1) secs
say [And now I am a cat again!]

The pauses within the scripts is constant and lines up. Therefore, the following combination is possible:

when gf clicked
switch costume to [cat v]
say [I am a cat!] for (1) secs
wait (1) secs
switch costume to [dog v]
say [Now I am a dog!] for (2) secs
wait (1) secs
switch costume to [cat v]
say [And now I am a cat again!]

However, some scripts with the same hat block cannot be combined, like these:

when gf clicked
broadcast [start game v] and wait
broadcast [calculate score v] and wait

when gf clicked
forever
  play sound [background music v] until done

The pauses that the blocks make do not line up, and one is in a loop while the other is not.

Finding Patterns

This is a bit more tricky. See the scripts for this project's shop buttons for a more complex example.

For a simpler example, we will use a script shown previously:

when gf clicked
set [upgrade v] to [0]
set [cost v] to [5]
forever
  wait until <<mouse down?> and <touching [mouse pointer v]?>>
    if <(upgrade) = [0]> then
      change [attack v] by (10)
      set [cost v] to [8]
      change [upgrade v] by (1)
      switch costume to [costume2 v]
    end
    if <(upgrade) = [1]> then //and so on until upgrade = 10
     change [attack v] by (20)
     set [cost v] to [12]
     change [upgrade v] by (1)
     switch costume to [costume3 v]
   end
wait until <not <mouse down?>>

Where's the pattern? Well, we know a few things that can give us clues on what script to make:

  • There are ten upgrades, and therefore, ten costumes (assuming each upgrade changes the button's appearance).
  • The attack so far is being upgraded by ten more each time. Therefore, you can assume that the attack will be upgraded in the same way up until upgrade equals 10, unless you prefer different results.
  • The costumes are organized in order, and therefore, the upgrade number can correspond to them. Even better, the Next Costume block can be used.
  • The cost is changed by the rounded number of the previous number times 1.5. (1.5×5≈8; 1.5×8≈12)

From these clues, this script can be made:

when gf clicked
set [upgrade v] to [0]
set [cost v] to [5]
forever
 wait until <<mouse down?> and <touching [mouse pointer v]?>>
    if <(upgrade) < [11]> then
      change [attack v] by (((upgrades) + (1)) * (10))
      set [cost v] to (round ((cost) * (1.5)))
      change [upgrade v] by (1)
      next costume
    else
      say [Sorry, this upgrade is maxed out.]
    end
wait until <not <mouse down?>>
  end

Tip

Improving your scripts is something that should be done, but it is much easier to improve and find patterns in scripts after you have made them. Make your scripts so that they do what you want them to do, then improve them, unless you already have efficient scripts.