New idea for switch statements

I have came up with (I think) new way of making switch statements. This uses the "Create Variables" library to create a temporary global variables called "__cases__", where case blocks put their... cases. Its complicated to explain, so I wont.

UPDATE: Multiple values for the case blocks!

For anyone wanting to test it out, here's the link.

(user [CodingIsFun2831t] :: operators) hopes you [enjoy v] :: control

If you prefer a XML to import, here's the link to it. (I didn't see any rules against file links.)

There are no rules against it. However, if you create a script pic (by right clicking the script), you can then drag and drop that image into snap, and snap will import the script in the image.

Okay, got it. Will use that in my future posts.

Clever. I remember considering doing something like this for the if block before it became variadic. I like how simple and elegant your program is. In fact, I like it so much, I might actually use it, and thus I have a few suggestions :~).

One thing you could consider is, instead of having a global variable that is created, create a script variable. I know what you're probably thinking: the script variable will only be accessible to the script that created it, in this case the switch block's definition. This is true, but you can get around it by using TELL. TELL can make a script be run in another sprite. For example, this: tell [sprite (2) V] to ({move (10) steps}@>) @:> will make sprite (2) move instead of the sprite running the tell block. This also works for the creation of script variables in other scripts (the technical term here being constants). You can use TELL alongside its reporter counterpart ASK like so:


Note that, if you were to use the regular SCRIPT VARIABLES block to create the variable, it wouldn't work - I'm not sure why.

Next, I noticed that you used report to terminate your SWITCH...DEFAULT block:


This does work, and there is nothing wrong with it per se, but Snap! provides you with a tool to do just that:
Switch Showcase script pic (2)
Note that STOP [THIS SCRIPT] will also stop the script that called the custom block, not just the block itself.

Finally, there is one more thing (yes, I know I'm going on quite long). C-shapes can take reporters as inputs because those reporters might report a command to be run. This is helpful for recursion, but generally it just means that it is super easy to kick out an entire script with just one reporter.


However, there is a way to disable this. Go into the input menu, click the gear and select "static":

Now, no reporters will be able to be put in the c-slot. That's all!

Thank you for your suggestions! I'll be sure to implement them now.

EDIT: Updated! I also added a value "upvar" to the default. Now, I did have to do it a hacky way. I used the script variable thing you taught me, and used that in the default code to set the value variable. It will still reference the variable if you grab the variable from the upvar, because they have the same name.

Actually, upvars are designed to exist in both the script that called them and the block definition at the same time (it is kind of cool). So, you can just do this: set [value V] to (_value) and it will work! Do let me know if I'm missing something about why you did this. Yeah, nice work!

Oh. Well, updated.