# Reasons why Local Vars should be added

**URL:** <https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488>\
**Category:** Questions about Thunkable X\
**Created:** [July 14, 2021, 6:29pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488 "2021-07-14T18:29:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 14, 2021, 6:29pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/1 "2021-07-14T18:29:55Z")

</div>

So, Local Vars. Not really used a ton while making anything other than a game.

Then again, if you wish to create a game, local vars can be really helpful.

“How can local vars be helpful in any way?”  
It’s really simple. When a sprite is created it can be created with a number to identify itself from the other clones. For example, if you want an enemy to have its own X and Y saved as a var, then local vars can come in handy if you have multiple enemies from the same sprite type.  
Not having local vars makes evolution games _impossible._ It can make many things much more difficult as well.

If Thunkable really wants to become something that makes all apps possible, then you need all the tools.  
You can’t build a table with a hammer and some planks. You need nails too. And a saw.

Thunkable can be great, but it has less tools than Scratch or other block code sites.

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 14, 2021, 6:45pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/2 "2021-07-14T18:45:28Z")

</div>

Is this local versus global variables? Or something else? Sprites /are/ created with a component ID that identifies them. You can store those and use them to keep track of and modify individual sprites. What specifically can’t you do that you’d need for an evolution game? (I like a challenge!)

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 3:06pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/3 "2021-07-16T15:06:10Z")

</div>

The ID isn’t enough in some cases. For example if you want to make a game where it’s in any way scrolling, every sprite would need it’s own X and Y vars. Evolution games require clones of sprites, each with it’s own var (for example, speed, size, color.)

Local vars would just be a great quality of life thing.

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [July 16, 2021, 3:23pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/4 "2021-07-16T15:23:44Z")

</div>

> [@jonathonschnoor6x4b5](#):
>
> every sprite would need it’s own X and Y vars

Which is already available. What do you exactly need?

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 3:33pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/5 "2021-07-16T15:33:18Z")

</div>

> [@jonathonschnoor6x4b5](#):
>
> The ID isn’t enough in some cases. For example if you want to make a game where it’s in any way scrolling, every sprite would need it’s own X and Y vars. Evolution games require clones of sprites, each with it’s own var (for example, speed, size, color.)

Currently implemented in Thunkable. Each plant has a location (you can drag them around), day it’s going to get picked up, how much you’ll get paid for taking care of it, etc.

> **[thunkablecompanion](https://thunkable.site/web-build/index.html?webAppId=oiokDDGzH&from=x)**
>
> Powered by Thunkable

I put each plant’s sprite ID into a table, along with other parameters that I wanted to keep track of. Except X and Y - I just get those from the sprite whenever I want them, since the sprites can be moved by the user at any time.

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 3:57pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/6 "2021-07-16T15:57:26Z")

</div>

catsarisky this is a great idea, but I feel like it’s a little much when you can have just one block of code.

Also, my tables disconnect from the app very often. ☹

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 4:03pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/7 "2021-07-16T16:03:44Z")

</div>

![plane mayhem](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/e/3/e340050a68b1d2c7f90106347b8e6ed332b824f0.png)  
If the brown plane stays in the center of the screen, and all the grey planes are moving objects. Each grey plane would need it’s own X and Y _vars_ in order for this to work.

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 4:10pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/8 "2021-07-16T16:10:54Z")

</div>

> [@jonathonschnoor6x4b5](#):
>
> If the brown plane stays in the center of the screen, and all the grey planes are moving objects. Each grey plane would need it’s own X and Y _vars_ in order for this to work.

Yep, and it does - use this and plug in each sprite’s component ID (which you can store in a list if you prefer)  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/4/84a93ec6126fb3e81f86af561ac9f5c4bb745d23.png)

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 4:14pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/9 "2021-07-16T16:14:39Z")

</div>

The issue is it needs to be:

Set [object’s] X to [(Scroll X) - (Object X)] Y [(Scroll Y) - (Object Y)]

This wouldn’t work because it’s X would be the result of the Scroll X and it’s X.

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 4:16pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/10 "2021-07-16T16:16:54Z")

</div>

I think I’m not following you here?

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 4:21pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/11 "2021-07-16T16:21:38Z")

</div>

Sorry, this is really difficult to explain 😂

It’s a scrolling game, ok? That means the player will remain in the center of the screen at all times. The player is not moving, everything else is.

So every thing would need it’s own Location variables that can be subtracted by two other variables, which are scrolling vars which change where everything is.

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 4:23pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/12 "2021-07-16T16:23:16Z")

</div>

Gotcha. I understand scrolling games. 🙂  
So let me see if I can mock that up for a second, because what I’m not understanding is why it isn’t doable, and maybe if I try to do it, then I’ll understand! Or maybe I’ll teach you something. We’ll see!

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 4:26pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/13 "2021-07-16T16:26:06Z")

</div>

Also: Keep in mind that the player is moving at angles, not up/down/left/right, and the enemies are also moving.  
 ![Screen Shot 2021-07-16 at 11.25.32 AM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/c/5c5182fd89a15bcbd02b3abce58b868479ed517e.png)  
This is the code I’m using.

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 4:45pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/14 "2021-07-16T16:45:46Z")

</div>

So this:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/e/1e8426c1cfce1a5bf9d403c007258ca131b04094.png)

Here’s the code : [Thunkable](https://x.thunkable.com/projectPage/60f1b11476dcec0011c83a33)

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 16, 2021, 9:04pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/15 "2021-07-16T21:04:39Z")

</div>

I see what you mean now. I still think local vars could be useful for Thunkable.

Thanks again!

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [July 16, 2021, 9:15pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/16 "2021-07-16T21:15:41Z")

</div>

And now I want to build a scrolling game! 🙂

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [July 16, 2021, 10:28pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/17 "2021-07-16T22:28:26Z")

</div>

> [@jonathonschnoor6x4b5](#):
>
> still think local vars could be useful for Thunkable.

You mean a variable scoped to the screen, right?

You can accomplish this by using function parameter variables. These are only accessible on the screen they live in

[![](https://i.vimeocdn.com/video/1191202221-2d09a82a79debcd60c554f30956bee23cad6623b40178896799fc4e7aa6d365a-d?f=webp "Untitled") ](https://vimeo.com/576004145)

---

<div class="post-metadata">

**Author:** ![jonathonschnoor6x4b5](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jonathonschnoor6x4b5/32/112262_2.png) [@jonathonschnoor6x4b5](https://community.thunkable.com/u/jonathonschnoor6x4b5)\
**Post date:** [July 17, 2021, 6:35pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/18 "2021-07-17T18:35:46Z")

</div>

No, a local variable is a variable that is limited to one sprite only.

The use of this is that a lot of sprites can have different values in the same variable, without having to create all those variables.

(also that picture is very blurred I can’t really see what’s going on.)

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [July 17, 2021, 7:00pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/19 "2021-07-17T19:00:38Z")

</div>

They can still be local in this sense. You can type a variable to be an object with key value pairs or have paths or both. This way you can assign many values to 1 variable in an orderly fashion.

```auto
SpriteVar:{
“Value1”:34,
“Value2”:485
}

```

For example

If I get to a computer I’ll while up an example. I use pathways to save data to cloud variables to objects.

You can do the same idea for app variables and in effect make each variable it’s own JSON tree filled with different key:value pairs and buckets to hold different info.

It’s a nice way to cut down on the total number of variables you use. But it requires you to be mindful of the data structure at all times.

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 1:19pm UTC](https://community.thunkable.com/t/reasons-why-local-vars-should-be-added/1396488/20 "2024-11-08T13:19:44Z")

</div>


