Discuss Scratch

Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

Hi! I have a basic question about cloning:
Lets imagine that I have a sprite, anything doing stuff: moving, attacking, whatever:

when green flag clicked
move (11) steps
say [blabla] for (2) secs
do more stuff [ v]

Then I create 3 clones, one that I want to be exactly like the original Sprite, so I add:
when green flag clicked
move (11) steps
say [blabla] for (2) secs
do more stuff [ v]
if <pressing on space etc> then
repeat (3)
create clone of [myself v]
end
end

Now, the only way I have for the clones to do all the stuff like the original sprite is just adding the following script:


when I start as a clone
move (11) steps
say [blabla] for (2) secs
do more stuff [ v]
if <pressing on space etc> then
repeat (3)
create clone of [myself v]
end
end

In other words, I need to put the “when I start as a clone” and then repeat all the stuff…is there any way to avoid repeating the stuff, any trick?
PutneyCat
Scratcher
500+ posts

How to avoid repeating stuff/scripts while cloning

You can avoid repeating yourself by creating a custom block (“more blocks then ”make a block“) containing the scripts you want to re-use. That way you only need to insert one line into your green flag script and your ”when I start as a clone" script.
gor-dee
Scratcher
1000+ posts

How to avoid repeating stuff/scripts while cloning

Or if you have
when green flag clicked
repeat (3)
create clone of [myself v]
end

Then if you have say
when I receive [do something v]
move (11) steps
say []

all the clones will “do something”
Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

Thanks for the tips, they were both useful! I will try the custom blocks.

gor-dee, never tought on that (in fact I am still learning some basics like broadcasting), but I dont get how can I use broadcast on clones, because (from what I understand) the clones can only do what is below the “when I start as a clone”…I mean, is it possible use any “hat”or“top”block on a clone, other than the “when I start as a clone”?

Thanks for clarify this point.
PutneyCat
Scratcher
500+ posts

How to avoid repeating stuff/scripts while cloning

Rickdangerous wrote:

Thanks for the tips, they were both useful! I will try the custom blocks.

gor-dee, never tought on that (in fact I am still learning some basics like broadcasting), but I dont get how can I use broadcast on clones, because (from what I understand) the clones can only do what is below the “when I start as a clone”…I mean, is it possible use any “hat”or“top”block on a clone, other than the “when I start as a clone”?

Thanks for clarify this point.

gor-dee is right, the clones will respond to broadcasts, the same way the main sprite responds. I think the basic position is:
- the green flag hat applies to the main sprite;
- the “when I start as a clone” hat applies to the clones;
- the “when I receive…” hat applies to both.

Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

Great! Now I get it

I dont know if it would be better to create a new topic to another question related to clones, but here goes:

how can I make the clones detect the distance to each other? Lets say that I want something to happen when 2 clones are 50 distance apart or less….since distance sensor only works between sprites, how can I make it happen between clones?

Thanks!
PutneyCat
Scratcher
500+ posts

How to avoid repeating stuff/scripts while cloning

Rickdangerous wrote:


how can I make the clones detect the distance to each other? Lets say that I want something to happen when 2 clones are 50 distance apart or less….since distance sensor only works between sprites, how can I make it happen between clones?


That's possible, but I don't think there is a really easy way to do it. You would need to calculate the distances yourself using Pythagoras's theorem. (If one clone is at x1,y1, and another at x2,y2, the distance would be the square root of (x1-x2)^2 +(y1-y2)^2
You would also need a way of accessing and comparing the positions. You could consider using lists. You could also for example get a clone to set a pair of global variables to its own x,y position, and then do a broadcast which makes all the other clones work out (using pythagoras, their own x,y position and the global variables) if their own position is within 50 of that.

Last edited by PutneyCat (May 11, 2018 12:03:16)

mstone326
Scratcher
1000+ posts

How to avoid repeating stuff/scripts while cloning

I actually just created this the other day to help with clone distances. See if this helps as you will be able to track their distances in a list and you can work it that you can work out their distances between each other.

https://scratch.mit.edu/projects/219654099/
Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

Thanks both of you for the tips.

Mstone326, do you think that it is possible to modify your project in order to achieve that? I mean to detect if, for example, a given clone is detecting anything at 50 of distance from him?

Anyway, I think I will learn more about lists, they seem to be useful.
PutneyCat
Scratcher
500+ posts

How to avoid repeating stuff/scripts while cloning

mstone326's project shows how you can associate an id with a clone, which is useful because you can use the id to store and look up information about the clone in a list - the id can correspond to the item number in the list. (Note that when creating the “id” variable you need to select “For this sprite only”, which means that each clone will have its own id.) In mstone326's project, the list stores clone-apple distances, but you can easily modify it to have two lists storing each clone's x and y position. You are then able to calculate whether a given clone is within a particular distance of any of the positions recorded in the lists. (Note that you need to exclude the position of the particular clone you are checking, because its distance to itself will be zero. Again, the id helps with that.) (Sorry, I know the question was really directed to mstone326, who may have further or different ideas on how to adapt the project.)

Last edited by PutneyCat (May 11, 2018 16:18:16)

Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

Great, thanks for clarifying!
Rickdangerous
New Scratcher
38 posts

How to avoid repeating stuff/scripts while cloning

In that project, why is the “set distance to distance” block alone?? Does it work even withtou beeing attached to other blocks?
PutneyCat
Scratcher
500+ posts

How to avoid repeating stuff/scripts while cloning

Rickdangerous wrote:

In that project, why is the “set distance to distance” block alone?? Does it work even withtou beeing attached to other blocks?
Looks like it's just been discarded as unnecessary. It won't do anything on its own like that.

Powered by DjangoBB