[Input needed] New HS feature: Local Variable

Hey Hopscotchers,

We are considering a new feature, and want your input!

  • What’s the feature?
    Local variables.

  • What is it?
    The ability to create a variable and define a local ‘scope’ for it. Meaning, it will be usable only for specific blocks of code that you define.

  • How can you use it?
    Say you wanted to make a bunch of variables, but each one was only needed for 1 function. Instead of cluttering up your variables menu with a ton of global variables, you could make 1 local variable per function - and that variable would be the only one that showed up in your ‘Local’ variables menu when you were editing that function/specific object.
    Local variables will also make it easier to share snippets of code in the future - say, to a global Hopscotch code library, should such a feature exist…but more on that later. :wink:

Here is how it would look like

Questions for you

  • Does the concept of local variables make sense to you?
  • What questions do you have about how it would work?
  • How would you use local variables in Hopscotch today?
  • What do you think local variables would allow you to make in Hopscotch that you haven’t been able to make before?
39 Likes

Yes

None

For scoring, character stuff, etc

Nothing but it’s so much better than now

7 Likes

I’d love to see this!

5 Likes

:eyes:

10 Likes

Here are my answers!

8 Likes

Looks cool! So a local variable would only be usable within a certain function?
The future of hopscotch looks exciting!

6 Likes

I think this feature is a must. It would definitely clear up clutter as you mentioned, which also means the project will be smaller in size due to less variables in the project overall.

Also, will it ask us what local ‘scope’ we want to define when we create the variable, or how would that work?

8 Likes

does it act like a self variable where each clone has a variable? or is it more like a global variables?

It would be sort of like that - so basically where you could designate the specific container(s) of code the variable applied to, as opposed to a global variable.

19 Likes

Yep, exactly :slight_smile:

14 Likes

that would be amazing; itd help so much with clutter and possibly even overall lag in a project with tons of variables.

definitely a feature id love to see implemented

9 Likes

Cool!

6 Likes

Hey, Elizabeth, long time no see.

5 Likes

I think some scoping would be great, and it would be useful as part of HS being an intro to coding in other languages (where scoping variables is essential to code organization).

Some questions:

  • How many more scopes are you thinking of? A single additional function scope (like, local to the current block)? Or (like on other languages), would each level of block nesting create the possibility of a new scope?
  • How will the scope be indicated? Right now, there are three scopes to HS variables: object, global (“game”) and user. Preceding the variable name is something that visually indicates which scope it’s in. In the case of object scope you can choose which object’s variables you’re working with. In your mock-up, I see a new two-overlapping-squares indication, but is that expressive enough multiple new scopes (in nested blocks)?
  • What is the relationship between clones and scoping? Does the meaning of this local scope ensure that all clones would see their own values for these variables? Or could one scope be shared amongst all clones, while another scope is per-clone?
  • Will you warn about shadowing? Right now, you can have variable “X” in both the global and object scope, and this works as long as you know what you’re doing (many languages offer warnings when they see that you have variables of the same same in different scopes). With more scopes, the risks of shadowing increase.
  • Can scope be changed? Right now, when you create a variable you chose its scope (by choosing what kind of variable it is), but you can’t change the scope of the variable after it’s created (you can only rename or delete it). Changing the scope of a variable, after you’ve started using it, is really common in programming in text-based languages.
  • Are any of the scopes private, and if not private, how will you refer to them outside the object? Being able to access the object variables of other objects is really important for debugging (you create a text object that, while game is playing, just displays the value of some other object’s variable). Will the new scoped variables be similarly accessible (which aids debugging, but if so how do you identify while scope the variable is in), or will they be private?

Yes, lots of words, but only proportional to my interest in this possible feature.

7 Likes

Yes, having local variables would be an amazing replacement for using object variables! When I first learned them a long time ago in a different programming language, they made sense to me pretty quickly.

This would be amazing for code organization and experimentation - I agree that it can be very cluttered when using variables sometimes. For instance, I tend to reuse object variables like i for loops, and x1/y1/x2/y2 for position-based math in certain objects/clones. It would be great if these could be kept within one function.

I totally agree that these would be great in case of library functionality, too, which I would be excited to see!

6 Likes
6 Likes

You’d be able to change the scope by moving the block around, and the scope would be based on where the variable function was in the code.

So, say you added a When block to an object. Then, you added a Repeat block inside it, then a variable function with a local variable. The local variable would be usable only in the functions inside the Repeat container.

But then you move the variable function outside the Repeat container, but within the When container. The local variable would now be usable in all code within the When container.

Let me know if that makes sense! I can also make a sketch. :slight_smile:

13 Likes

That makes more sense, so it has to do with containers, not just limited to functions

7 Likes

That makes total sense, thanks. Will definitely be worth checking out if added officially.

6 Likes
3 Likes

I do have one more question: Like user variables, will Local variables be exclusive to Advanced Mode?

3 Likes