Skip to content

A First example

Leopold A-C edited this page Oct 13, 2020 · 5 revisions

Here we will try print some text to the console when a key is pressed. This could probably be called the "Hello World" example for this Add-On.

Activating the Add-on.

The newer versions of UPBGE (0.3.0) come with it pre-installed. For 0.2.5 and below or custom installs, see Installing the Add-on.

Head to Edit -> Preferences -> Add-ons and filter for Game Engine. There, check the box for the Logic Nodes and you're good to go.

Creating A New Logic Tree

Now, let's get started. First we will need to create a logic tree. For this, check the view menu for a new editor called Logic Tree Editor (Should be under "General"). This editor is akin to the Shader Editor, so creating a new logic tree is the same as creating a new Material. Just click on New and a new tree (named NodeTree by default) should appear.

Adding Nodes

Let's make something happen here. When you press Shift + A, you'll notice that you can add new nodes now. Go ahead, take a look at what's at your disposal.

For this example, we're looking for two nodes: the Key Pressed node and the Print node. If you can't find them, you can also search for those words.

The Key Pressed node is a node of the condition type. These nodes do not actually do anything in-game, but they either provide a condition or can be used to check for a more complex set of conditions.

The Print node is an action type node. These nodes actually do something. They move objects, change properties, add constraints, you name it.

Now we need to link the two together. The Key Pressed node has a Condition Socket, they are colored red. Connect that to the condition socket of the print node and enter "Hello World" in the text box. Also, if you didn't already, look at the Key Pressed node and you'll see that it asks you to choose a key. It should look something like this now:

hellowoarld

Applying Logic Trees

Once you've done that, all that's left is to apply the tree to an object. Logic trees work the following way: each tree can be applied to as many objects as you want, meaning it is executed by each object it is applied to separately.

Example: If you attached this tree to 4 objects and pressed the key ONCE, the message would be printed 4 times, once for each object.

To apply a tree to a cube, first add a cube, select it and press Apply to selected in the logic tree editor under the Item tab. Careful: trees can be applied to multiple objects at once!

System Console

Now! Last step! Hit Window -> Toggle System Console (for Linux and Mac users: save the file and start blender via console again), start your game and hit that key you entered before. Quit the game, check the system console and there should be your message, printed as many times as you hit the key.
This console is also the place where your errors will be printed if you are working in Debug Mode, which we'll try in our next example.

Note: The Print node prints to the system console only, not to the python interactive console. Sadly this is not changable.