Discuss Scratch

MarsChompsVenus
Scratcher
100+ posts

A summarization of clone & instance-based variable values

Ok. @–SupraCode– wants to know why something doesn't work in my Notification Project.


–SupraCode–

I click 2 times on the “Create a notification” block, and it works. But why does it crash when I send a broadcast twice to create a notification, with the SAME block ????? I can see you’ve used ‘_is_clone_’. But what do I do if I need to send individual notifications?

Based on his comment, he's done this:

// Sprite 1

when . . . something happens ::hat grey
broadcast [send notification v]


// Sprite 2 - with the functions

when I receive [send notification v]
NEW NOTIFICATION WITH COS [cryteksoft_developer] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom

And then it crashes.

This is because each notification is a clone. You can run this code when there's no clones - and it works great.

But every clone receives the messages.

So every time you run

broadcast [send notification v]

then all the clones get the broadcast and run the code to make a new notification. This multiplies very quickly.

To eliminate this, we need to do one of two things:

Do what I did in the project to make the notifications:

when I receive [NOTIFICATION - NEW NOTIFICATION v]
if <(_is clone) = [0]> then // Check if it's a clone or not and only run the code if it's NOT a clone
NEW NOTIFICATION WITH COS [cryteksoft_developer] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [Turbo_mode] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [App] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [succsess] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [succsess] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
end

OR we could just stop the script if it is a clone. I kind of like this one better because it looks nicer in my view:


when I receive [NOTIFICATION - NEW NOTIFICATION v]
if <(_is clone) = [1]> then // Check if it's a clone or not and only run the code if it's NOT a clone
stop [this script v]
end
NEW NOTIFICATION WITH COS [cryteksoft_developer] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [Turbo_mode] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [App] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [succsess] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
NEW NOTIFICATION WITH COS [succsess] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom


Because of how I made the project, the

 (_is clone) 

variable always represents whether the current code string is running as a clone or not.




To send one notification you can do this:



when I receive [single notification v]
if <(_is clone) = [0]> then // Check if it's a clone or not and only run the code if it's NOT a clone
NEW NOTIFICATION WITH COS [cryteksoft_developer] AT [145] [125] [75] WAIT [1] SECONDS BEFORE CLICK? <not <>>::custom
end


broadcast [single notification v] //broadcast me for one notification

You could even use variables or a list like this:

delete all of [list v] // this stack can be in a seperate sprite!
add [cryteksoft_developer] to [list v]
add [145] to [list v]
add [125] to [list v]
add [75] to [list v]
add [1] to [list v]
add <not<>> to [list v]
broadcast [single notification v]



when I receive [single notification v]
if <(_is clone) = [0]> then // Check if it's a clone or not and only run the code if it's NOT a clone
NEW NOTIFICATION WITH COS (item (1) of [list v] :: list) AT (item (2) of [list v] :: list) (item (3) of [list v] :: list) (item (4) of [list v] :: list) WAIT (item (4) of [list v] :: list) SECONDS BEFORE CLICK? (item (5) of [list v] :: list) ::custom
end



Hopefully that answers the question!

Last edited by MarsChompsVenus (Today 14:35:52)

--SupraCode--
Scratcher
16 posts

A summarization of clone & instance-based variable values

ohhhhhhhh
--SupraCode--
Scratcher
16 posts

A summarization of clone & instance-based variable values

ITS WORK !!!!! Thanks you so much Mars, you are the best

(But why on earth is Scratch reacting like that???)
MarsChompsVenus
Scratcher
100+ posts

A summarization of clone & instance-based variable values

The reason scratch does this is because each clone has all the same code as the parent sprite.

For example:

Sprite1:

when green flag clicked
create clone of [myself v]

when I receive [message 1 v]
go to [random position v]


if message 1 is broadcasted, both the sprite and its clone will go to a random position.

Clones “inherit” all the code from the parent sprite.


You can fix this by adjusting it slightly:

when green flag clicked
create clone of [myself v]
set [_is clone v] to [False] // This block only runs on the original sprite because the clone isn't around yet

when I start as a clone
set [_is clone v] to [True] // This code only runs on the clone because it's on the "when I start as a clone" block

// This only works if the variable is "for this sprite only" - if it's global, then it's the same for all sprites. But if it's set to "for this sprite only" then it can be different for each clone and sprite.


when I receive [message 1 v]
if <(_is clone) = [True] > then // Checks if it's the clone or not - remember that the variable has a different value depending on if the code is run on the sprite or the clone. The code is technically run on both but each instance has its own variable values.
go to [random position v] // Only the clone does this because we checked the variable.
end




Hopefully this clears it up a bit!

Powered by DjangoBB