Discuss Scratch
- Discussion Forums
- » Suggestions
- » Handling Sprite and Clone Variables
- fezzinate
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
Proposal
I'm proposing an elegant and simple new way of using existing scratch blocks which allows for better management/control of sprites and clones
Setting a sprite's variables with a reporter block


Returning a clone ID when creating clones


Accepting clone ID's as a block parameter

Setting a clone's variables with a reporter block


Using clone ID's on custom blocks

Why is this useful
I'm proposing an elegant and simple new way of using existing scratch blocks which allows for better management/control of sprites and clones
Setting a sprite's variables with a reporter block
Currently, scratch only allows you to ‘get’ a variable from a given sprite using the Sprite Attribute reporter - with no way of ‘setting’ a variable from outside itself. By allowing the reporter to be accepted as a parameter for the ‘Set’ block, it allows for this behavior in a simple to understand way.
Note: If the use of combining the reporter and set block is considered too confusing, a new block can be created instead that specifically sets a variable for a given sprite


Returning a clone ID when creating clones
Currently, trying to keep track of clones individually has been cumbersome and difficult. By allowing the ‘Create Clone of’ block to be cast as a reporter, this allows the block to automatically return a unique ID for that clone, which we can then use to reference that clone elsewhere in the code. You could store the clone ID's both in a standard variable, or in a list - allowing you to manage many clones systematically
Note: If casting a regular block into a reporter is considered too confusing, a new reporter can be created instead that specifically creates a clone and returns it's ID


Accepting clone ID's as a block parameter
Now that we have inherent unique clone ID's, we can allow certain blocks to accept them as a parameter wherever a sprite is expected - such as in the ‘Sprite Attribute Reporter.’

Setting a clone's variables with a reporter block
Naturally, this allows for you to set variables for a clone much like we would for a regular sprite


Using clone ID's on custom blocks
Building on this idea, and combining it with my other suggestion (read here: Global Access to Custom Blocks), we can also call custom blocks for specific clones

Why is this useful
This vastly increases the power of what's possible with scratch both from an organizational standpoint as well as an extensibility standpoint, all without increasing the complexity or difficulty of scratch. No new blocks are required (with exception to the separate custom-block feature), and the blocks behave in ways you'd normally expect them to
The goal here is encapsulation; something that scratch only, well, scratches the surface on, but has tremendous potential for. One of Scratch's main goals is to learn by sharing/tinkering. In practice, although there are many mechanisms in place to encourage it, it can be quite difficult due to how tangled scratch code can become. The way I see it, encapsulation encourages true sharing. It allows users to take pieces and use them in their own projects without getting lost in spaghetti code.
Allowing for local variables to be accessed globally (essentially making them ‘public’), allows for programs to interface with encapsulated code in a non-destructive fashion. This is especially important in the case of clones which have very little support for managing them en masse. Even more important to this cause is to allow publicly accessible custom blocks - making possible things like reusable math classes, physics classes, anything really.
Last edited by fezzinate (Aug. 28, 2014 15:13:31)
- TheHockeyist
-
Scratcher
1000+ posts
Handling Sprite and Clone Variables
Indifferent. I like this, but wouldn't this be too confusing for New Scratchers?
- fezzinate
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
Which aspect exactly do you think would be too confusing? From what I can see, all of the blocks behave in ways you'd expect them to - with maybe the exception of being able to cast a Create Clone block as a sensor. If that aspect is too confusing, I suppose creating a separate sensor block for it would solve that issue.
- TheHockeyist
-
Scratcher
1000+ posts
Handling Sprite and Clone Variables
Which aspect exactly do you think would be too confusing? From what I can see, all of the blocks behave in ways you'd expect them to - with maybe the exception of being able to cast a Create Clone block as a sensor. If that aspect is too confusing, I suppose creating a separate sensor block for it would solve that issue.
Hmmm… I'm still not sure, but semi-support then.
- fezzinate
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
Bumping for further discussion. Anyone have more thoughts on this?
- KingOfAwesome58219
-
Scratcher
1000+ posts
Handling Sprite and Clone Variables
Semi-support, even I don't quite understand it all, but some of it I've realized I needed several times.
- Zro716
-
Scratcher
1000+ posts
Handling Sprite and Clone Variables
this all seems really confusing to me, and I'm a seasoned Scratcher
I think you're just overthinking what can already be done with lists:
I think you're just overthinking what can already be done with lists:
//at the start of the project
when gf clicked
delete (all v) of [Clone X v]
delete (all v) of [Clone Y v]
delete (all v) of [Active Clone ID's v]
make clones ::custom
//when a clone spawns
when I start as a clone
set [myCloneID v] to (length of [Clone X v])
add (myCloneID) to [Active Clone ID's v]
add (x position) to [Clone X v]
add (y position) to [Clone Y v]
and so on and so forth ::grey
//example data retrieval from clone
define get distance to clone (id)
if <[Active Clone ID's v] contains (id)> then
set [x v] to ((item (id) of [Clone X v]) - (x position))
set [y v] to ((item (id) of [Clone Y v]) - (y position))
set [distance to clone v] to ([sqrt v] of (((x) * (x)) + ((y) * (y))))
end
//end of clone's life
define delete clone (id)
set [cloneCheck v] to [1]
repeat until <(item (cloneCheck) of [Active Clone ID's v]) = (id)
change [cloneCheck v] by (1)
end
delete (cloneCheck) of [Active Clone ID's v]
delete (id) of [Clone X v]
delete (id) of [Clone Y v]
delete this clone
- fezzinate
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
I think you've proven my point quite well. you've given dozens of lines of code while creating tons of redundant variables to echo the clone's variables; generally making quite a mess for something that should be as simple as one or two lines of code max. Not to mention, those are only the lines necessary for simply tracking X and Y - if you needed to track more complex things, the code needed for it grows quite a bit. For example, instead of just retrieving data from a clone, what if you want to send data to a clone - again that requires a whole new chunk of code to accomplish.
By allowing public access to variables (or custom blocks) from outside the sprite, and giving clones inherent ID's, it turns a really complex problem into a single line of code. Is it possible do do this stuff without my suggestion? Of course. But that's not really my point. My point is, aside from just being easier, it's also *cleaner*. It's these features that allow code to fully encapsulated (isolated from the rest of the program) without being dependent on extra bits of code throughout the project for it to work. Not only does this promote better coding practices, but it also promotes sharing. Encapsulated code is easier to share and learn from.
It's not like this is a foreign concept either. The concept of public variables/functions is a core aspect of object-oriented programming.
By allowing public access to variables (or custom blocks) from outside the sprite, and giving clones inherent ID's, it turns a really complex problem into a single line of code. Is it possible do do this stuff without my suggestion? Of course. But that's not really my point. My point is, aside from just being easier, it's also *cleaner*. It's these features that allow code to fully encapsulated (isolated from the rest of the program) without being dependent on extra bits of code throughout the project for it to work. Not only does this promote better coding practices, but it also promotes sharing. Encapsulated code is easier to share and learn from.
It's not like this is a foreign concept either. The concept of public variables/functions is a core aspect of object-oriented programming.
- PH-zero
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
Not bad
i'd really like to see this added!
This would also help with the multithread-problem.
(Two scripts trying to acces the same list/variable, resulting in a complete data-mess)
i'd really like to see this added!This would also help with the multithread-problem.
(Two scripts trying to acces the same list/variable, resulting in a complete data-mess)
- StudioGilliam
-
New Scratcher
4 posts
Handling Sprite and Clone Variables
I think Fezzinate has a very well thought out idea and did a great job annotating his thoughts.
This is something I've thought about myself - this is exactly what I want to see in Scratch.
This is something I've thought about myself - this is exactly what I want to see in Scratch.
- theonlygusti
-
Scratcher
1000+ posts
Handling Sprite and Clone Variables
Support just for the settig of properties of other sprites, but no support for the clone stuff: seems a but cumbersome and far fetched and ambiguous.
- fredmiracle
-
New Scratcher
8 posts
Handling Sprite and Clone Variables
I wanted to highlight the topics discussed in this thread–sorry if there is a more recent version, but this is what I came across when searching.
In my project I did exactly what post #7 says–created a hybrid data structure of lists and clones to simulate objects.
So yes, that works. But why are we teaching children to do this? Isn't this everything that was wrong with 1980s C spaghetti code? It seems a golden opportunity to do things in a more object-oriented way.
This would probably entail:
- sprite instance references of some kind
- data accessors to get the data from within sprite instances
- block(method) calls into sprite instances
In my project I did exactly what post #7 says–created a hybrid data structure of lists and clones to simulate objects.
So yes, that works. But why are we teaching children to do this? Isn't this everything that was wrong with 1980s C spaghetti code? It seems a golden opportunity to do things in a more object-oriented way.
This would probably entail:
- sprite instance references of some kind
- data accessors to get the data from within sprite instances
- block(method) calls into sprite instances
- SaturdayKnight27
-
Scratcher
3 posts
Handling Sprite and Clone Variables
I need help. I really don't know how to explain this but I am making a game were you defend your base from zombies. But the turrets i'm using are clones so i can have a lot. The problem is the zombies are also clones. I know how to make the clones update there coordinates to a list but not how to give each clone a individual list part. There are like 500 zombies at a time at some points in the game. Please help!!!!
- CKCG
-
Scratcher
100+ posts
Handling Sprite and Clone Variables
I need help. I really don't know how to explain this but I am making a game were you defend your base from zombies. But the turrets i'm using are clones so i can have a lot. The problem is the zombies are also clones. I know how to make the clones update there coordinates to a list but not how to give each clone a individual list part. There are like 500 zombies at a time at some points in the game. Please help!!!!dont necropost please
make your own topic in “help with scripts”
- sumlad
-
Scratcher
4 posts
Handling Sprite and Clone Variables
So how do we reset the clone IDs of existing clones?
- Discussion Forums
- » Suggestions
-
» Handling Sprite and Clone Variables