Making a Custom Block with an Identifier Menu (variable name)

Hello Snap! community :grinning:

I'm trying to make a decrease <variableName> block, that decrease the given variable by -1.
My goal is to make a custom block that do it, not to use the block change <varName> by -1.

I would like to put a slot menu for choosing the variable name inside the custom block decrease var.
Is this possible ?


(the varName is Any Type in the screenshot)

Just copy-and-paste this into a text editor and save as an .xml file:

<blocks app="Snap! 6, https://snap.berkeley.edu" version="1"><block-definition s="decrease %&apos;VAR&apos;" type="command" category="variables"><header></header><code></code><translations></translations><inputs><input type="%s" readonly="true"><options>ยง_getVarNamesDict</options></input></inputs><script><block s="doRun"><block s="reifyScript"><script><block s="doSetVar"><l></l><l>-1</l></block></script><list></list></block><list><block var="VAR"/></list></block></script></block-definition></blocks>

Just do this in the custom block:

run({change[ v]by(-1)}@</span>addInput::grey ring)with inputs(varName)@</span>delInput@addInput::control 

The missing part ...

how did you get flat design menus on the default GUI design?

Flat only.

// ==UserScript==
// @name         Snap! Dark Flat
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Dark Flat Mode
// @author       Sanp! source || DarDoro
// @match        https://snap.berkeley.edu/snap/snap.html
// @icon         
// @grant        none
// ==/UserScript==

(function() {
	IDE_Morph.prototype._setDefaultDesign ||= IDE_Morph.prototype.setDefaultDesign;
	IDE_Morph.prototype.setDefaultDesign = function(){
		this._setDefaultDesign();
		MorphicPreferences.isFlat = true;
   };
})();

Thank for your answers and precision, it work now with @helicoptur's answer.

It's still weird to me that change x by -1inside the custom block definition don't work with x a variable like in @dardoro answer.

I decided to make a faq about this.

I read the faq and I agree that it don't make any sense, but I was rather thinking of something like :
untitled script pic
which does not work, I wonder why and if there is any decision behind it.

Suppose you put a SAY (NAME) at the end of your script. Then you call your DECREASE block with the input 5. So you start out with NAME=5, and then you CHANGE [NAME] BY (-1). What would you expect the SAY block to say? "4," right?

Now call your block with the input FOO. So you start out with NAME=FOO and you try to subtract 1 from that value, and it doesn't work because the value is a word rather than a number.

I think, attempting to read your mind, that you think putting the block in the Variables category (the orange blocks) gives it some special variable-nature. Or you think that because you called the input NAME, Snap! will realize that you meant it to be the name of a variable and "do the right thing." But no, it tries to do the same thing it did for 5, which doesn't work.

So it's not so much a "decision," as you label it, but rather just a working-out of the way things happen in the absence of a decision. Except for the decision not to allow putting variable ovals in those slots at all, since everyone gets so confused about it. The people who learn not to be confused can use the CALL WITH INPUTS form.

NAME is declared as :gear:>menu>variables.
For the other menu items everything works as expected.

Confusion is still there as (varName is a built-in menu)
untitled script pic (3)
untitled script pic (2)
has a scope issue.

This way, as unevaluated, works as expected because of special case handling
untitled script pic (5)
but no menu.

Okay I see,
Thank bh and dardoro for your answers and explanations

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.