GameBlender 4 Ever
Advertisement

The Advantages of Python[]

The core of game making isn’t how good your game looks, but how it feels to play. When a player first picks up a first person shooter, they expect the mouse to aim, WSAD to move, and there to be a damage and ammo readout on the screen. Blender presents us with two ways to create these interactions: Logic bricks and Python.

Screenshot1

A simple, but long logic setup

Logic bricks have several limitations though, because they cannot convey anything except True and False, and because even simple things require stacks of logic.

To the right is a comparison of a logic setup and a python setup. It is just a key-based setup for a space-ship, this section dealing with key based movement, and the logic version continues down the screen for several (virtual) meters.

Screenshot2

What python can offer

It is a nice relief that this entire setup can be replaced by a trio of bricks.

Python also gives us access to numerical access to data from sensors, so instead of just that the mouse is moving, we can tell where it is and what it is over. 




Accessing Python[]

The best thing is that python is already there waiting for you to use, there are no addons to install or any requirements other than blender itself. However there are a few useful things to know.

The System Console[]

Blender Tip Console toggle

Opening the console in Windows (image from learningblender3dsoftware.blogspot)

The system console is where we can see the output of any scripts we write. This includes syntax errors and variables we chose to display. Each different operating system has it’s own way of accessing the system console.  For Windows, you simply go to View -> Toggle System Console, and it will appear.

Screenshot from 2013-06-16 23-13-15

Starting the system console in Linux

For Linux you have to start blender from a terminal. If you have blender installed you can simply open a terminal and type blender, but if you just have it in a folder, you have to type the path as well. 

I don’t have a mac, so I’ll just point you to someone elses instructions




The Text Editor[]

Screenshot from 2013-06-16 23-20-01

Opening the text editor

For starters, it’s easiest to code right from inside blender! To do this you turn one of the windows into a text editor. This is done by clicking the icon in the header bar of a window and selecting ‘Text Editor.’ You can change it back when you’ve finished scripting as well.



The bottom bar of the text editor holds some useful buttons:

Screenshot from 2013-06-17 21-43-34

The Text Editor Bar

  • The three buttons at the right turn on word wrap, the line numbers, and python syntac highlighting. Most of the time I have all three of these options enabled.

  • The big button (that starts off labeled new) is used to select and rename existing files. 




Python and the Frame[]

Before we go and actually start writing a script, it is good to know how python scripts are run inside of a game. Because the python controller is a controller logic brick, it is run whenever the sensor connected to itself is triggered. Each time it runs it starts from scratch, meaning that any variables we define in a script get reset.

Monster on blenderartists provides a good reference if you are interested in reading further.


If You Get Stuck[]

Everyone get’s stuck with some problems, and there is a good chance someone has hit the same issue as you before, so go ahead and google it. If you can’t find an answer though don’t feel ashamed to sign up and ask questions in the game forum on blenderartists. Another useful link to know is that of the blender API. I’ll go into more detail later in the tutorial series, but for now just know that it is the summary of all the commands possible with python in blender.

Advertisement