# Best practices for "update" code re-usage

**URL:** <https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724>\
**Category:** Questions\
**Created:** [November 13, 2018, 12:02pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724 "2018-11-13T12:02:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustafsson.ea](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gustafsson.ea](https://forum.defold.com/u/gustafsson.ea)\
**Post date:** [November 13, 2018, 12:02pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724/1 "2018-11-13T12:02:33Z")

</div>

I’m wondering about best practices for reusing code that is put in the update function.

For example movement.  
If I want movement to be handled the same for different units should I put the code into a function that I then put into a LUA module and call this from the update function?  
Like so:

```lua
local movement = require "..."

function update(self, dt)
    movement.move()
end

```

I’m also not sure how I would reference the unit’s direction, speed, etc, should I pass those as arguments?

```lua
function update(self, dt)
    movement.move(dir, speed, pos, dt)
end

```

Is LUA module the way to go or is there another good way to do it?

---

<div class="post-metadata">

**Author:** ![marco.giorgini](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/marco.giorgini/32/8409_2.png) [@marco.giorgini](https://forum.defold.com/u/marco.giorgini)\
**Post date:** [November 13, 2018, 12:06pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724/2 "2018-11-13T12:06:12Z")

</div>

not a LUA expert - but I would write something like this

movement.move(self,dt)

and put dir speed in self (self.dir self.speed) while I’d keep pos as an element I’d get from gameobject (or not - it’s your call)

---

<div class="post-metadata">

**Author:** ![benjames171](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/benjames171/32/34759_2.png) [@benjames171](https://forum.defold.com/u/benjames171)\
**Post date:** [November 13, 2018, 12:25pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724/3 "2018-11-13T12:25:01Z")

</div>

The way I do this is to have a script file with the common code in and another script specific for particular units. Then just add both scripts to the game objects. Not sure if this is best practice but it works well for me.

---

<div class="post-metadata">

**Author:** ![gustafsson.ea](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gustafsson.ea](https://forum.defold.com/u/gustafsson.ea)\
**Post date:** [November 13, 2018, 12:59pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724/4 "2018-11-13T12:59:05Z")

</div>

> [@marco.giorgini](#):
>
> movement.move(self,dt)

If I can just pass the self “object” then this might be the way I do it.

> [@benjames171](#):
>
> The way I do this is to have a script file with the common code in and another script specific for particular units. Then just add both scripts to the game objects. Not sure if this is best practice but it works well for me.

I was thinking of this and while it would probably work fine it bothers me that some functions like update will be called twice per game object.

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [November 13, 2018, 1:27pm UTC](https://forum.defold.com/t/best-practices-for-update-code-re-usage/34724/5 "2018-11-13T13:27:57Z")

</div>

> [@gustafsson.ea](#):
>
> I was thinking of this and while it would probably work fine it bothers me that some functions like update will be called twice per game object.

If you have an update function, yes. Remove it if you don’t use it. Also remember that ”self” refers to the component, not the game object so the two scripts won’t share self.
