Is is possible to create a "switch" block in snap?

I was trying to create the "switch" code structure from Gamemaker Studio (For those who haven't used Gamemaker Studio, the switch structure is as follows: At the top you input a number (or other type), and then that is checked against a bunch of cases below it in an if-elseif sort of structure, and if any of the inputs match the original input, then the code nested below is ran, and then after that code is ran, it moves on to whatever proceeds the whole block.)

I saw that Snap! already has the "if {code}, else if {code}, else if ..." structure, and I am trying to turn it into a "switch (input), case (input) {code}, case ..." but I could only figure out how to essentially recreate the if-elseif block, is there a way to do this?

Attached should be a script pic containing what I created so far if that helps.

Untitled script pic(1)

(As an extension to this, if it is currently possible to do the above, would it be possible to but a boolean input after the code that would be individual to each input?)

The short answer is: yes.

You could edit the if block, save it, duplicate it, delete the original (which will now automatically be replaced by the primitive if block), and continue editing the copy.

You may want to define reference value, comparator, and case-specific values as separate inputs, or combine reference value with comparator, or comparator with case value, or even keep both values the same and vary the comparator - whatever you like. You may create a block that will run something depending on the case, or one that will report a value that is case-dependent.

What exactly are you looking for. Could you present a few examples of what you want the block to do? (i.e. not how it works internally, but its external behavior).

I have figured out how to edit the primitives and make a copy of the if-else block, but what I am intending to do is change the "if else" text in the if-else block (preferably a custom block that dosen't replace the default one) to the text: "case:" and replace the boolean input with an "any type" input (And maybe an option for "return;" as a boolean true/false after the code). I found a menu that might be for those edits, but I can't figure out how to use it to create the block I am trying to make.

Here is an image of what I am trying to make (with all of the yellow blocks being one block):

Edit: I wrote this before you edited your message, so this is in reply to what you originally said (sorry about not replying as fast as I would have hoped). I don't understand what you mean in the current middle paragraph of your updated message, but hopefully this post will help clarify?

OK, what I meant is you could specify cases by comparing values, like:

if value = 1 then run a
else if value = 2 then run b

else run z

If so, you could combine value and = into untitled script pic 299, use that as the universal comparator and specify only a case-specific second value for each case.

Or perhaps you are looking for something like:

if value < 0 then run a
else if value = 0 then run b
else if value < 1 run c
else run d

… in which case you could specify the first value once, and use case-specific comparators, such as untitled script pic 300, and untitled script pic 301.

And the other, separate remark about run vs. report is that, as an alternative for a block resembling untitled script pic 302, you might prefer to create a block like untitled script pic 303.

For the block I am trying to create, the "if value = 1 then run a, else if value = 2 then run b..." is what I am trying to create, with the custom block packaging the variable and conditional checks (which I know how to create) inside of it so that it is easy to use multiple times, which is the part where I get stuck (I don't know how to make the custom block look like the above image I sent (assuming they were combined)).

Also to reply to the extension to your post, I am intending that the code is ran, and not returned from the switch block.

Edit: The return boolean that I would want to add would allow the check to run multiple cases if it was turned off, such as having multiple "2" cases, where return would prevent any further cases from running even if they exist, and having it set to false allowing that.

Edit 2: Better pseudocode explanation:

Take an input A
For each case input:
__If the case input is equal to A:
____Run the code associated with the case input.
____If return is true, exit the for loop, otherwise continue to check further cases.

Look inside the if-block. The final input parameter is a list of pairs. Click on it. Click on the right arrow. Now click on the partial gear icon in the lower right corner. Select “special”, and find “conditionals” checked. This is how it’s done: the first item if each pair is what’s behind the “if” text, the second is the script to be run.

Edit: it’s not as flexible as I thought at first. I once used a different approach …

Copying the if block for the code part got me to (image below), but I don't know how to make it use non-booleans as input and change the text.

I see what you mean. Could you copy a script pic of your currently best developed block to this forum, or a link to your project? So I can have a look and make siggestions.

old suggestion

This is really ugly, but it works?
untitled script pic - 2024-09-17T092942.486
Definition:
untitled script pic - 2024-09-17T093029.043
"cases" is a multiple-input command input where the separator is "%n".

New suggestion: could you do
untitled script pic - 2024-09-17T094233.863

I can develop it further later, when I have time.

I wrote a Lisp case structure a while ago, not sure if it helps, though.

In the next minor release (v10.1) - which might come later this fall/winter - there will be "input groups" for custom blocks, which will let you make blocks like the expandable IF block in the palette yourself. I've just done a little experiment trying to write a naive SWITCH block in my current development version:

it's pretty straight forward, here's an example of how it would be used out in the wild:

Switch

Again, this is all very experimental at the moment, so please don't yet use it in class or for any projects that you rely on. But it's still fun to poke at. Here's the project in the current v10.1 pre-alpha-test version of Snap!.

yes!! I love this!

In fact this was precisely the problem I was facing when I was trying to create a case-switch block a few weeks ago

what is this?

oh nice, I thought it would be only in snap 11.

yes. for example, I tried making a variadic if reporter block (which i was actually able to make quite elegant-I will be sharing it with the release of snap 10.1), and unfortunately unevaluated inputs dont work in input groups. so be careful everyone!

don't worry, coming...

Oh ok nice!

Number input, I assume? I haven't checked the files developer mode recently so I don't remember much of it.

oh, didnt know we could do that :slight_smile:

I built something similar to @jens’s example (post #11), using Snap!’s current version.

The above script actually does more or less the same as jens’s (it’s actually slightly more sophisticated, but also slower). It requires two custom blocks, switch and case though:

Switch case script pic 3

The case block doesn’t really do anything, since it doesn’t even have a definition part; it’s merely used as a workaround for not having input groups yet. The (rather convoluted) switch block uses metaprogramming to “execute” the case blocks.

(posted on wrong topic)