Lifecycles

Chapters and Characters are given optional lifecycles which can automate their usage.

YOU WILL LEARN HOW TO

  • Identify lifecycle hooks

  • Use lifecycle hooks


Default Lifecycles

By adding lifecycle hooks to your Chapters and Characters, you can allow them to run autonomously. Below are the basic lifecycles that Quill provides:

1

Chapters & Characters are initialized

:init() is called

2

Chapters & Characters are started

:start() is called

3

Chapters & Characters can be destroyed

If a :destroy() function is specified, then it will be called whenever your Chapter/Character is destroyed. This essentially acts as a cleanup function.

All :init() and :start() functions are called at the same time.


Best Use-Cases

While there is no definitive rule about how you should use your lifecycle hooks, the general rule of thumb is to include variables and constants that your Chapter or Character will use in the :init() lifecycle hook. For example, you may have a Chapter that adds players to a table. You can add an empty table when your Chapter is initialized:

function my_chapter:init()
    self.players = {}
end

Now you can add a player to the table by adding a connection to the :start() lifecycle hook:

function my_chapter:start()
    Players.PlayerAdded:Connect(function(player)
        self.players[player] = player
    end)
end

You can then add a function which prints the number of players in your players table:

function my_chapter:count_players()
    print("There are " .. #self.players .. " present in-game!")
end

Summary

  • Lifecycle hooks are methods that are called automatically upon starting Quill.

  • You can use lifecycle hooks to automate your workflow with ease.

Last updated