Jump to content

[Documentation] Understanding Components


Recommended Posts

Understanding Components

Hello and welcome to this documentation post on understanding components! I’ll help you understand components and how to use them.

So, what is a component?:
A component is something an entity can do. For example, a birchnut does different things like being able to be examined, cooked, go into inventories, be planted. So a birchnut would have different components like the “inspectable”, “cookable”, “inventoryitem”, “deployable” components to accomplish all those things I mentioned. Every component in the game can be found as files in the scripts/components folder of the games folder.


Why do we have them?:
We have components because it makes it really easy to add pre-existing mechanics to new entities. Could you imagine if the code for examining an entity had to be manually done in every single prefab file? That would be terrible! But of course, instead we have an “inspectable” component file and if we want something to be examinable, we simply add the inspectable component to the entity.

What does a component file look like?:
At its most basic form, a component’s file will look like this:

local Component = Class(function(self, inst)
      self.inst = inst

return Component

In this snippet of code, a new Class is created and assigned to the “Component” variable and the Component variable is returned. That’s the most basic form of a component. But of course, most components don’t look like that, most are much much more complicated.

How are components added?:
Components are generally added when an entity is being initially constructed from their Prefab file.


All components are added through the AddComponent function available to all entities in the game.

In the code snippet above, we’re adding the “inspectable” component, so the game searches in the components folder for an inspectable file and finds it(Well hopefully it finds it. If the game doesn’t find the component you asked for it will crash!). The component initializes and is now accessible with:


We can now use this specific instance of the component however we like, set and change new/old variables, call functions from it, etc.

That’s a simple explanation of components done, if there’s something you’re still confused about or something you think should be added to this post then please let me know and i’ll see about it! I may add a little snippet on replicable components later on and how they work :)

  • Like 8
  • Thanks 2
  • Wavey 1
  • Shopcat 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...