Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Clone Variables
- caterpillow
-
Scratcher
22 posts
Clone Variables
Hi scratchers!
I have a really REALLY nagging problem about how to make variables for each clone, so that each clone can get a set x and y position, or a health bar.
Does anyone know how to do that?
I have a really REALLY nagging problem about how to make variables for each clone, so that each clone can get a set x and y position, or a health bar.
Does anyone know how to do that?
when I start as a clone
set [local x position v] to (Global X position)
set [local y position v] to (Global Y position)
forever
go to x: ((local x position) + (Scroll X)) y: (local x position)
end
Last edited by caterpillow (Jan. 15, 2018 08:54:44)
- deck26
-
Scratcher
1000+ posts
Clone Variables
As long as you create the variables ‘for this sprite only’ each clone will get its own copy.
- exxe_
-
Scratcher
5 posts
Clone Variables
Hi scratchers!If the variable is only for that cretain sprite, then it is reset individually for each clone because it is lexically-scoped.
I have a really REALLY nagging problem about how to make variables for each clone, so that each clone can get a set x and y position, or a health bar.
Does anyone know how to do that?when I start as a clone
set [local x position v] to (Global X position)
set [local y position v] to (Global Y position)
forever
go to x: ((local x position) + (Scroll X)) y: (local x position)
end
- Mittensbrother
-
Scratcher
100+ posts
Clone Variables
This wiki post is a useful starting place for using clones with variables in a more sophisticated way: https://wiki.scratch.mit.edu/wiki/Detecting_Clones
Thanks to @exxe_ for sharing the term ‘lexical scope’ (as opposed to dynamic scope). I've hit plenty of problems because i did not really understand scope in my code, but have never had the precise term to search on to learn more. If you want to understand WHY the clone variables are not immediately accessible from any other sprite, it's worth reading up on scope in programming, and lexical scope in Scratch.
Thanks to @exxe_ for sharing the term ‘lexical scope’ (as opposed to dynamic scope). I've hit plenty of problems because i did not really understand scope in my code, but have never had the precise term to search on to learn more. If you want to understand WHY the clone variables are not immediately accessible from any other sprite, it's worth reading up on scope in programming, and lexical scope in Scratch.
- caterpillow
-
Scratcher
22 posts
Clone Variables
This wiki post is a useful starting place for using clones with variables in a more sophisticated way: https://wiki.scratch.mit.edu/wiki/Detecting_Clones
Thanks to @exxe_ for sharing the term ‘lexical scope’ (as opposed to dynamic scope). I've hit plenty of problems because i did not really understand scope in my code, but have never had the precise term to search on to learn more. If you want to understand WHY the clone variables are not immediately accessible from any other sprite, it's worth reading up on scope in programming, and lexical scope in Scratch.
when green flag clickedThis is the script I used in https://scratch.mit.edu/projects/197768892/ but all my blocks overlap.
set [Global x v] to (480)
set [Global y v] to (0)
create clone of [myself v]
set [Global x v] to (720)
set [Global y v] to (-42)
create clone of [myself v]
set [Global x v] to (762)
set [Global y v] to (-42)
create clone of [myself v]
when I start as a clone
set [Local x v] to (Global x)
set [Local y v] to (Global y)
forever
go to x: ((Scroll x) + (Local x)) y: (Local y)
end
Last edited by caterpillow (Jan. 15, 2018 22:19:14)
- Mittensbrother
-
Scratcher
100+ posts
Clone Variables
Is the issue that your ‘level’ and ‘stuff’ sprite clones are not scrolling all the way off stage? If so, you'll need to add some code to hide sprites that are beyond the stage bounds, or they will be partially rendered at the edges (Scratch prevents sprites from moving beyond the edge of the stage, which can cause problems, like this one). Try this?
forever
if <(x position) > [240]> then
hide
else
show
end
Last edited by Mittensbrother (Jan. 16, 2018 04:37:33)
- caterpillow
-
Scratcher
22 posts
Clone Variables
Is the issue that your ‘level’ and ‘stuff’ sprite clones are not scrolling all the way off stage? If so, you'll need to add some code to hide sprites that are beyond the stage bounds, or they will be partially rendered at the edges (Scratch prevents sprites from moving beyond the edge of the stage, which can cause problems, like this one). Try this?I have absolutely no idea why, but the level sprite works, but the special stuff sprite clones all overlap. The should be on its own, ABOVE the level, not outside the level clone. And if you keep walking you should find another two blocks, but they simply don't exist.forever
if <(x position) > [240]> then
hide
else
show
end
- Mittensbrother
-
Scratcher
100+ posts
Clone Variables
I suspect the ‘level’ sprite clones work because you are making them inside a Repeat loop, and the ‘special stuff’ clones are done in sequence without a Repeat loop. I'm no expert on the specifics of how every script is executed, but I think that without the repeat loop, the clones are being made too quickly to catch the changes in the variables you want to use for their XY coordinates.
- ParadoxScratching
-
Scratcher
100+ posts
Clone Variables
To be simple enough, as long as your variable is “for this sprite only” and the variables are changed or set inside the “if cloned” hat, then scratch will automaticly clone the variables.
- deck26
-
Scratcher
1000+ posts
Clone Variables
I suspect the ‘level’ sprite clones work because you are making them inside a Repeat loop, and the ‘special stuff’ clones are done in sequence without a Repeat loop. I'm no expert on the specifics of how every script is executed, but I think that without the repeat loop, the clones are being made too quickly to catch the changes in the variables you want to use for their XY coordinates.Easy enough to check - create a temporary list and get each clone to write its value(s) to the list when created/ Then you can check the list to see if the values are as you expected. I helped someone recently with something like this and creating clones too quickly after changing variables can be a problem.
- asivi
-
Scratcher
1000+ posts
Clone Variables
when green flag clickedThis is the script I used in https://scratch.mit.edu/projects/197768892/ but all my blocks overlap.
set [Global x v] to (480)
set [Global y v] to (0)
create clone of [myself v]
set [Global x v] to (720)
set [Global y v] to (-42)
create clone of [myself v]
set [Global x v] to (762)
set [Global y v] to (-42)
create clone of [myself v]
when I start as a clone
set [Local x v] to (Global x)
set [Local y v] to (Global y)
forever
go to x: ((Scroll x) + (Local x)) y: (Local y)
end
You set the Local x and y under the green flag instead.
Last edited by asivi (Jan. 16, 2018 09:38:11)
- deck26
-
Scratcher
1000+ posts
Clone Variables
As @asivi says, there's no need to set Globalx/y in your script, just set the local variables and let the clone inherit from the sprite.
- caterpillow
-
Scratcher
22 posts
Clone Variables
As @asivi says, there's no need to set Globalx/y in your script, just set the local variables and let the clone inherit from the sprite.Ahh… thanks guys!!!
- aelliott-korytek
-
Scratcher
3 posts
Clone Variables
Try creating lists to manage each of your clones, like this:
when green flag clicked
set [Clones v] to [10]
delete (All v) of [ClonesX v]
delete (All v) of [ClonesY v]
delete (All v) of [ClonesHP v]
repeat (Clones)
add [100] to [ClonesHP v]
add [250] to [ClonesX v]
add (pick random (100) to (200)) to [ClonesY v]
end
Last edited by aelliott-korytek (Jan. 16, 2018 19:41:36)
- exxe_
-
Scratcher
5 posts
Clone Variables
Hi scratchers!A way to solve it is to make one of each variable but when you make it press “for this sprite only” which makes it lexically-scoped.
I have a really REALLY nagging problem about how to make variables for each clone, so that each clone can get a set x and y position, or a health bar.
Does anyone know how to do that?when I start as a clone
set [local x position v] to (Global X position)
set [local y position v] to (Global Y position)
forever
go to x: ((local x position) + (Scroll X)) y: (local x position)
end
You can reset the variable at the start of the clone script and it will not affect other clones.
- Discussion Forums
- » Help with Scripts
-
» Clone Variables