This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-02-13 19:14:30

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Speeding projects up and reducing memory usage

More to come

Offline

 

#2 2008-02-14 05:20:07

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Speeding projects up and reducing memory usage

That's a pretty good "teaser" topic you have there.  Do you have a problem in this area or recommendations?


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#3 2008-02-14 16:45:21

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Sorry, I only had time to create a forum that day.

Offline

 

#4 2008-02-14 16:53:41

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Rule #1:
     Forevor blocks cause lag.
Forcing sprites/backgrounds to always do an action can potentially slow up a project, even if not much memory is used.  Forevor blocks with sensing are particularly draining. 
     Solution:
Sometimes there is not much you can do.  Just make sure you do not have multiple redundant scripts sensing for the same thing.  You can also try to put  <when[  ]key pressed> instead of having a <forever><if> script.

Offline

 

#5 2008-02-14 17:07:00

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Rule #2:
     Avoid redundancy.  Sounds, sprites and scripts that accomplish the same thing take up extra memory.
     Solution:
Try checking over your projects once in awhile to see if you have any scripts that accomplish the same thing and try to weed them out.  You can reduce the number of sprites that you use by looking for two sprites that never appear or are used at the same time.  Simply have one of them change costumes and begin new scripts to function as the other.  Sounds can be a HUGE drain on memory and speed, especially music.  The trick with sounds is to only have one of each.  Let's say you have two sprites.  Both make the "pop" sound when they are clicked.  This can be simplified with broadcasts.  Change the scripts so that when either sprite is clicked, it tells the background to make the "pop" sound.  This way, you only have to have one "pop" sound, and both sprites can still make it.  Of course, if you have any sounds that are not used at all, delete them.  Remember, all sprites have the "pop" sound on default.  If you do not need it, make sure to delete it.  It does not take up much memory, but every little bit counts.

Offline

 

#6 2008-02-14 17:12:00

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Rule #3:
     <repeat until> and <wait until> blocks cause lag.
Just like the Forevor block, they force your sprite/background to always check for something.  There is really not much that you can do to replace them, though.

Offline

 

#7 2008-02-14 17:25:15

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Rule #4:
     Variables slow projects up like crazy. 
Solution:
     There are many different ways to reduce variable memory use.  First, keep variables from being shown.  Scratch hates having to constantly change the visual display to follow the variable.  If you absolutely must have a variable be shown, have a sprite do it.  Give a sprite a script to constantly say what the variable is.  If you want to save even more memory, make a sprite with ten costumes- each costume showing the numbers 0-9, in that order.  Tell the sprite to switch to costume (Variable +1) and it should display the variable.  This can easliy be changed to accomodate large numbers. 
     Did you know that Scratch has default variables that are always used?  Tempo can be used in place of a variable if you are not using any note blocks.  Costume number, X position, Y position and direction can also be used, especally if that sprite is not always shown.  To change the "variable", have a broadcast telling that sprite to move/change direction/change costume/etc.

Offline

 

#8 2008-02-14 17:33:32

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     Here's an example of costumes being used as a variable.  "Bob" has a ship sprite.  In his program, the ship has 3 hit points, and when it runs out it disapears.  "Bob" wants the player to know how much health the ship has.  Rather than making a variable, "Bob" gives the ship three different costumes: One with a pictue of the ship and a full health bar, one with 2/3 health bar, and one with 1/3.  When the ship is hit, it simply changes costumes.

Offline

 

#9 2008-02-14 17:37:22

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     Here's an example of a sprite being used as a variable.  "Bob" has a ship sprite.  In his program, the ship has 300 hit points, and when it runs out it disapears.  "Bob" wants the player to know how much health the ship has.  Rather than making a variable, "Bob" makes an extra sprite that looks like a health bar.  Whenever the ship is hit, it tells the health bar sprite through a broadcast to move out of the screen, making it look as if the health were decreasing.  If the health bar is past a certain point, it tells the ship to hide.  In this situation, the X or Y coordinate of the health bar sprite is the variable.

Last edited by oooo (2008-02-14 18:10:45)

Offline

 

#10 2008-02-14 17:39:24

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     Variable Compression
This is one of the best things that I have figured out how to do on Scratch.  Combining two or more variables into one drastically improves playing speed.

Offline

 

#11 2008-02-14 17:50:14

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     Here's an example of compressed variables.  "Bob" has a ship sprite (A).  In his program, the ship has 3 hit points, and when it runs out it disapears.  "Bob" also has another ship (B) that has one hit point.  Rather than have two variables, "Bob" only has one.  Normally, A's variable will always be positive.  B's variable would only ever need to be one of two numbers, one for healthy and one for destroyed.  Instead, "Bob" has the compressed variable encompass both.  It consists of the numbers -4, 3, -2, -1, 1, 2, 3, 4.  The absolute value of this variable minus one is ship A's health.  Whether or not the variable is negative or positive indicates whether ship B has health or not.  When ship B is hit, the variable is set to (variable times -1).  When ship A is hit, it decreases the variable by one if it is positive, or increases by one if it is negative.

Offline

 

#12 2008-02-14 17:56:56

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     But that only combines two variables.  I found a way that allows 20+ variables to be combined, but it has a catch: All variables have to be posititive or all have to be negative, and each variable cannot exceed a certain number of places.  While it is hard to explain here, it involves assigning each place in a VERY long variable to stand for another variable.  For example, a three digit number could have the ones place indicate health, the tens place indicate speed, and the hundreds place indicate ammo type.

Offline

 

#13 2008-02-14 17:59:07

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Variables Solution Cont.
     It involves using the ((  <mod>  )) block to evaluate each place.  It uses a signifigant bit of memory to have a compressed variable, but a lot less than if the variables were seperate.

Offline

 

#14 2008-02-14 18:03:56

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

One day I tried to make a pokemon demo.  I began by making the variables.  One for level, one for each stat, one for health, one for type, one for species, one for experience, etc.  And all of this was for only one pokemon, besides any other gameplay variables that would have to be added.  I made so many variables that I crashed my Scratch window.  Ouch.  Later I learned that Scratch could indeed have that many variables, just it could not show them all at once.  When I made the deom again, I used compressed variables to combine most of a single pokemon's stats into just two variables when it was not in the party.  That's PC storage for ya.

Offline

 

#15 2008-02-14 18:07:48

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

What if You can't avoid slow Scratch projects?
Solution:
     For projects that are sensitive to speed, try increasing the speed at which your sprites move.  Rather than have a character move 5 steps when a button is pushed, have it move ten. 
Or you could just avoid large speed-sensitive projects all togethor.  Try making a turn based RPG where speed does not matter. 
If all else fails, you can always compress sounds or images.

Offline

 

#16 2008-02-15 02:00:18

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Speeding projects up and reducing memory usage

Hmm - thanks for this.  I have used the place values of variables before but for a different purpose.  Hadn't occured to me to use a 10 digit number to store 10 different variables - it might be just what I need to record the "Alive/Dead" state of enemies in my dynamic scrolling engine.

Heh - almost like using the variables as Binary numbers instead of decimals.

What calculation do you use to strip out the required digit?

Last edited by Mayhem (2008-02-15 02:06:35)


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#17 2008-02-15 03:48:01

MyRedNeptune
Community Moderator
Registered: 2007-05-07
Posts: 1000+

Re: Speeding projects up and reducing memory usage

Wow, that's quite a lot information you have here! Great job, this was well worth reading. I hope this will help a lot of people.


http://i52.tinypic.com/5es7t0.png I know what you're thinking! "Neptune! Get rid of those filthy advertisements and give us back the Zarathustra siggy, you horrible person!" Well, don't worry about it, the Zara siggy will be back soon, new and improved! ^^ Meanwhile, just do what the sig tells you to. >.>

Offline

 

#18 2008-02-15 04:39:19

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Speeding projects up and reducing memory usage

I have come up with my own script for reading the value of any given digit of a 10 digit number, but I am sure it could be simplified/improved.

Its got lots of nested blocks so it looks very complicated, and it only works if the digits are between 0 and 4, inclusive (as it uses rounding, and decimals bigger from 5 upwards round up instead of down)

Any suggestions for improvement gratefully recieved.

http://scratch.mit.edu/projects/Mayhem/99866


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#19 2008-02-15 10:59:09

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Speeding projects up and reducing memory usage

To get out the digit in the 1000s place of a non-negative number use
       (x/1000) mod 10

To zero out the digit in the 1000s place

     change x by - 1000 * ( (x/1000) mod 10)

To replace the digit in the 1000s place, first zero it out, then add 1000*digit

Offline

 

#20 2008-02-15 11:57:17

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Speeding projects up and reducing memory usage

I haven't noticed that having a lot of variables causes issues with speed, although displaying a lot of them definitely will bog scratch down.  The variable displays are treated just like sprites, so the real sprites will test for "touching" and can "collide" with them if your program looks for something like "touching black".

Personally, I see this as a bug.  It would be nice if the variable displays were shown, but "ignored" by the other sprites.

-MrEd

Offline

 

#21 2008-02-15 12:07:29

TegansPoppy
Scratcher
Registered: 2008-01-06
Posts: 47

Re: Speeding projects up and reducing memory usage

I have noticed that adding a micro-delay (such as wait(0.01 seconds)) can improve performance somehow, at least on my system. I have no idea if this is a quirk on my setup, or if it is in Scratch itself. On my machine, my 3D Logo Test project ran slow and choppy until I added in that delay. With the delay, it runs faster and smoother. Paradoxical? Reproducible? Inconceivable? ;P

David:.


http://guessmedia.com/scratch/

Offline

 

#22 2008-02-15 13:46:31

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Speeding projects up and reducing memory usage

kevin_karplus wrote:

To get out the digit in the 1000s place of a non-negative number use
       (x/1000) mod 10

To zero out the digit in the 1000s place

     change x by - 1000 * ( (x/1000) mod 10)

To replace the digit in the 1000s place, first zero it out, then add 1000*digit

Hmm.  Trying that.

I'm using the 10^(z-1) instead of "1000" so that the formula returns the digit from place Z - ie if z = 1, it returns the first digit (the units), if Z=2 it returns the Tens, etc.

Curiously, I keep getting answers with a decimal place.

My tracking number (x) I am testing with is 0987654321

( x / ( 10^ ( z-1) ) ) mod 10 returns the following values:

z=1 returns 1
Z=2 returns 2.1
Z-3 returns 3.2
4 - 4.3
5 - 5.4
6 - 6.5
7 - 7.7
8 - 8.8
9 - 9.9
10 - 1

This should not be possible, surely.

Last edited by Mayhem (2008-02-15 13:47:53)


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#23 2008-02-15 14:46:14

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Speeding projects up and reducing memory usage

"Mod" returns the remainder after dividing. 

"987654321/100" is: 9876543.21,  10 will go (evenly) into that 9876540 times, with "3.21" as the remainder.  You'll need to truncate, or perform some more math tricks, to extract the "number of interest".

-MrEd

Last edited by EdnaC (2008-02-15 14:50:46)

Offline

 

#24 2008-02-15 14:56:58

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Speeding projects up and reducing memory usage

Ah, of course.

For some bizarre reason I had gotten it into my head that the result of a mod operation had to be an integer.  Doh!


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#25 2008-02-18 20:05:30

oooo
Scratcher
Registered: 2007-08-09
Posts: 20

Re: Speeding projects up and reducing memory usage

Mayhem wrote:

I have come up with my own script for reading the value of any given digit of a 10 digit number, but I am sure it could be simplified/improved.

Its got lots of nested blocks so it looks very complicated, and it only works if the digits are between 0 and 4, inclusive (as it uses rounding, and decimals bigger from 5 upwards round up instead of down)

Any suggestions for improvement gratefully recieved.

http://scratch.mit.edu/projects/Mayhem/99866

The trick is to subtract mod to clear out the decimal.  This way you can have 1-9.

Offline

 

Board footer