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?
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.
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