Revision as of 22:52, 30 July 2013 by CAA14 (talk | contribs) (Redid an undone, essential part of a script. ;))

This article is about improving you scripts, or making them more compact and efficient.

Reasons for This

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:

  • 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:
when gf clicked
set [upgrade v] to (0)
forever
  if <<mouse down?> and <touching [mouse pointer v]>
    if <(upgrade) = (0)>// 10x
      set [attack v] to (10)
      change [upgrade v] by (1)
      switch costume to [costume2 v]
    end
  end

Or, you could do this:

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)
      set [attack v] to (((upgrade)+(1))*(10))
      switch costume to ((upgrade) + (1))
    else
      say [Sorry, this upgrade is maxed out.]
    end
  end

Though this is a very simple example, this is really 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

It's actually very simple.

You are looking for two things:

  1. Patterns of numbers.
  2. Scripts that can be combined.

What Scripts to Combine

You need to see if the scripts have to be apart, or if they can be 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 didn't need to be on different hat-blocks, rather they can easily be combined.

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 the script show previously:

when gf clicked
set [upgrade v] to (0)
forever
  if <<mouse down?> and <touching [mouse pointer v]>
    if <(upgrade) = (0)>// 10x
      set [attack v] to (10)
      change [upgrade v] by (1)
      switch costume to [costume2 v]
    end
  end

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 is being upgraded by ten. It is safe to assume that it will probably be upgraded at least by an increment of ten.
  • The costumes are organized in order, and therefore, the upgrade number can correspond to them.

Well, from these clues, we can make this script:

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

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.

See also

Note Note: If you are a Wiki contributer, please feel free to add relevant links here.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.