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?
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 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.