Components
Learn about using Components with the Kakara Engine.
Components are what give GameItems functionality. The engine provides several pre-made components to provide common functionality to your GameItems. We have already used two major components: Transform and MeshRenderer.
Add Components
Before we talk about the existing components and creating our own, we need to learn how to add them. Component can be added through the addComponent()
method.
That adds the MyComponent
component to the GameItem. You cannot add multiple of a single type of component to a GameItem.
Removing Components
Components can be removed through the removeComponent()
method.
A component will only be removed if it exists in the GameItem.
Getting Components
You can retrieve components through the getComponent()
method.
If the specified component does not exist in the game item than the method will return null.
Transform Component
The transform component is the most common component and holds the position, rotation, and scale of the game item. The transform component is mandatory and all game items have it by default.
Unlike other components you cannot add or remove the transform component (An exception will be thrown if you try). The transform component also has a special public field so you don't have to use the getComponent()
method.
So you can use
instead of:
Both will work.
Mesh Renderer Component
The mesh renderer component handles the rendering of a mesh for a GameItem. If you use the GameItem constructor that accepts a mesh or array of meshes, than this component is added automatically. The following code snippets are equivalent:
Since the MeshRenderer component is such a common component, it also has a direct way to reference it. You can use the getMeshRenderer()
method to retrieve the MeshRenderer. It is important to note that the getMeshRenderer()
method returns an Optional
since a MeshRender is not required.
The mesh render has special behavior when added and deleted. Due to the way the Standard Render Pipeline works, when a MeshRenderer is added or removed, the game item is temporary removed than added back to the list of game items in the scene.
Custom Components
Now that we know about components, let's add in functionality to make our cube rotate. To do that we need to make a new class for our component. Lets make a new class called RotateCube
and have it extend the Component
abstract class.
The start
method is called when the component is added to a game item and the update
method is called every update. Components additional methods that you can override for additional functionality. We will not talk about any other methods for now.
Since we want our cube to rotate, we need to modify the transform component. The Component abstract class provided us with the getGameItem()
method so we can easily get the game item that the component is on. So in the update
method let's add the following:
The code above causes the cube to rate 5 degrees in the x and y direction every frame. Note: That code is dependent on your FPS.
Adding the Custom Component
Now that we made our custom component, it is time to add it to the cube. Go back to the MainScene
class and add in the following below setting the position of the cube:
Now if you run the code you should see a rotating cube:
Final Code
Last updated