How would you use OOP?

-- Skill Functions 
return {
    SwipeSword = function(self, target)
        if self.Weapon.Type ~= "Sword" then return "You need to equip a sword to use this skill" end
        target:TakeDamage(self.Damage * self.Weapon.DamageMultiplier)
    end
    SpinAttack = function(self, target)
        -- code
    end
    AirAttack= function(self, target)
        -- code
    end
    DoubleShot = function(self, target)
        -- code
    end
}
-- Character Data
local SkillFunctions = require(game.ReplicatedStorage.SkillFunctions)

return {
    Swordsman = {
        Instance = game.ReplicatedStorage.Characters.Swordsman,
        Health = 10,
        Damage = 3,
        MainSkill = SkillFunctions.SwipeSword,
        SecondarySkill = SkillFunctions.SpinAttack,
    }
    Bowman = {
        Instance = game.ReplicatedStorage.Characters.Bowman,
        Health = 6,
        Damage = 4,
        MainSkill = SkillFunctions.AirAttack,
        SecondarySkill = SkillFunctions.DoubleShot,
    }
}
-- Character Class
local CharacterData = require(game.ReplicatedStorage.CharacterData)
local Notification = require(game.ReplicatedStorage.Notification)

local class = {}
class.__index = class

function class.new(characterType)
    local data = CharacterData[characterType]
    assert(data, "Character type not found")

    local self = setmetatable({}, class)
    self.Type = characterType
    self.Instance = data.Instance:Clone()
    self.Health = data.Health
    self.Damage = data.Damage
    self.MainSkillFunction = data.MainSkill
    self.SecondarySkillFunction = data.SecondarySkill
    return self
end

function class:GiveWeapon(weapon)
    self:DropWeapon()
    self.Weapon = weapon
    self.Weapon.Instance.Weld.Part0 = self.Instance.RightHand
end

function class:DropWeapon()
    if self.Weapon then
        self.Weapon.Instance.Weld.Part0 = nil
        self.Weapon = nil
    end
end

function class:MainSkill(target)
    local notification = self:MainSkillFunction(target)
    if notification then Notification:Notify(notification) end
end

function class:SecondarySkill(target)
    local notification = self:SecondarySkillFunction(target)
    if notification then Notification:Notify(notification) end
end

function class:TakeDamage(amount)
    self.Health -= amount
    if self.Health <= 0 then
        self:DropWeapon()
        self:Destroy()
    end
end

function class:Destroy()
    -- code
end

return class
local CharacterClass = require(game.ReplicatedStorage.CharacterClass)
local WeaponClass = require(game.ReplicatedStorage.WeaponClass)

-- create 2 characters
local character1 = CharacterClass.new("Swordsman")
local character2 = CharacterClass.new("Bowman")

-- give character 1 a sword
local sword = WeaponClass.new("Sword")
character1:GiveWeapon(sword)

-- character 1 uses there main skill on character 2
character1:MainSkill(character2)

-- character 2 uses there secondary skill on character 1
character2:SecondarySkill(character1)

One thing you might notice when doing object oriented design is that you run into problems with type checking because when you pass a object to another script that script wont know that objects type unless you require there module and import the type and then you can easily run into circular dependencies if your always requiring all the classes that’s why I made Global Framework

Global Framework makes OOP and type checking easier

5 Likes