Tips/tutorials on project organization with OOP/module scripts

In fact, advice about this can be given endlessly.

  1. Separation of Concerns
    Try to divide the logic of the game into separate, independent modules, each of which is responsible for its specific task
  • For example: the script for a custom death screen should be divided into a script of a pair of modules, DeadHandler and DeadUIcomponent with different methods. In DeadHandler, you create an OnStart function in which you specify the logic for creating a UI at death(I mean, you don’t create it in the same script, it’s done by DeadUIComponent, from which you require everything.). In the same module, you return the OnStart function. Well, then I personally create the core of the project. which simply require all modules and uses the OnStart function
    image
    Core script:
    image
    Client loader script:
    image
    Modules structure:
    image
    But, you don’t have to repeat after me, I just decided to share.
  • Using folders for organization. The choice of style is yours. Organizing your game

Design Patterns:
the most used ones are singletons and factories. The former will help you set up objects quickly, and the latter… Pretty useless, just a special case of OOP

Singleton: Ensures that the class has only one instance. Useful for managers who need to be globally accessible.
Observer: Defines a one-to-many relationship between objects. Useful for events and notifications.
Factory: Used to create objects without specifying a specific class. It is useful for abstracting the process of creating objects.
State: Allows an object to change its behavior depending on its internal state. Useful for managing player states, AI, etc.

** Coding Style:** Adhere to a consistent coding style. Use clear names for variables and functions. Write comments explaining the logic of your code.

Use modules! This is the “basis” for creating reusable code.
As for the reusable code, you should definitely use a utilitarian style. Just create utilities. For example, MathUtils can contain functions like Lerp, etc.
In the end, I advise you to document the function(before declaring it. Thanks to this, when you use it, you will immediately see this comment in the hint), and the code itself - only if it is an “important point”.
And also about functions, be sure to type at least its arguments and output.
Bad practice:

local function lerp(a, b, t)
	return a + (b - a) * t
end

Good practice:

--Linear interpolation between the initial and final values.
local function Lerp(startValue: number, endValue: number, alpha: number): number
	return startValue + (endValue - startValue) * alpha
end

although, to be honest, I’m not sure, most likely no one will mind a,b,t, because it is very often used, however, I am more than sure that a beginner will not understand anything if you show him this function without saying its name.

P.S. STUPID TRANSLATOR😣

5 Likes