Best way to structure my code (SOLVED)

Good evening! Bit of a noob question here as I’m still just learning.

I have a script, let’s call it ‘gameController.script’ and in the update function I have a function call, let’s’ call it ‘moveBalls()’

My question is, where should I put the function moveBalls() itself? Should it go inside the update function, or alongside the update function in the same script file, or should it go in a separate lua module? I’ve tried all three and they all seem to work, so I’m interested to know what’s the best practice and why.

Many thanks! :slight_smile:

2 Likes

It depends somewhat on the context but generally speaking I would do this:

local function moveBalls(self, dt)
--
end

function update(self, dt)
    moveBalls(self, dt)
end

This keeps the function within the scope of which you intend to use it.

If you had, say, a utility function that you need in various scripts, you could put that in a lua module.

2 Likes

Perfect explanation - thank you.