Discuss Scratch

ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

So, I'm making a calculator app for an OS. When a button is clicked a single sprite gets sent a message to clone itself so then the previous number will move to the left. I want to be able to delete a clone after it's been made. Is this possible? Here are my scripts:

when I receive [First Input v]
go to x: (187) y: (108)
\ switch costume to (First Input)

when I receive [Second Input v]
\ switch costume to (First Input)
create clone of [myself v]
\ switch costume to (Second Input)

when I receive [Third Input v]
wait (.3) secs
\ switch costume to (First Input)
create clone of [myself v]
\ switch costume to (Second Input)
create clone of [myself v]
\ switch costume to (Third Input)

when I receive [Third Input v]
delete this clone

when I start as a clone
change [Clone # v] by (1)
change x by ((-40) * (Clone #))

The first and second inputs work and the numbers are spread out. It's the third input that isn't.


Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

Thingied
Scratcher
1000+ posts

How to delete a clone after it's been made.

The scripts seem to be perfectly fine so I think it would be a better if you linked the project.
ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

Thingied wrote:

The scripts seem to be perfectly fine so I think it would be a better if you linked the project.
Ok, I ported it to my alt for you to try. https://scratch.mit.edu/projects/572309356/

Last edited by ComputerCole (Sept. 19, 2021 21:18:26)



Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

--Dovahkiin--
Scratcher
100+ posts

How to delete a clone after it's been made.

The one thing thats not working, is the delete clone block should be under the “when i start as clone”

when I receive [third input v]
set [3rd input v] to [true]


when I start as a clone
if <(3rd Input) = [true]> then
delete this clone
end
ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

--Dovahkiin-- wrote:

The one thing thats not working, is the delete clone block should be under the “when i start as clone”

when I receive [third input v]
set [3rd input v] to [true]


when I start as a clone
if <(3rd Input) = [true]> then
delete this clone
end
But, if I did that wouldn't that just delete the 2nd clone. I'm trying to delete the clone made in Input 2, and then recreate the clone plus add another one after.


Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

Bump


Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

sonicfan12p
Scratcher
1000+ posts

How to delete a clone after it's been made.

Your broadcast idea is sound. Every instance of a clone would receive it and immediately delete itself. The problem, or problems more like, that you're experiencing come from different parts of your code.

First, your number sprites don't actually broadcast your third input. The if statement is there, but it's empty. I filled in the blanks so I could test it, and sure enough, the clone deleting part works. However…

Second, your numbers then overlapped. This is partially because your clone # variable is “for this sprite only.” Each instance of a clone also creates its own instance of each private list and variable the original sprite had, so updating it with the clone only affects the clone's variable, not the original. Incrementing clone # before making each clone would give them each a unique id, but incrementing it after it's created will just give them all an id of 1.

Thirdly, even fixing the id's doesn't solve it because your current script would display them out of order. Clone 1 representing the first input would display in the second position, Clone 2 would be in the first position, and the original would stay in third position. Each new number added would continue this pattern, with the last number added in the right spot, and the rest of the number displayed in reverse next to it. You could fix this by having your cloning script create the clones backwards, from most to least recent input, except the last number which would still be last. But that's not the biggest problem.

Lastly, this isn't the way to do this in the first place. It's way more work for you to do it this way, and your maximum number length will always be limited by the number of variables you're willing to tolerate making. So here's a different, more versatile method for you.

when green flag clicked
broadcast [clear v]

when I receive [clear v]
set [input v] to [0]
broadcast [print v]

when this sprite clicked // For each button sprite
if <(length of (input)) < [8]> then
set [input v] to (join (input) [9]) // Replace 9 with what's on the button
broadcast [print v]
end

when I start as a clone
wait until <(clone #) = [0]>
delete this clone

when I receive [print v]
set [clone # v] to [0]
wait (0.2) secs
set [i v] to (length of (input))
go to x: (0) y: (0) //set to desired starting position
repeat ((i) - (1))
change [clone # v] by (1)
switch costume to (letter (i) of (input))
create clone of [myself v]
change [i v] by (-1)
change x by (-40)
end
switch costume to (letter (i) of (input))

This should cover the basics of what you need for a display. This also allows for a clear button, just broadcast clear when it's clicked and it will reset the display without the green flag. If any of this needs more explanation or functionality, let me know and I'll do my best to help out.

Last edited by sonicfan12p (Sept. 20, 2021 10:10:18)


Comeback time? Maybe?
ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

sonicfan12p wrote:

Your broadcast idea is sound. Every instance of a clone would receive it and immediately delete itself. The problem, or problems more like, that you're experiencing come from different parts of your code.

First, your number sprites don't actually broadcast your third input. The if statement is there, but it's empty. I filled in the blanks so I could test it, and sure enough, the clone deleting part works. However…

Second, your numbers then overlapped. This is partially because your clone # variable is “for this sprite only.” Each instance of a clone also creates its own instance of each private list and variable the original sprite had, so updating it with the clone only affects the clone's variable, not the original. Incrementing clone # before making each clone would give them each a unique id, but incrementing it after it's created will just give them all an id of 1.

Thirdly, even fixing the id's doesn't solve it because your current script would display them out of order. Clone 1 representing the first input would display in the second position, Clone 2 would be in the first position, and the original would stay in third position. Each new number added would continue this pattern, with the last number added in the right spot, and the rest of the number displayed in reverse next to it. You could fix this by having your cloning script create the clones backwards, from most to least recent input, except the last number which would still be last. But that's not the biggest problem.

Lastly, this isn't the way to do this in the first place. It's way more work for you to do it this way, and your maximum number length will always be limited by the number of variables you're willing to tolerate making. So here's a different, more versatile method for you.

when green flag clicked
broadcast [clear v]

when I receive [clear v]
set [input v] to [0]
broadcast [print v]

when this sprite clicked // For each button sprite
if <(length of (input)) < [8]> then
set [input v] to (join (input) [9]) // Replace 9 with what's on the button
broadcast [print v]
end

when I start as a clone
wait until <(clone #) = [0]>
delete this clone

when I receive [print v]
set [clone # v] to [0]
wait (0.2) secs
set [i v] to (length of (input))
go to x: (0) y: (0) //set to desired starting position
repeat ((i) - (1))
change [clone # v] by (1)
switch costume to (letter (i) of (input))
create clone of [myself v]
change [i v] by (-1)
change x by (-40)
end
switch costume to (letter (i) of (input))

This should cover the basics of what you need for a display. This also allows for a clear button, just broadcast clear when it's clicked and it will reset the display without the green flag. If any of this needs more explanation or functionality, let me know and I'll do my best to help out.
Wow. Thanks. I would never of been able to figure this out. Also, how would you make it solvable? It needs keep track of all numbers and symbols on the bar. Could you help with that?

Edit: Nevermind I noticed the input variable keeps track. However, I need help with the fraction and square root buttons. With the fraction, it will put one number on top of the other. Is that possible to do with this code, and how? With the square root, it needs to switch to the square root costume of that current number. Could you show me scripts to use for these? Thanks!

Last edited by ComputerCole (Sept. 20, 2021 13:35:31)



Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

ComputerCole
Scratcher
500+ posts

How to delete a clone after it's been made.

Bump


Banner by @Lightning_Bolt77, OC designs by me but OC created by @Lightning_Bolt77.

Powered by DjangoBB