Improving Scripts

Материал из Поле цифровой дидактики
Описание Как улучшать скрипты в своих проектах Scratch - делать их быстрее и понятнее.
Область знаний Информатика, Робототехника
Область использования (ISTE)
Возрастная категория


Поясняющее видео
Близкие рецепту понятия
Среды и средства для приготовления рецепта:

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:

whenclickedsetupgradeto0setcostto5foreverwaituntilmousedown?andtouchingmousepointer?ifupgrade=0thenchangeattackby10setcostto8changeupgradeby1switchcostumetocostume2endifupgrade=1thenandsoonuntilupgrade=10changeattackby20setcostto12changeupgradeby1switchcostumetocostume3endwaituntilnotmousedown?

...can be transformed into this:

whenclickedsetupgradeto0setcostto5foreverifmousedown?andtouchingmousepointer?thenifupgrade<11thenchangeattackbyupgrades+1*10setcosttoroundcost*1.1changeupgradeby1nextcostumeelsesaySorry,thisupgradeismaxedout.endend

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:

whenclickedshowsetHPto100whenclickedbroadcastgameStart

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:

whenclickedswitchcostumetocatwait1secsswitchcostumetodogwait1secsswitchcostumetocatwhenclickedsayIamacat!for1secswait1secssayNowIamadog!for1secswait1secssayAndnowIamacatagain!

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

whenclickedswitchcostumetocatsayIamacat!for1secswait1secsswitchcostumetodogsayNowIamadog!for2secswait1secsswitchcostumetocatsayAndnowIamacatagain!

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

whenclickedbroadcaststartgameandwaitbroadcastcalculatescoreandwaitwhenclickedforeverplaysoundbackgroundmusicuntildone

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:

whenclickedsetupgradeto0setcostto5foreverwaituntilmousedown?andtouchingmousepointer?ifupgrade=0thenchangeattackby10setcostto8changeupgradeby1switchcostumetocostume2endifupgrade=1thenandsoonuntilupgrade=10changeattackby20setcostto12changeupgradeby1switchcostumetocostume3endwaituntilnotmousedown?

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:

whenclickedsetupgradeto0setcostto5foreverwaituntilmousedown?andtouchingmousepointer?ifupgrade<11thenchangeattackbyupgrades+1*10setcosttoroundcost*1.5changeupgradeby1nextcostumeelsesaySorry,thisupgradeismaxedout.endwaituntilnotmousedown?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.