Character logic help

The one on top (the Character Class) “contains” references to all of the ones below! Think of it like this:

The Character Class is created first by requiring the module and then calling .new() to create a new “class object” which is just a table with data. Upon creation, the Character Class then creates a copy of each sub-manager below it in the same way, and store it as a reference within its own table.

An example of this:

function CharacterClass.new()
    local self = -- set metatable here
    self.EffectManager = require(script.EffectManager).new(self)
    self.WeaponManager = require(script.WeaponManager).new(self)
    self.CharacterMovement = require(script.CharacterMovement).new(self)
    -- etc...
end

I like to pass through a reference of the CharacterClass when creating .new() for the sub-managers, so that they can look at the CharacterClass’s references and talk to the other managers to access their data and methods, for example accessing the character movement data from a weapon module script.

function WeaponManager.new(CharacterClass)
    local self = -- set metatable here
    
    self.CharacterClass = CharacterClass
    print(CharacterClass.CharacterMovement.movementSpeed) -- i can access the other thing!
    -- etc...
end

Unfortunately Lua doesn’t have a native way to conveniently create custom “classes” like you would in say C#, but you can still do it using metatables. A random tutorial on OOP to help you get started:

4 Likes