m (→‎Reasons for This: Fixed a comment on a script.)
(Automated edit: fixed 1 style guideline)
 
(45 intermediate revisions by 11 users not shown)
Line 1: Line 1:
This article is about improving you [[scripts]], or making them more compact and efficient.
+
This article explains methods of '''improving [[script]]s''' for compaction and efficiency.
  
=Reasons for This=
+
==Reasons to Keep Scripts Compact and Efficient==
  
Well, there are several. Besides from improving your scripts being a professional practice, here's some reasons why compacting and further work on your scripts is a good idea:
+
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 [[remix]]es easier, by simply changing the pattern.
 +
 
 +
For example, using the methods stated later in this tutorial, this long, cluttered script:
  
*Lag. Lag can be caused in scratch because of disorganized and to cluttered scripts. Improving them will help.
 
*Easier to handle. For instance, if you want to make a script for a button, in which can be clicked ten times, and it is determined by it's "upgrade" variable, then you could right if statements... Over and over again:
 
 
<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) = (0)>// 10x
+
     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
  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?>>
 
</scratchblocks>
 
</scratchblocks>
  
Or, you could do this:
+
...can be transformed into this:
  
 
<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]>>
+
   if <<mouse down?> and <touching [mouse pointer v]?>> then
     if <(upgrade) < (11)>
+
     if <(upgrade) < [11]> then
 +
      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)
       set [attack v] to ((upgrade)*(10))
+
       next costume
      switch costume to ((upgrade) + (1))
 
 
     else
 
     else
 
       say [Sorry, this upgrade is maxed out.]
 
       say [Sorry, this upgrade is maxed out.]
 
     end
 
     end
 
   end
 
   end
 
 
</scratchblocks>
 
</scratchblocks>
  
Though this is a very simple example, this is really all you need, whereas the previous script would have '''10''' if statements in it.
+
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, much easier, as remember, there would be '''9''' other if statements to the first one.
 
  
=How to do it=
+
As you can see, handling the second one would be much easier.
  
It's actually very simple.
+
==How to do it==
  
You are looking for two things:
+
There are two main things to look for in inefficient scripts:
#Patterns of numbers.
+
# Scripts that can be combined
#Scripts that can be combined.
+
# Patterns with numbers that can be made into loops
  
==What Scripts to Combine==
+
===Combining Scripts===
  
You need to see if the scripts have to be apart, or if they can be together.
+
You should see if scripts can be ran together.
  
 
Here's a good example of some scripts that can be combined:
 
Here's a good example of some scripts that can be combined:
Line 59: Line 70:
 
show
 
show
 
set [HP v] to (100)
 
set [HP v] to (100)
 +
 +
when gf clicked
 +
broadcast [gameStart v]
 
</scratchblocks>
 
</scratchblocks>
  
 +
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|hat blocks]] as they can easily be combined.
 +
 +
Here is a more complex example:
 +
<scratchblocks>
 +
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!]
 +
</scratchblocks>
 +
 +
The pauses within the scripts is constant and lines up. Therefore, the following combination is possible:
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
broadcast [gameStart v]
+
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!]
 
</scratchblocks>
 
</scratchblocks>
  
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.
+
However, some scripts with the same hat block cannot be combined, like these:
The two scripts didn't need to be on different hat-blocks, rather they can easily be combined.
+
<scratchblocks>
 +
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
 +
</scratchblocks>
 +
The pauses that the blocks make do not line up, and one is in a loop while the other is not.
  
==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 the script show previously:
+
For a simpler example, we will use a script shown previously:
  
 
<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) = (0)>
+
    if <(upgrade) = [0]> then
set [attack v] to (10)
+
      change [attack v] by (10)
change [upgrade v] by (1)
+
      set [cost v] to [8]
switch costume to [costume2 v]
+
      change [upgrade v] by (1)
end
+
      switch costume to [costume2 v]
//x 10
+
    end
end
+
    if <(upgrade) = [1]> then //and so on until upgrade = 10
end
+
    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?>>
 
</scratchblocks>
 
</scratchblocks>
  
 
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 is being upgraded by ten. It is safe to assume that it will probably be upgraded at least by an increment of ten.
+
* 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:
+
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
change [attack v] by (((upgrade)+(1))*(10))
+
      change [attack v] by (((upgrades) + (1)) * (10))
change [upgrade v] by (1)
+
      set [cost v] to (round ((cost) * (1.5)))
switch costume to ((upgrade) + (1))
+
      change [upgrade v] by (1)
else
+
      next costume
say [Sorry, this upgrade is maxed out.]
+
    else
end
+
      say [Sorry, this upgrade is maxed out.]
end
+
    end
 +
wait until <not <mouse down?>>
 +
  end
 +
</scratchblocks>
  
</scratchblocks>
+
==Tip==
  
The reason for all the additions and subtractions from upgrade, is because when upgrade first starts out, it starts at 0. There is no such thing as costume 0, and therefor 1 must be added. As for the addition to upgrades, it is for a similar reason. again, as it starts out at 0, then the first upgrade will cost 0, as 10 x 0 = 0. To correct this, we add one, which will solve the problem. Making upgrade start out at one would fix these problems, but would cause a slight off when counting to the tenth upgrade, as you could now only upgrade it 9 times.
+
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.
  
=See also=
 
  
{{note|If you are a Wiki contributer, please fell free to add relevant links here.}}
+
[[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.