Search Results

Sunday, January 23, 2011

Using Python in the Blender Game Engine - Part 2.5

Hey. So before we move on to the next part of the tutorial, some code house-keeping is in order to help make our source code more manageable.
While the Player function isn't too long right now, it can easily become much larger and become un-manageable. This would particularly be a problem if we make a mistake or want to add a new feature once the Player's function code balloons to several hundred lines of code. So, here's our source code from part 2 of this tutorial series:
from bge import logic

from bge import events

def Player():
cont = logic.getCurrentController()
obj = cont.owner
motion = cont.actuators['Motion']
key = logic.keyboard.events
kbleft = key[events.LEFTARROWKEY]
kbright = key[events.RIGHTARROWKEY]
mx = 0.0
my = 0.0
if kbleft > 0:
mx = -0.1
elif kbright > 0:
mx = 0.1
motion.dLoc = [mx, my, 0.0]
cont.activate(motion)

Now, then. Let's expand the object's code by making another functions inside of the Player function. How does this work? Well, it's actually quite simple - Python allows us to define a function inside of another function, and so the inside function is only accessible from the 'parent' (outside) function. This just makes the process a bit simpler and more manageable - for example, we can have an Init() function to initialize variables in the object, an Update() function to move that object, and an Animate() function to animate it. With this setup, if we want to add a portion of code, we needn't look in the unrelated portions of code. So, we'll implement this process in our Player's code:
from bge import logic

from bge import events

def Player():
cont = logic.getCurrentController()
obj = cont.owner
motion = cont.actuators['Motion']
key = logic.keyboard.events
kbleft = key[events.LEFTARROWKEY]
kbright = key[events.RIGHTARROWKEY]
def Init():
if not 'init' in obj:
obj['init'] = 1
def Update():
mx = 0.0
my = 0.0
if kbleft > 0:
mx = -0.1
elif kbright > 0:
mx = 0.1
motion.dLoc = [mx, my, 0.0]
cont.activate(motion)
Init()
Update()
As you can see, the change in code is very slight. We simple made two new functions, an Init() function for initializing object variables, and an Update() function for moving an object and interacting with that object's code. The Init() function simply checks to see if the object has been initialized - if a variable named init has been created in the object. Because of the check, this Init() function will only initialize any variables and run completely when the object is first created and doesn't have the variable init contained in it. Our Init() function here creates only the init variable to ensure that the object has been initialized.

The Update() function just contains our original code. We then call the Init() and Update() functions at the end to initialize variables (we only create one, the init variable), and move and update our object. As can be seen, it's actually quite simple to implement a very nice containable system that is easy to use. So, this lesson was short, simple, and sweet, as the other lessons should be. Keep havin' fun with the Blender Game Engine as we head into Part 3 of learning how to Use Python in the Blender Game Engine.

5 comments:

  1. This makes everything stop working...

    ReplyDelete
    Replies
    1. this is late but make sure that you have all the indentations correct,
      all the text should be indented passed the def Player(), the def inti() and def Update should line up close to under the P in def Player(),
      Python is very dependent on format of your code, you have to get the indents correct.

      Delete
  2. Thank you loads for these!
    I've been wanting to get into BGE for quite some time now. Its been hard to find a place to begin - im familiar with python, and barely with game developing with python, but i had no idea how to combine it with BGE, but your tutorials are perfect for introducing people to BGE, and python for that matter.

    Once again, thank you tons for the tutorials! This is exactly what i needed to get into the world of BGE!

    Sincerely, a humble friend.

    ReplyDelete
  3. Thx for your work! Great job.

    1) Just one question, why do i use the initfunction?
    I have several game-properties in my Gameobjects logic section, defined in Blender, after Modelling the objects.
    Those Objects are stored on a different Layer and spawned from that layer in my game.
    So...what is the use of the init() function? I guess it is for more sophisticated uses. But whats the point of using it?

    2) The Motion Actuator has to be build in with logic bricks. The code triggers it, so it should be existant.

    3) Code for new Blenderversions (2.66):
    For the right indentation, look in the original code. only the import statement has changed and how to use the logic and event submodules(?)

    import bge


    def Player():
    cont = bge.logic.getCurrentController()
    obj = cont.owner
    motion = cont.actuators['Motion']
    key = bge.logic.keyboard.events
    kbleft = key[bge.events.LEFTARROWKEY]
    kbright = key[bge.events.RIGHTARROWKEY]

    def Init():
    if not 'init' in obj:
    obj['init'] = 1
    def Update():
    mx = 0.0
    my = 0.0
    if kbleft > 0:
    mx = -0.1
    elif kbright > 0:
    mx = 0.1
    motion.dLoc = [mx, my, 0.0]
    cont.activate(motion)
    Init()
    Update()

    ReplyDelete
    Replies
    1. I don't completely understand your questions, so I'll answer them to the best of my ability.

      1) You use the initialization function to initialize object properties. If you want to do that via the properties panel for each individual object, you can do that.

      The advantage of doing it in code is that objects that share code, like enemies, can have similar properties set for them rather than you having to manually go back and add the properties.

      Note that you don't have to do it this way - whatever way you like and that works is fine; this is just the way I approach it, all in Python.

      2) I'm not sure what you mean.

      3) You import the logic and event modules using the standard Pythonic "from module import submodule" syntax. So, you would do, "from bge import logic, events".

      Delete