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.
(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?)
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?
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 …
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.
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:
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!
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:
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.