The Basics of UPBGE's Python Components

The UPBGE project has brought to us many new and cool features. The "Python Components" feature is one of them.

Its main goal is to replicate the behaviour of the Unity components, providing a new way of coding your game logic without logic bricks, preventing spaghetti.

"Spaghetti is delicious" they said...
But how do we use it?

If the Python Docs from UPBGE aren't clear enough, I'm here to teach you The Basics of UPBGE's Python Components.

First of all in order to use this new concept, we should adopt a new programming paradigm, and that's OOP (Object-Oriented Programming) since a component is nothing more than a class that extends the KX_PythonComponent type.
Let's break down the code provided in the Python Docs from UPBGE:
import bge
from collections import OrderedDict

class ThirdPerson(bge.types.KX_PythonComponent):
    """Basic third person controls

    W: move forward
    A: turn left
    S: move backward
    D: turn right

    """

    args = OrderedDict([
        ("Move Speed", 0.1),
        ("Turn Speed", 0.04)
    ])

    def start(self, args):
        self.move_speed = args['Move Speed']
        self.turn_speed = args['Turn Speed']

    def update(self):
        keyboard = bge.logic.keyboard.events

        move = 0
        rotate = 0

        if keyboard[bge.events.WKEY]:
            move += self.move_speed
        if keyboard[bge.events.SKEY]:
            move -= self.move_speed

        if keyboard[bge.events.AKEY]:
            rotate += self.turn_speed
        if keyboard[bge.events.DKEY]:
            rotate -= self.turn_speed

        self.object.applyMovement((0, move, 0), True)
        self.object.applyRotation((0, 0, rotate), True)

First we have the imports. It's important to notice that it uses "import bge" only, because the system creates a fake module in order for "bge" to work outside the BGE.
Then we import the OrderedDict type from collections, this allows us to have more control over our properties displayed on the UI, you can use a normal dict instead if you don't care about the order.

class ThirdPerson(bge.types.KX_PythonComponent)

This is the important part. First we create a class with the component name, then we extend the type KX_PythonComponent, so now that class is a Python Component.

Next we need to define the component arguments, the ones that are displayed in the UI.
That's where we use our dict (or OrderedDict):

args = OrderedDict([
    ("Move Speed", 0.1),
    ("Turn Speed", 0.04)
])

Now another important part. The component must implement two methods: start and update:

The start method is called once, and is used to set values up. The args parameter is the same as the args variable from previously, except the values are the new values updated from the UI.
The update method if called every frame, and there is where you should run your logic.

That's all you need to do to create a Python Component, now you just need to add it to the current object:

First make sure your script has the .py extension, then in the Game Logic screen layout, above Properties you will find the Components panel, just click Add Component and type in the path of the component. The path is
script_name.class_name

Python Component UI
So in this case, if the script name is "tp.py", your path will be "tp.ThirdPerson". If everything is right, you should see the UI with the arguments.

And that's it, you have functional game logic without logic bricks and without spaghetti.

Until next time!

Comments

Post a Comment

Popular Posts