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

#1 2009-04-11 07:59:53

Shruikan42
Scratcher
Registered: 2007-10-26
Posts: 23

Variable Question

Is there any noticeable advantage to making a variable available to only one sprite? For example, will it reduce lag or game size? Thanks in advance.


http://scratch.mit.edu/users/Shruikan42 <Go on, click the pretty link, I know it's tempting!

Offline

 

#2 2009-04-11 08:16:19

bosox397
Scratcher
Registered: 2008-02-17
Posts: 1000+

Re: Variable Question

I think its just so if you make a game with lots of variables, you don't have a hard time finding the right one.


Dear Scratch Users,
I'm done with scratch, or at least making projects. I have made one last big game, thats both fun and teaches a lesson about water. It'd mean a lot if you gave me feedback.                              http://scratch.mit.edu/projects/bosox397/569201

Offline

 

#3 2009-04-11 10:23:49

Mike_W
Scratcher
Registered: 2009-02-05
Posts: 100+

Re: Variable Question

Shruikan42 wrote:

Is there any noticeable advantage to making a variable available to only one sprite? For example, will it reduce lag or game size? Thanks in advance.

I addition if a variable indicates status for that sprite alone (for example if the sprite is walking or running could be stored in a variable in the spite then you just duplicate the sprite and don't have to change anything).

The only thing I don't get if is this is a 'variable question' why does it always read the same every time I load it.

Offline

 

#4 2009-04-11 12:56:21

Shruikan42
Scratcher
Registered: 2007-10-26
Posts: 23

Re: Variable Question

Mike_W wrote:

Shruikan42 wrote:

Is there any noticeable advantage to making a variable available to only one sprite? For example, will it reduce lag or game size? Thanks in advance.

I addition if a variable indicates status for that sprite alone (for example if the sprite is walking or running could be stored in a variable in the spite then you just duplicate the sprite and don't have to change anything).

The only thing I don't get if is this is a 'variable question' why does it always read the same every time I load it.

I don't understand what you say. What does duplicating a sprite have to do with a variable storing information for that sprite alone? Do you mean that duplicating a sprite duplicates all variables which belong to it?

I have no idea what you're talking about in your last sentence. Could you explain more?


http://scratch.mit.edu/users/Shruikan42 <Go on, click the pretty link, I know it's tempting!

Offline

 

#5 2009-04-11 21:50:24

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

Re: Variable Question

bosox397 wrote:

I think its just so if you make a game with lots of variables, you don't have a hard time finding the right one.

Let's say you are going to make a lot of duplicate enemy sprites that each take 5 hits to kill.  When you are writting your scripts for the enemy sprite, you would want to make a variable that can be seen by that sprite only to keep track of the number of times it has been hit.  You might call it "Hit Count" or something.  Then, when you duplicate the sprite to make an army of enemies, each enemy sprite will have it's own version of the Hit Count variable that only they can see.  Changing the value of one will have no impact on the value of all the others.

That's one example of why you might want to use the "for this sprite only" option when making a variable.  In general, it's good programming practice to avoid the use of global variables (variables that can be changed by any sprite) as much as possible as they can make your code hard to understand - just because they can be changed anywhere.  So I use the "for this sprite only" option a lot and use the sensing block to read "for this sprite only" values from other sprites - but not change the value!

I hope that wasn't too confusing.


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

Offline

 

#6 2009-04-11 21:54:27

Mike_W
Scratcher
Registered: 2009-02-05
Posts: 100+

Re: Variable Question

Shruikan42 wrote:

[

I have no idea what you're talking about in your last sentence. Could you explain more?

The last sentence was a joke (pun). If the question is variable doesn't that mean it should change. Just like all the people that have moving sales - but they are at the same address all day,

Offline

 

#7 2009-04-12 05:42:37

Shruikan42
Scratcher
Registered: 2007-10-26
Posts: 23

Re: Variable Question

Mike_W wrote:

Shruikan42 wrote:

[

I have no idea what you're talking about in your last sentence. Could you explain more?

The last sentence was a joke (pun). If the question is variable doesn't that mean it should change. Just like all the people that have moving sales - but they are at the same address all day,

Oh, now I get it  smile  Variables change, the question doesn't.

Thanks for the info, Bosox and Paddle2See.

Another question, though, are non-global variables simply for convenience or do they actually help the project?

Last edited by Shruikan42 (2009-04-12 05:51:15)


http://scratch.mit.edu/users/Shruikan42 <Go on, click the pretty link, I know it's tempting!

Offline

 

Board footer