Declare a local variable using primitive blocks

What's problem you are encountering?
I'm looking for a way to create a local variable using primitive blocks. Anyone knows how to do it?

Do you mean local to a sprite, or local to a script?

If local to a script, use the SCRIPT VARIABLES block.

If local to a sprite, there isn't a primitive, but there's a "Create variables" library that will let you do it.

I meant sprite variables, I realized that typing "sprite" as a scope using var_declare(scope, name)
you can create sprites variables
untitled script pic (2)

yes, but why would you want to do that? Really, let me repeat: If you find yourself needing to dynamically declare variables from strings your thinking is - terribly(!) - wrong. You should not ever need to use the abominable "make variables" library or the extension primitives, the script variables block and the "make a variable" button is all you need (goddammit), because you already know your variable name when you write your script!.

The only reason to ever use that library / extension is to fake a library with state.

Grrr, this is sooo discouraging. These stupid libraries are a terrible, terrible disservice to learning real programming. We're so doomed. We've set out to make programming fun and accessible and have turned it - with the enthusiastic help of wanna-be "hackers" - into a travesty of tautological meta-BS, and - worst of all - by defending abstruse practices that nobody outside of this community ever uses we're creating the impression of being a super advanced bunch of smart mathematicians while we're really just entertaining a clique of unimaginative boasters.

Here's the thing about variables:

  1. If you know the name of the variable when you're writing the program, you just declare the variable there and then. Press the "make a variable" button for global and sprite-local variables, use the "script variables" block for one or more script-local variables.

  2. If you want (your program) to remember a value by a name that doesn't get determined until the program runs you use a List, and perhaps an association. There is a library for that and you should use it. Situations in which you don't know the name of a value in advance are when your program fetches complex data such as tables, perhaps even nested ones, from an external source such as a web service or a file.

If you find yourself needing to create a variable whose name you don't know at the time that you're writing the program, you're doing it wrong. Learn about lists and data structures instead.

I'm making a block that creates a clone with an specific ID. I also want that block to be portable, so I don't get errors like "variable x" doesn't exist. That's why i'm creating script variables block dynamically

As a rule of thumb, if you find yourself needing to write a program that writes a program, with very few exceptions you're doing it wrong. If your sprite / clone needs an ID, then that's already an attribute of its prototype!

I agree with Jens; you can use a clone-local script variable under the "When I start as clone" hat block.

See what I mean in the example where a clone-local script variable is used and an inter-clone communication is based on a) relative position to each other and b) matching colors of their costumes.

FWIW, I agree with Jens that users don't need dynamic variable names, but I disagree that the end is nigh if users use them anyway.

Let's start by noting that programmers don't need variables, period. You can use association lists even in cases in which you know the names in advance, or you can use lambda binding as the sole naming mechanism. Given that, why are user-generated names on one side of the abyss and program-generated names on the other side?

The make-a-variable button isn't a satisfactory solution even for all cases in which the names are known in advance. Let's say I want 26 variables, named A to Z. Yes, I can push that damn button 26 times, but I'd really rather not.

There are practical reasons why variables (however generated) are preferable to association lists. Most notably, searching an association list takes linear time, whereas (I hope) variables are stored in a hash table, searchable in constant time. Ironically, that isn't so important when the number of variables is limited by the user's patience with pushing the make-a-variable button, but it is important when the user's program generates 10,000 names to be bound. (And yes, the user can write the code to implement a hash table, but now we're talking about a very advanced user.)

I mean, really:

"Real programming." "Doomed." "Tautological meta-BS." (I don't even know what that means.) "Clique of unimaginative boasters." C'mon, Jens.

And in particular, let me address "abstruse practices that nobody outside of this community ever uses." Creating variable names programmatically is a mainstream practice in the Logo community. That's why MAKE doesn't automatically quote its first input. If you read my Logo programs you'll find this technique used bunches of times. It was also a fairly widespread technique in early Lisp programming, although it's largely been abandoned.

And languages in which array indices can be symbols rather than just numbers are in effect giving users environments for programmatically generated local variable names. That's a widespread technique among current mainstream pro programmers, and it's just a different syntax for variables.

History, including Snap! history, shows that programmers keep wanting to use program-generated variable names.

I think that instead of rants, it would be interesting to include in the Create Variables library another set of blocks, red instead of orange, that solve the problem the way you prefer: a VARIABLE FRAME reporter that reports an empty list, a SET __ TO __ IN __ block to add or modify a binding, and a VALUE OF __ IN __ reporter, or maybe just __ IN __ to shorten it. Or maybe make it possible to leave out the IN inputs by establishing a single implicit frame per sprite, including copy-on-write for clones. And then see how users decide to solve their problems, given two alternatives.

has anybody in this community ever used a loop in Python that concatenates a bunch of strings and programmatically made those variables? In Java? in C? In BASIC?, in Pascal? in JavaScript? in Smalltalk? Does anybody even ask how that would be possible in any of those programming languages?

Does anybody even know how to look up the value of variable if all they have is a string in any of those widely used programming languages? C'mon, be quick and tell me! This must be really easy and really common, right? Everybody knows this and can do it without having to Google it, right? Show me your examples in actual code, show me where this is useful! In fact, forget all those other programming language, just show me in Java!

And if you don't believe me maybe you believe answers from actual programmers on Stackoverflow:

  • "Why do you want to do this? Surely there's a better way to achieve what you're trying to do"

  • "> 90% of the time reflection is not the solution to the over-all problem that the poster is trying to solve"

  • "While you can do what you're trying in some scripting languages such as PHP (and this question is often asked by many PHP programmers who start Java), this is not how Java works, and in fact variable names are a much less important than you may realize" (java - Creating a variable name using a String value - Stack Overflow)

  • "A note about the various "eval" solutions: you should be careful with eval, especially if the string you're evaluating comes from a potentially untrusted source" python - How to get the value of a variable given its name in a string? - Stack Overflow

  • "NOT RECOMMENDED [...] it's a horrible idea"

  • "it's the maintainance and debugging aspects that cause the horror. Imagine trying to find out where variable 'foo' changed when there's no place in your code where you actually change 'foo'. Imagine further that it's someone else's code that you have to maintain"

  • "A further pitfall that hasn't been mentioned so far is if such a dynamically-created variable has the same name as a variable used in your logic. You essentially open up your software as a hostage to the input it is given"

  • " You can use dictionaries to accomplish this. Dictionaries are stores of keys and values."

  • "It's not a good idea"

  • " If the program requires arbitrary variable "names," a dictionary is the best choice, as explained in other answers. However, if you're simply trying to create many variables and you don't mind referring to them with a sequence of integers, you're probably looking for a list ."

  • "Whenever you want to use variable variables, it's probably better to use a dictionary." python - How do I create variable variables? - Stack Overflow

Do you really believe anybody who's asking this here even knows about that "common practice in LOGO" or even ever used LOGO in that way? Asking that question reveals that the asker doesn't understand abstraction and probably also first-class-ness, the answer ("use the library") is a disservice to learners and defending the practice is misleading learners into believing they are onto an exciting and powerful meta-programming path when in fact they are only cluelessly thinking in circles.

Don't you realize that the reason this keeps coming up as a user request is because learners don't know better? If we really want to help learners we tell them when they're wrong and teach them better.

I've been quit this whole time, but I think now is a good time to say something. I personally don't see any use in the library, except for libraries that rely on a certain global or local variable. Heck, I've never even used the make variables library in a program. I've only played around with it. So I don't see any reason to keep the library (but I do think the function should stay, you know, for library makers).

Normal variables like untitled script pic (5) can't be codified, but if you use untitled script pic (6) you can.
I use this for my OS as it allows me to upload coded scripts to the cloud. Something similar to linux repositories.

untitled script pic (7)<== OS can distinguish var name

untitled script pic (8)<== The OS can't distinguish var name

Well then, there's a reason to use the var block

huh? are you talking about Snap's codification that turns blocks to text following a syntax grammar? Of course variables participate in that mechanism!! Codification and splitting blocks is not the same. I'm already regretting adding the syntax analysis feature into Snap because now the rage is all about turning text into blocks. Let me repeat: Syntax Analysis is NOT about text.
Whenever I think I've created something beautiful and new y'all race to turn it into the one old and boring crap. This is so depressing.

See, instead of using these new features to explore something exciting like Algebra everybody is only interested in turning blocks to text and back again, as if there were some deeper truth in text. I just can't stand it anymore.

how can I do that?

stop obsessing about text and start exploring Math.

I didn't wanted to make you angry, i'm just playing around with snap! and trying to translate my ideas into it, I think that's the beauty of programming...

Why don't you respect peoples' ideas, even if you don't like them?

Sorry if I'm aggressive / defensive about blocks vs. text. Blocks are my passion, but all everybody is interested in is text. There's a reason I decided not to use things like "block specs" for syntax analysis: I wanted to divert the attention from what's written on the blocks to what the blocks mean.

(I guess if I were a bazillionaire I'd be prone to become one of those frustrated Bond-type former idealist-turned-super-villains ^^)

So built-in codification was just a moment of weakness and deserves deprecation?

I made codification after Brian took me to a robot race at UC Berkeley, and we wondered how we could support programming autonomous robots that need to be programmed in C using Snap.