Add scripts to a sprite

I need to make a block that adds scripts to a sprite, can delete the scripts in a sprite, and another block than can access the scripts in a sprite (and reports them in a list). can someone help? (it's not because I wanted first-class hat blocks, I came up with this first, but I felt I just HAD to suggest first-class hat blocks first.)

I think this is a very good idea to add. But it is probably a little complicated.


You can use this. Nothing to add, delete or list in the other sprite. The commands can probably be store in a list in the master sprite.

maybe it can help you...

I know about that. [offtopic] I know how to duplicate a sprite with code! [/offtopic]

This is interesting and definitely on or list of things to add, albeit in the more longish run. The interesting bit is why you think you might need it. Is this something you're missing in Snap! that you've seen in some other programming language, and - if so - which one (except Scheme / Racket)? Like, I wonder if you'd ask the same of a Java/Python/JS/C++ developer (in fact, did you perhaps?). So, please don't get me wrong, I think it's a great thing to ask for, and I'm genuinely curious what made you develop that wish. Is it something about programming with Snap! that lets you develop the wish for more meta-abstractions, and why isn't anyone ever asking such questions about and to Java.

here is one reason I will need it: I can duplicate a sprite with code, see the [scratchblocks] a [ v] of myself :: control reporter [/scratchblocks] block of this project I made, I also want to be able to delete all/some scripts of the created permanent clone or sprite.

I only use JS other than snap and scratch, and the only time I use it is in the JSfunction block, and I only use that block when I can't do the thing I want in snap. I like showing off what snap! can do!

cool, thanks!

you're welcome!
I think the solution to this problem (if you can't implement it yourself) is one block: untitled script pic(16) or even just adding a "scripts" option to untitled script pic(18) : untitled script pic(17) so you could access the scripts like that, and do things like

I am just not sure how the scripts will be ordered.

Sorry for necroposting, but I made a block to report the current sprite's scripts if you're interested.
It's in my Custom Blocks project.

thank you for the block! I have added it to Screen Shot 2021-02-08 at 4.38.07 PM
haha lol

Good work !
This Block: image dosen't work with accent letters (like é: from french canada keyboard)(if i add é to the list of letters in the function, it work...)
And a litle bug with "apostrophe" (in combinaison with "shift"): if i write an apostrophe (shift+apostrophe), the apostrophe is considered pressed even if this is not the case during subsequent detections. i i write an apostrophe again, all come back in order...

and about this keyboard :smiley::image

use JS keys pressed then!

that's a sticky key, so theres no way to fix that

also, please use my existing thread: Cool custom blocks!
@bh can you split mine and @loucheman's post please to that topic

I made a block to add a script to a sprite!
Sadly, though, you can't really directly delete them, because a) a sprite can have multiple of the same script, and b) there would be no way to label and find them without changing the way scripts work altogether. But you can do this.scripts.children = [] which deletes all of the scripts at once (included in the project).
Also, the scripts don't appear in the scripting area right away, but since the purpose of this is to work programmatically, that doesn't really matter. The script is still present in the scripting area, even if you can't see it.
Finally, you need to use @ego-lay_atman-bay's Hat blocks in grey rings for the script to function.
Without further ado, here's a link to the library.

the clear scripts is buggy

how so?

the first time you run it, nothing happens, but when you click it works.

maybe a "delete last made script" block?

No, it does work immediately, but the canvas doesn't update properly (which, stated earlier, shouldn't matter because this is supposed to be programmatically). Try it, put a when hat block and then clear scripts

okay! what about