Create a component

In this tutorial we will use the entity we created here and attach a new component reflecting rigidbody functionalities, holding data of the object’s mass and drag.

Lets create first our rigidbody component

class RigidBody(Component):
gravity = 10

def __init__(self, name=None, type=None, id=None):

    super().__init__(name, type, id)

    self.mass = 100
    self.drag = 5

def show(self):
    print('This is the gravity component. Gravity: ' + str(self.gravity) + ' Drag: ' + str(self.drag) + ' Mass: ' + str(self.mass))

def update(self, **kwargs):
    pass

def accept(self, system: System, event = None):
    system.applyGravityEffect(self)

def init(self):
    pass

This is a simple component holding the physical properties of our object.

Pay attention to the following points:

  1. We inherit the Component class.

  2. The class implements the update() accept() and init() abstract methods. This is mandatory.

  3. We added mass, drag and gravity (static var) as the main data of our component.

  4. We implemented the show() me method to print the data (this is optional of course).

  5. The accept() method calls the system.applyGravityEffect(self). This means that when the GravitySystem visits this component it will apply the gravity effect. We will mention this in the next tutorial.

The following code creates a RigidBody component. Then we call the show() method just to see that our component works properly.

rigidBody = RigidBody("gravity", "RigidBody", "1")
rigidBody.show()

The final step is to attach the component to our object.

gameObject.add(rigidBody)