Say you create your oop for skills, and make like 5 skills for a project you’re working on. How would you get a table / array of all of the skills you’ve made so that you can check for things?
e.g of OOP
local Skills = {}
Skills.__index = Skills
function Skills:get()
return -- ?? This is where I'm confused. Idk how to get the table they're all from.
end
function Skills:new(SkillName)
local Skill = {}
setmetatable(Skill, Skills)
Skill.SkillName = SkillName
return Skill
end
return Skills
Maybe I could return Skills itself but the problem with returning a dictionary of the module is that it will also give me functions and I don’t think it will give me the skills I create whenever I call Skills:new().
I’m not entirely sure if this is called a singleton structure, but my suggestion would be to have your Skills class create a new base instance, in which then you can create new skill objects relating to your base instance of the Skills class. (so essentially all of your created skills get encapsulated in each instance of the skills base object)
I’m not sure if you need something unique for a per-player basis, or if all the skills can be stored in a global table essentially taking into account every skill that’s been created.
If that’s okay with you, you could just store all of the created skills in another table in your module, and then in your getter method Skills:get() you could return that table.
If you need to encapsulate all the skills in each new instantiation of the Skills class, you could probably just create a new table field in each skills object and then add all the created skills to that table instead, so each time you construct a new skills object you also get a new skills table.
I hope that makes sense
If you need me to break something down I’ll gladly do so, but here’s an example of what I mean:
function Skills:createSkill(skillName)
self.skills[skillName] = true --//Add the new skill to the table within the skills object
end
function Skills:getSkills() --//And then in your getter, you could just return the skills table.
return self.skills
end
function Skills.new() --//Typically it's good practice to use a period for constructors, as they are responsible for creating new objects of a class
local skillObject = setmetatable({}, Skills)
--//Field declarations
skillObject.skills = {} --//You could create a new table in each new skills object, then add all of your skills to that instead.
return skillObject
end