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.











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
using snap to grid makes all the shards not a cube, sort of jumbled up
for obj in bpy.context.selected objects:
Syntax Error:Invalid Syntax
location::-1
location::-1
How do i fix this?
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.
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.
I found out how to do it. I have to import GameLogic and then use GameLogic.x. Thanks for the help though.
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!
God, i hate blender, but i’m so far in now i can’t stop.
THE PRINT STATEMENT NO-LONGER WORKS. (on mine)
WWWWWWWWWWHHHHHYYYYYYYYYYY??????????????????!!!!!!!!!!!!!!!!!!!??????????????!!
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
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?
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??
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 ?
It looks like python is not installed correctly
how can i fix that ?(i have a windows xp 32 bit sp3 operating system and a slow computer)
how can i fix that ?
Try a build from graphicall.org these have python built in
maybe the one “blender 2.64 + BGE components” ?
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.
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.
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.
sorry the first tutorial
How would I do it if I were to use a game property that I add through the properties panel?
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
Please do more on this. Slowly teach on more advanced game scripting. This was the basics, but now teach more intermediate, then advanced.