Character logic help

How should I go about making the characters function in a character select system? What I mean by this is, how should I handle the data for the characters, like should I have a module script for each move to handle the logic and have their animations and keybinds attached to it? Or is there a better way I can go about it?

An object-inheritance style system would work well here. Create base ‘super’ classes (using ModuleScripts) that have all of the base logic needed for general movesets and characters, then create sub-modules off of those that inherit those methods and have custom logic for each character ability.

As for the actual character control, you’d have your general character & input logic within a custom character class script setup.

Example of how my class hierarchy looks (handlers for actions, character states, movement, input, animations etc):

1 Like

Wow, thats actually insanely helpful, my only question is how do you keep coordination between all the managers?

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

:rofl: whips out a logic flowchart. /bow

3 Likes

I seriously appreciate the effort you went through to explain this, I had some prior knowledge on oop, but this really helped to get a better visual on it. Plus, it gave me a good understanding on a pretty ideal way to handle characters. Thanks a bunch dude!

1 Like

Np! And just keep in mind that my way isn’t necessarily the DEFINITIVE way, it’s just a way that I know works well and with maximum communication and organisation between different modules, as the framework is set up how it may be in another engine like Unity with custom C# classes :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.