Best practices for "update" code re-usage

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:

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?

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?

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)

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.

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

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.