(Added some tips.)
(Automated edit: fixed 1 style guideline)
 
(23 intermediate revisions by 10 users not shown)
Line 1: Line 1:
This article explains methods of '''improving [[scripts]]''' for compaction and efficiency.
+
This article explains methods of '''improving [[script]]s''' for compaction and efficiency.
  
==Reasons for This==
+
==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:
 
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 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 [[remix]]es easier, by simply changing the pattern.
+
* To make them easier to handle, so you can easily change something without having to change many scripts, and so people can make [[remix]]es easier, by simply changing the pattern.
 +
 
 +
For example, using the methods stated later in this tutorial, this long, cluttered script:
 +
 
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
set [upgrade v] to (0)
+
set [upgrade v] to [0]
 +
set [cost v] to [5]
 
forever
 
forever
 
   wait until <<mouse down?> and <touching [mouse pointer v]?>>
 
   wait until <<mouse down?> and <touching [mouse pointer v]?>>
     if <(upgrade) = [0]> then  
+
     if <(upgrade) = [0]> then
       set [attack v] to (10)
+
       change [attack v] by (10)
 +
      set [cost v] to [8]
 
       change [upgrade v] by (1)
 
       change [upgrade v] by (1)
 
       switch costume to [costume2 v]
 
       switch costume to [costume2 v]
 
     end
 
     end
 
     if <(upgrade) = [1]> then //and so on until upgrade = 10
 
     if <(upgrade) = [1]> then //and so on until upgrade = 10
     set [attack v] to (20)
+
     change [attack v] by (20)
 +
    set [cost v] to [12]
 
     change [upgrade v] by (1)
 
     change [upgrade v] by (1)
 
     switch costume to [costume3 v]
 
     switch costume to [costume3 v]
Line 25: Line 31:
 
</scratchblocks>
 
</scratchblocks>
  
Or, you could do this:
+
...can be transformed into this:
 
 
<!-- OKAY, the "(((upgrades)+(1))*(10))" is meant to be, so DON'T change it please. -->
 
  
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
set [upgrade v] to (0)
+
set [upgrade v] to [0]
 +
set [cost v] to [5]
 
forever
 
forever
 
   if <<mouse down?> and <touching [mouse pointer v]?>> then
 
   if <<mouse down?> and <touching [mouse pointer v]?>> then
     if <(upgrade) < (11)> then
+
     if <(upgrade) < [11]> then
       set [attack v] to (((upgrades)+(1))*(10))
+
       change [attack v] by (((upgrades) + (1)) * (10))
 +
      set [cost v] to (round ((cost) * (1.1)))
 
       change [upgrade v] by (1)
 
       change [upgrade v] by (1)
       switch costume to (upgrade)
+
       next costume
 
     else
 
     else
 
       say [Sorry, this upgrade is maxed out.]
 
       say [Sorry, this upgrade is maxed out.]
Line 51: Line 57:
  
 
There are two main things to look for in inefficient scripts:
 
There are two main things to look for in inefficient scripts:
#Scripts that can be combined
+
# Scripts that can be combined
#Patterns with numbers that can be made into loops
+
# Patterns with numbers that can be made into loops
  
 
===Combining Scripts===
 
===Combining Scripts===
Line 71: Line 77:
 
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.
 
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 don't need to be on different [[Hat Blocks| hat blocks]] as they can easily be combined.
+
The two scripts do not need to be on different [[Hat Blocks|hat blocks]] as they can easily be combined.
  
 
Here is a more complex example:
 
Here is a more complex example:
Line 77: Line 83:
 
when gf clicked
 
when gf clicked
 
switch costume to [cat v]
 
switch costume to [cat v]
wait (2) secs
+
wait (1) secs
 
switch costume to [dog v]
 
switch costume to [dog v]
wait (2) secs
+
wait (1) secs
 
switch costume to [cat v]
 
switch costume to [cat v]
  
Line 97: Line 103:
 
wait (1) secs
 
wait (1) secs
 
switch costume to [dog v]
 
switch costume to [dog v]
say [Now I am a dog!] for (1) secs
+
say [Now I am a dog!] for (2) secs
 
wait (1) secs
 
wait (1) secs
 
switch costume to [cat v]
 
switch costume to [cat v]
Line 117: Line 123:
 
===Finding Patterns===
 
===Finding Patterns===
  
This is a bit more tricky. See the scripts for [http://scratch.mit.edu/projects/10735439/ this] project's shop buttons for a more complex example.
+
This is a bit more tricky. See the scripts for [[projects:10735439|this]] project's shop buttons for a more complex example.
  
 
For a simpler example, we will use a script shown previously:
 
For a simpler example, we will use a script shown previously:
Line 123: Line 129:
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
set [upgrade v] to (0)
+
set [upgrade v] to [0]
 +
set [cost v] to [5]
 
forever
 
forever
 
   wait until <<mouse down?> and <touching [mouse pointer v]?>>
 
   wait until <<mouse down?> and <touching [mouse pointer v]?>>
     if <(upgrade) = [0]> then  
+
     if <(upgrade) = [0]> then
       set [attack bonus v] to (10)
+
       change [attack v] by (10)
 +
      set [cost v] to [8]
 
       change [upgrade v] by (1)
 
       change [upgrade v] by (1)
 
       switch costume to [costume2 v]
 
       switch costume to [costume2 v]
 
     end
 
     end
 
     if <(upgrade) = [1]> then //and so on until upgrade = 10
 
     if <(upgrade) = [1]> then //and so on until upgrade = 10
     set [attack bonus v] to (20)
+
     change [attack v] by (20)
 +
    set [cost v] to [12]
 
     change [upgrade v] by (1)
 
     change [upgrade v] by (1)
 
     switch costume to [costume3 v]
 
     switch costume to [costume3 v]
Line 140: Line 149:
  
 
Where's the pattern? Well, we know a few things that can give us clues on what script to make:
 
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.)
+
* 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 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 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.  
+
* 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)
Well, from these clues, we can make this script:
 
  
<!-- OKAY, the "(((upgrades)+(1))*(10))" is meant to be, so DON'T change it please. -->
+
From these clues, this script can be made:
  
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
set [upgrade v] to (0)
+
set [upgrade v] to [0]
 +
set [cost v] to [5]
 
forever
 
forever
  if <<mouse down?> and <touching [mouse pointer v]>>
+
wait until <<mouse down?> and <touching [mouse pointer v]?>>
     if <(upgrade) < (11)>
+
     if <(upgrade) < [11]> then
       set [attack v] to (((upgrades)+(1))*(10))
+
       change [attack v] by (((upgrades) + (1)) * (10))
 +
      set [cost v] to (round ((cost) * (1.5)))
 
       change [upgrade v] by (1)
 
       change [upgrade v] by (1)
 
       next costume
 
       next costume
Line 160: Line 170:
 
       say [Sorry, this upgrade is maxed out.]
 
       say [Sorry, this upgrade is maxed out.]
 
     end
 
     end
  end
+
wait until <not <mouse down?>>
</scratchblocks>
 
 
 
Or
 
 
 
<scratchblocks>
 
when gf clicked
 
set [upgrade v] to (0)
 
forever
 
  if <<mouse down?> and <touching [mouse pointer v]>>
 
    if <(upgrade) < (11)>
 
      change [upgrade v] by (1)
 
      change [attack v] by (10)
 
      next costume
 
    else
 
      say [Sorry, this upgrade is maxed out.]
 
    end
 
 
   end
 
   end
 
</scratchblocks>
 
</scratchblocks>
Line 185: Line 179:
  
  
[[category:Scripting Tutorials]]
+
[[Category:Scripting Tutorials]]
 +
[[de:Komprimieren]]

Latest revision as of 08:17, 25 May 2020

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.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.