# When to use self? When to use local?

**URL:** <https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058>\
**Category:** Questions\
**Created:** [June 6, 2016, 5:03pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058 "2016-06-06T17:03:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_Long](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/xavier_long/32/1299_2.png) [@Xavier\_Long](https://forum.defold.com/u/Xavier_Long)\
**Post date:** [June 6, 2016, 5:03pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/1 "2016-06-06T17:03:56Z")

</div>

Whats the main difference between using **self** and **local** to declare variables?

When is one more appropriate than the other?

My current working understanding is that **local** would be best used inside of function/if blocks

and **self** would be a kind of global variable within the scope of a script/gameobject.

I’d appreciate help to expand my understanding, thanks.

---

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [June 6, 2016, 5:12pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/2 "2016-06-06T17:12:53Z")

</div>

If you would create a local variable in a script like this:

```
local speed = 1

function update(self, dt)
speed = speed + 1 * dt
end

```

it would be a problem as soon as you use several instances of the same gameobject as they would all refer to the same variable ‘speed’. If you had 5 cars with this script speed would be updated 5 times in the same frame and all cars would have the same speed.

If you on the other hand would do this:

```
function init(self)
self.speed = 0
end

function update(self, dt)
self.speed = self.speed + 1 * dt
end

```

Here all the cars will have an individual speed in self.speed.

Conclusion.  
self is an instance variable meanwhile local will be shared with all using the same script.

---

<div class="post-metadata">

**Author:** ![Xavier\_Long](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/xavier_long/32/1299_2.png) [@Xavier\_Long](https://forum.defold.com/u/Xavier_Long)\
**Post date:** [June 6, 2016, 5:27pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/3 "2016-06-06T17:27:54Z")

</div>

> [@Andreas\_Jirenius](#):
>
> self is an instance variable meanwhile local will be shared with all using the same script.

What about a local variable defined within a statement block?

i.e.

```
if foo == false then
   local speed = random(1,5)
   --do something with speed
end

```

so the variable is recreated and would be unique on a per instance basis. yes?

Also i understand that self is on a per instance basis.

However is there any difference between

```
self.speed = 5
--and
go.property("speed", 5)

```

besides being viewable in the inspector?

Thanks for answering

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 6, 2016, 6:00pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/4 "2016-06-06T18:00:56Z")

</div>

> [@Xavier\_Long](#):
>
> What about a local variable defined within a statement block?
> 
> i.e.
> 
> if foo == false then  
> local speed = random(1,5)  
> –do something with speed  
> end
> 
> so the variable is recreated and would be unique on a per instance basis. yes?

Correct. A local variable is available only in the block where it is declared. It’s only local variables declared outside the scope of the `init()`, `final()`, `update()` etc that are available to all scripts of the same type. Consider them similar to `private static` variable declarations in a Java class or something similar.

> [@Xavier\_Long](#):
>
> However is there any difference between
> 
> self.speed = 5  
> –and  
> go.property(“speed”, 5)  
> besides being viewable in the inspector?

A go.property() declared variable is accessible by other scripts using go.get().

---

<div class="post-metadata">

**Author:** ![Xavier\_Long](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/xavier_long/32/1299_2.png) [@Xavier\_Long](https://forum.defold.com/u/Xavier_Long)\
**Post date:** [June 6, 2016, 6:04pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/5 "2016-06-06T18:04:23Z")

</div>

Very eye opening.

I can truely see where I need to change my code to make it more efficent and avoid slip ups.

Thanks for the info

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [June 7, 2016, 8:04am UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/6 "2016-06-07T08:04:22Z")

</div>

More info [Best practice: Local variable or go.property?](https://forum.defold.com/t/best-practice-local-variable-or-go-property/885)

---

<div class="post-metadata">

**Author:** ![bensawyer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/bensawyer/32/18843_2.png) [@bensawyer](https://forum.defold.com/u/bensawyer)\
**Post date:** [June 7, 2016, 8:43pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/7 "2016-06-07T20:43:35Z")

</div>

So is it considered good practice to use local variables declared outside a block - or better to use go.property() instead? Just trying to parse this further.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 7, 2016, 10:18pm UTC](https://forum.defold.com/t/when-to-use-self-when-to-use-local/2058/8 "2016-06-07T22:18:36Z")

</div>

This is what I usually do:

go.property(“foobar”, 123)  
If would primarily use go.property() when I need a variable on a script to vary between script instances (eg changed from the Properties window or when passed as an argument to factory.create)

self.foobar = 123  
I would use this for any other instance variable that I need on my scripts

local foobar = 123  
I use local variables declared at the top level of a script for stuff I need to share between multiple instances of the same type of script. This can be some kind of shared counter or perhaps a shared list of instances so I can do things like broadcasting to all script instances of a certain type.
