My first attempt at python scripting

So ive decided to get into scripting and my first thing i tryed doing was just simply by pressng space bar witch runs a script telling the cube to move and the script works when i hit run script in the text editor but if i add some logic so that when space is placed it runs the script called “move” but nothing happens how come?

here is the script

import bpy[‘Player’].location.y +=2

EDIT: when i press play nothing happens but when i hit ESC to exit the game the cube moves afterwords…?

Your script wont work in realtime. The bpy module is not available in the BGE.
you need to import bge, define the object and give it a displacement.
Translated, your script could look like this:
import bge
cont= bge.logic.getCurrentController()
own= cont.owner
own.position= (position[0]+a, position[1], position[2]) # to move the cube on the x axis only.
I haven’t tested this so tell me if it doesn’t work.

That is mostly correct; the bpy module is available only in the bge with blender.
Additionally, you can just transform the position along the certain axis.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

own.worldPosition.x += 2

In addition to torakunsama’s post:

the Python API’s of Blender (BPY) and the Game Engine (BGE) are different. This is because both applications are separate even when you can start the BGE from Blender. Both application have a different architecture and different purpose. The BGE can use data created by Blender.

To continue with BGE Python programming I recommend to use the Blender Game Engine API for reference. You can use the Standalone Modules too but not the Application modules as the are not available in the Blenderplayer.

Some hint:

  • look for KX_GameObject for available attributes
  • do not use KX_GameObject.position as it is deprecated and misleading -> use worldPosition or localPosition

a+= 2 can only be executed if a defined [by assigning a value] already.

ok thanks guys also monster i saw that webpage the blender game engine api but i was looking for a complete series of beggining to end of scripter but in video do you happen to know any good ones?

This are good video tutorials:

It teaches beginner (like me) about working with bge python API.

I recommend to keep the API open next to your text editor, even when following a tutorial. With that you can quickly refer to the mentioned methods and you can read what it is supposed to do.
Finally it makes it much easier for you when you work on your own.

+1 on Monster - You also learn to find stuff in the api docs and get over time a clue about what is there, that is a big win!

That’s right. The tutor in those vids mostly refer to the bge python API documentation before he type the scripts into blender text editor. That’s why he let the documentation page opened until the end of the tutorials :wink:
But he didn’t exactly open the docs page next to the text editor like Monster suggest tough.

Check out my video tutorials:

In the “Tunnel Runner” series, I basically build the whole game from beginning to end, and I use quite a bit of python throughout, so you might find that helpful.

But, that said, it’s not a “Python” tutorial - I assume you know the basics, and I’m just explaining the API.

… I’m actually in the process of making something that’s aimed at complete beginners, but it’s going to take me some time to work everything out.

I have a series of Python tutorials for working with the BGE on my blog, but they’re not videos. I didn’t think that it would be useful (nor was I equipped to make such tutorials at the time that I started) since I would only be recording words. Scroll down to the bottom for the first tutorial in the series.