Greetings, dear friends, I recently wrote a “quest class” module for one of my projects.
The module returns a “quests class” which has 2 methods and a quest constructor.
The quest class itself has 8 methods and 3 events. Events made with the help of “Good Signal”. Class fields are protected (Private table), while the constructor returns only an open table with methods.
In principle, I am happy with everything in this code, but the Remove method of the quest class does not give me peace of mind.
- Please let me know if there is a memory leak using this method.
If you have suggestions on how to improve / speed up the code, please reply to this post.
I will attach a file with a place in which there is this module and a script for a small test.
Baseplate.rbxl (37.2 KB)
local Quests = {}
local signalModule = require(script.GoodSignal)
Quests.Public = {}
Quests.Private = {}
Quests.Private.CurrentQuests = {}
function Quests.Public:GetQuestsCount() :number
return #Quests.Private.CurrentQuests
end
function Quests.Public:RemoveAllQuests()
for _, quest in pairs(Quests.Private.CurrentQuests) do
quest:Remove()
end
table.clear(Quests.Private.CurrentQuests)
end
function Quests.Public:Create(Player:Player,Reward:string,Steps:{})
assert(Player,"Player expected got nil")
assert(Reward,"String reward expected got nil")
assert(Steps,"QuestSteps expected got nil")
assert(not (#Steps == 0),"QuestSteps cannot be empty")
local Quest = {}
Quest.Public = {}
Quest.Private = {}
Quest.Private.Reward = Reward
Quest.Private.Steps = Steps
Quest.Private.CurrentStep = 0
Quest.Private.CurrentTask = ""
function Quest.Public:SkipQuest()
Quest.Private.CurrentStep = #Steps
Quest.Public.Ended:Fire(Quest.Private.CurrentStep)
end
function Quest.Public:NextStep()
if Quest.Private.CurrentStep == #Steps then
return
end
Quest.Private.CurrentStep += 1
Quest.Private.CurrentTask = Quest.Private.Steps[Quest.Private.CurrentStep]
Quest.Public.Stepped:Fire(Quest.Private.CurrentStep)
if Quest.Private.CurrentStep >= #Steps then
Quest.Public.Ended:Fire(Quest.Private.CurrentStep)
end
end
function Quest.Public:StartQuest()
Quest.Private.CurrentStep = 1
Quest.Private.CurrentTask = Quest.Private.Steps[Quest.Private.CurrentStep]
Quest.Public.Started:Fire(Quest.Private.CurrentStep)
Quest.Public.Stepped:Fire(Quest.Private.CurrentStep)
end
function Quest.Public:Remove()
Quest.Public.Ended:DisconnectAll()
Quest.Public.Stepped:DisconnectAll()
Quest.Public.Started:DisconnectAll()
table.clear(Quest.Public.Stepped)
table.clear(Quest.Public.Started)
table.clear(Quest.Public.Ended)
table.clear(Quest.Public)
table.clear(Quest.Private)
table.clear(Quest)
Quest = nil
end
function Quest.Public:GetSteps() :{}
return Quest.Private.Steps
end
function Quest.Public:GetCurrentTask() :string
return Quest.Private.CurrentTask
end
function Quest.Public:GetCurrentStep() :number
return Quest.Private.CurrentStep
end
function Quest.Public:GetReward() :string
return Quest.Private.Reward
end
local Stepped = signalModule.new()
local Ended = signalModule.new()
local Started = signalModule.new()
Quest.Public.Stepped = Stepped
Quest.Public.Ended = Ended
Quest.Public.Started = Started
table.insert(Quests.Private.CurrentQuests,Quest.Public)
return Quest.Public
end
return Quests.Public
Post about “Good Signal” module