In this Blender 2.5 video tutorial series we will be going over the basics of how to use Python in the Blender Game Engine.

In this second part we’ll continue right where we left off and create the bullet’s and obstacles’ scripts so that the bullets will have an effect on the obstacles, depleting their health. We’ll also be using the Fracture Object add-on to make objects with 0 HP fall into pieces.

What is covered in this tutorial:
- Setting and changing object properties.
- Using the Touch and Sound sensors.
- Using the Fracture add-on.

Intro to Scripting in BGE<

You must be logged in to upload images. Register

Discussion

96 Responses to “Introduction to Scripting in the Blender Game Engine – Part 02”
  1. rAINBOW rOAD
    Posts: 5

    The cube disappears instantly instead of taking damage.

    why is this happening? and sound will not play!

    ObstacleScript:
    http://pastebin.com/gnfwPn1s
    BulletScript:
    http://pastebin.com/WiQxiWMU
    playerscript:
    http://pastebin.com/9AhUpZ1P

    .blend file(0.99Mb):
    http://www.sendspace.com/file/4zthxx

    #
    51
    Jun 3, 2012 at 5:24 am
  2. rAINBOW rOAD
    Posts: 5

    using snap to grid makes all the shards not a cube, sort of jumbled up

    #
    52
    Jun 8, 2012 at 9:50 pm
  3. No Name
    Posts: 1

    for obj in bpy.context.selected objects:

    Syntax Error:Invalid Syntax

    location::-1
    location::-1

    How do i fix this?

    #
    53
    Jul 15, 2012 at 2:16 pm
  4. Posts: 3

    I am working on a game and I have two different text data block for scripting. In one of the text data block for one object there is a variable (x) that changes and stuff, and in another text data block for a different object I want it to apply a movement based on the variable (x) from the other text document. Do you know how to do this?

    for example:

    the first text data block:

    if x == True:
    x += 1

    the second text data block:

    block.applyMovement((x, 0, 0), True)

    I want to know how to make the x from the second text data block equal the x from the first text data block.

    #
    54
    Aug 14, 2012 at 11:13 am
    • Guest
      Posts: 2

      You should probaby store the x variable as a property. You can use an empty to store these, e.g.:
      1. Create an empty, name it PublicPropertStorage;
      2. In block one: PublicPropertStorage['x'] += 1 (or whatever);
      3. In block two: aplyMovement((PublicPropertStorage['x'], 0, 0), true);

      This should work. I’m not an expert in python, however I find it is very easy to switch to from C# or Java.

      #
      54.1
      Oct 4, 2012 at 11:51 am
      • Posts: 3

        I found out how to do it. I have to import GameLogic and then use GameLogic.x. Thanks for the help though.

        #
        54.1.1
        Dec 16, 2012 at 1:56 pm
  5. Posts: 2

    Absolutely FANTASTIC tutorial!!!!!!!
    This is EXACTLY what Ive been looking for!
    Patrick – what is your paypal account details, I need to buy you a beer! :)

    #
    55
    Aug 15, 2012 at 6:55 am
  6. Posts: 1

    God, i hate blender, but i’m so far in now i can’t stop.

    THE PRINT STATEMENT NO-LONGER WORKS. (on mine)

    WWWWWWWWWWHHHHHYYYYYYYYYYY??????????????????!!!!!!!!!!!!!!!!!!!??????????????!!

    #
    56
    Aug 15, 2012 at 4:06 pm
  7. Max
    Posts: 2

    Hiya, I have been following this tutorial and it has been great for my Senior Project in making basic video games, but I ran into a couple problems.
    Firstly it won’t let me save the file at all, whenever I try to save it says “Can’t open file /Users/pinkavam/Desktop/Second Shooter.blend@ for writing: Too many open files.” and it won’t let me save, I don’t know what it means by that.
    Secondly the moment I got the block to explode into different pieces, the explosion sound effect wasn’t working or the sound effect I made for when the player shot (That was just a logic brick that made it so when you hit the shooting key (I used space key) that it would make the noise, and since it was the same key as shooting it worked out fine) and I can’t figure that out either.
    And thirdly is just a silly question, my shards have 2 very large shards and a bunch of little ones, any idea how to make it even?
    Sorry to load you with a lot of odd questions, but they are all probably stupid mistakes without me knowing…
    Sincerely,
    Max

    #
    57
    Aug 17, 2012 at 1:14 pm
    • Max
      Posts: 2

      Okay, I’m not sure what the problem was, but it went away after restarting everything and doing it over…But I’m still stuck on the making it so the shards are even, any ideas?

      #
      57.1
      Aug 20, 2012 at 12:18 pm
  8. mandango
    Posts: 1

    Traceback (most recent call last):
    File “bulletScript.py”, line 29, in
    File “bulletScript.py”, line 21, in main
    KeyError: ‘value = gameOb[key]: KX_GameObject, key “hp” does not exist’

    I tried in different ways and always the same error.
    Maybe changes something with Blender 2.64??

    #
    58
    Nov 4, 2012 at 5:15 pm
  9. silverphil
    Posts: 5

    i have a problem! using this tutorial i wrote this in the python console:
    import bge

    def main():
    move = 0.02 #float
    cont = bge.logic.getCurrentController()
    Cube = cont.owner
    scene = bge.logic.getCurrentScene
    sens = cont.sensors['mySensor']
    actu = cont.actuators['myActuator']

    bge.logic.keyboard=keyboard

    if sens.positive:
    cont.activate(actu)
    else:
    cont.deactivate(actu)

    if bge.logic.KX_INPUT_ACTIVE.keyboard_events(bge.events.DKEY) is TRUE :
    Cube.applyMovement((0,move,0),TRUE)

    BUT WHEN I PRESS ALT+P MY PYTHON CONSOLE SAYS THIS :
    Import error: no module named bge

    WHY DOES THIS HAPPEN!!???
    ANY IDEAS ?

    #
    59
    Nov 26, 2012 at 1:27 pm
    • Posts: 454

      It looks like python is not installed correctly

      #
      59.1
      Nov 26, 2012 at 4:36 pm
      • silverphil
        Posts: 5

        how can i fix that ?(i have a windows xp 32 bit sp3 operating system and a slow computer)

        #
        59.1.1
        Nov 29, 2012 at 9:40 am
      • silverphil
        Posts: 5

        how can i fix that ?

        #
        59.1.2
        Nov 29, 2012 at 9:41 am
    • Posts: 454

      Try a build from graphicall.org these have python built in

      #
      59.2
      Nov 29, 2012 at 6:45 pm
      • silverphil
        Posts: 5

        maybe the one “blender 2.64 + BGE components” ?

        #
        59.2.1
        Nov 30, 2012 at 12:15 pm
    • Posts: 5

      There are two different things…

      The bpy commands
      &
      The bge commands

      The bpy commands are ran using Alt+p
      The bge commands are ran using p (in the 3D viewport OR start in the Render tab in the properties viewport)

      bpy commands can ONLY be ran when a game is NOT in progress. (using Alt+p)
      bge commands can ONLY be ran when a game IS in progress. (prompted by an always logic brick linked to a Python Controller)

      This had me confused for a LONG time.

      #
      59.3
      Jan 2, 2013 at 10:08 am
    • Posts: 5

      There are TWO different ways of using python in Blender.
      WITH a game running – when you use bge commands.
      &
      WITHOUT a game running – when you use bpy commands.

      They are started using two different methods:-
      Bpy scripts are ran using Alt+p
      Bge scripts are ran by linking an Always Sensor to a Python Control brick in the Logic Editor.

      #
      59.4
      Jan 2, 2013 at 10:11 am
    • Posts: 2

      There are TWO different ways of using python in Blender.

      WITH a game running – when you use bge commands.
      &
      WITHOUT a game running – when you use bpy commands.

      They are started using two different methods:-
      Bpy scripts are ran using Alt+p (or The run button at the bottom of the TextEditor Viewport)
      Bge scripts are ran by linking an Always Sensor to a Python Control brick in the Logic Editor. They start when you begin the actual game (using either p in the 3D viewport or the START button in the Render Tab of the Properties Viewport *dont forget to change from blender render to blender game*)

      Ive only JUST got my head round it but it makes the little python prompts that popup when you hover over commands make SO much more sense – they are bpy commands and WONT work in bge!

      Hope that helps.

      If Ive gotten that wrong and anyone wants to correct me PLEASE do… It’ll help everyone who reads this thread out. :)

      #
      59.5
      Jan 2, 2013 at 10:33 am
  10. silverphil
    Posts: 5

    sorry the first tutorial

    #
    60
    Nov 26, 2012 at 1:29 pm
  11. Posts: 17

    How would I do it if I were to use a game property that I add through the properties panel?

    #
    61
    Feb 21, 2013 at 9:12 am
  12. Posts: 4

    everything works great but im not certain why i can’t obstacle.color = ## to work in GLSL. only works in multi. is this something i have to live with?? haha

    #
    62
    Apr 19, 2013 at 9:18 pm
  13. Posts: 3

    Please do more on this. Slowly teach on more advanced game scripting. This was the basics, but now teach more intermediate, then advanced.

    #
    63
    May 8, 2013 at 12:43 pm

Leave a Comment

You must be logged in to post a comment.