Python - updating code, can not import bge

Hello everyone,
I recently started learning python, and i now want to apply it to blender.
I looked for tutorials, but everyone i found was using depreciated code.

I have attempted to update this code (from a SageDread video on youtube).

It is now telling me:

Python script error - object 'player', controller 'code#CONTR#1':
Traceback (most recent call last):
  File "movement.py", line 3, in <module>
    cont = bge.getCurrentController()
NameError: name 'bge' is not defined

How can i fix it?

(my current code is below)

from bge import logic

cont = bge.logic.getCurrentController()

own = cont.owner

#declare sensors

forward = cont.sensors["forward"]
left = cont.sensors["left"]
right = cont.sensors["right"]
backward = cont.sensor["backward"]

#declare actuaters

move = cont.actuators["movement"]

#declare integer variables for movement

FB = 0
LR = 0
speed = .05

#declare events
if forward.positive():
    FB = speed
elif backward.positive():
    FB = -speed
if left.positive():
    LR = speed
elif right.positive():
    LR = -speed
move.setDLoc(FB,0.0,0.0,True)
move.setDRot(0.0,0.0,LR,True)
controller.activate(move)

Also, are their and good beginners tutorials focusing on the new API?

Regards,

  • Grefuntor

you might want to search around in the game engine forum, and read : http://www.blender.org/documentation/blender_python_api_2_57_release/#game-engine-modules

for python specific things, it helps to know the standard library which you will find on python.org in documentation. (v 3.2)

maybe follow moguri’s blog about BGE : https://mogurijin.wordpress.com/2011/05/27/gsoc-bge-animation-status-report-week-1/

The docs gave me some keywords to google, and after a very long time i finally got it going!

Thank you Zeffii.

Can you give a brief summary of what you googled and a few words of advice to help others in the same position.

Yes,

From the error messages i was getting (e.g GameLogic/bge not defined, "AttributeError: ‘KX_ObjectActuator’ object has no attribute ‘setDLoc’ ").
I picked out the key words, (like GameLogic, BGE, KX_ObjectActuator) and googled them, and a bit of the error.
It was really a matter of working through errors.

Everytime it said something isn’t right I had to go through the script finding the parts causing the trouble, i then googled the error, and checked through the docs to make sure the code i am trying is not depreciated by 2.5x.
I found a few things like http://mogurijin.wordpress.com/2010/08/17/how-to-update-blender-2-49-bge-scripts-to-2-5x/ , http://www.blender.org/documentation/blender_python_api_2_57_release/change_log.html
which didn’t mention my problems but they did give me an idea of what the changes i needed are.

Change logs are a great way to find out if your code is depreciated.

So in summary, pick out the key words to google and then work through your code, keep running it, and keep googling the errors and fixing them.

Now onto my next challenge, adding jumping!

################################
# By Grefuntor.
#this script requires an object with dynamic/rigid physics. 
#they need the controlls "forward","left","right" and "back"
#a controller with the attached script,
#and an actuator with the name "movement"
#(also turn up the friction in the materials pane to stop sliding.)
###############################
#from bge import logic
import GameLogic
import bge

cont = bge.logic.getCurrentController()

own = cont.owner

#declare sensors

forward = cont.sensors["forward"]
left = cont.sensors["left"]
right = cont.sensors["right"]
backward = cont.sensors["back"]


#declare actuaters

move = cont.actuators["movement"]

#declare integer variables for movement

FB = 0
LR = 0
speed = 10
#declare events
if forward.positive:
    FB = - speed
if backward.positive:
    FB = speed
if left.positive:
    LR = speed
if right.positive:
    LR = -speed

#print("working",FB,LR) -- debug to test button detection
move.force = [FB, 0.0, 0.0] #move according to FB
move.torque = [0.0, 0.0,LR] #rotate according to LR
cont.activate(move) #activate it

-my working code, incase anyone wants it.

-Grefuntor