Discuss Scratch

jermajac000
Scratcher
35 posts

I need help with a variable.

So, in a game I'm coding, I have a single sprite that clones itself. This is the “enemy” sprite. I want to have some way to have health for each clone, but I came to realization that if the health variable changes, it does so to all of the sprites. Is there a way to make some sort of health individual to each clone?
wolfo_the_wolf
Scratcher
3 posts

I need help with a variable.

I found out how but its kinda hard to explane but you can watch this short video. https://www.youtube.com/watch?v=rTKDzz_d6X4. tell me if the link does not work on my account
RT_Borg
Scratcher
1000+ posts

I need help with a variable.

Edit: wolfo_the_wolf shared a link to the the griffpatch video, which will show you how to make a reusable health bar.

I still suggest you still read this message and ask any questions you have, because understanding the 2 different variable types is really important and will let you build much better games in Scratch.

Hi jermajac000,

When you make a new variable, you have an option to make it “For all sprites” or “For this sprite only.”

Your enemy health variable must be For This Sprite Only if you want all the clones (and the original sprite) to each have their own value.

For All Sprites: means every sprite (and clone) shares the exact same variable. There's just a single value for health in the entire world. So if any sprite or clone makes a change to the variable, they ALL see that change. If health is 5 and any clone reduces it by 1, then health is 4 for everyone. Not what you want.

For This Sprite Only: means that only the type of sprite it was defined in has this variable. If you have a For This Sprite Only variable and make clones, a clone will also get its own separate copy of the variable. It starts with the value of the original it was cloned from had at the moment of “create clone”. So if the original has health 100 and makes a “clone of myself”, the clone will start with a health of 100. But if any clone changes its health, it doesn't affect the health of any other clone.

You can always check which type a variable is if you check the box next to it and display the value. If it's “For This Sprite Only” then when Scratch displays the variable monitor, its name will look like: enemy:health instead of just health. (You'll only be able to see the value of the original sprite's health here, not the value of a clone's health.)

It's good to have a naming pattern for your variables, so you can see at a glance if something is For All Sprites or For This Sprite Only. I personally use ALL-UPPER-CASE if my variable is For All Sprites, and all-lower-case if my variable is For This Sprite Only. But you could choose any way of naming you like. The point is, if you can see just from the name which type of variable it is, you can save yourself a lot of confusion.

Here's a demo showing clones that each have their own copy of a “For This Sprite Only variable”.

Demo: Simple Clone Variables
https://scratch.mit.edu/projects/694742494

I hope this helps. Happy to answer any questions.

– RT_Borg

Last edited by RT_Borg (Aug. 10, 2022 22:47:37)

jermajac000
Scratcher
35 posts

I need help with a variable.

Wow. That is a very helpful response. Thank you so much for putting this much time into a response for my question.
jermajac000
Scratcher
35 posts

I need help with a variable.

Looks like my scripts are working correctly. Thank you so much!
RT_Borg
Scratcher
1000+ posts

I need help with a variable.

That's great! So glad I could help. When you're an advance scratcher, come back to Help with Scripts some time and see if you can help someone too. (It feels really good.)
Duece9698
Scratcher
3 posts

I need help with a variable.

Hey, I need help with my health system, what I want it do to: when the variable reaches zero, i want something to happen. but there isnt a block for variables that does that. Can i have some help?

Powered by DjangoBB