i am learning python and i just did a tutorial this is it here
http://blendenzo.com/tutBeginningBGEPython.html
i have written a script. i trying to make a cube move around just using python script. i have 4 sensors and 1 actuator and their linked to the python script but the console keeps saying that there is an error in line 9 but i dont see what is wrong with it. here is my script
cont = GameLogic.getCurrentController()
own = cont.getOwner()
#Sensors:
forward = cont.getSensor(“Up”)
backward = cont.getSensor(“Down”)
turnleft = cont.getSensors(“Left”)
turnright = cont.getSensors(“Right”)
#actucators
motion = cont.getActuator(“motion”)
#process
speed = 5
rspeed = 0.02
walk = 0
turn = 0
if forward.isPositive():
walk = speed
if backward.isPositive():
walk = -speed
if turnleft.isPositive():
walk = rspeed
if turnright.isPositive():
walk = -rspeed
motion.setLinearVelocity(0, walk, 0, 1)
motion.setDRot(0, 0, turn, 1)
GameLogic.addActiveActuator(motion, 1)
Monster
(Monster)
January 10, 2011, 11:14pm
2
If you surround it with
tags we could read the code much better ;).
Also the error message can be helpful. Usually it is already showing the problem.
BTW.: getSensors() and getActuators() are deprecated. You shouldn't use them. Check the [API](http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.SCA_PythonController-class.html) for more information.
oh ok then what would you recommend i use instead of getsensors() and getactuators()
could you give an egsample of what that would look like in the code.
oh and do you know any good python tutorials for noobs like me.
Monster
(Monster)
January 11, 2011, 11:07pm
6
hm, it is an example already
forward = cont.sensors["Up"]
For Python I suggest byteOfPython.pdf.
Do a search. The question for Python tutorials comes up every month. The Game Engine Resources forum contains some tutorials at the stickies too :).
#awesome thanks
#hehe i like learning python code
agoose77
(agoose77)
January 12, 2011, 12:39am
8
Most of your code is deprecated. It’s not hard to change, look at the backwards compatible in my Sig
whats your sig and now the console is saying that my sensors have no attrubute sensor
agoose77
(agoose77)
January 12, 2011, 10:32am
10
awesome thanks agoose for all your awesomeness
there were one problem i had in my code
if forward.positive
walk = speed
if backward.positive
walk = -speed
if turnleft.positive
walk = rspeed
if turnright.positive
walk = -rspeed
it says invalid syntax error
agoose77
(agoose77)
January 13, 2011, 11:06am
12
have you indented the text after walk?
e.g
if walk.positive:
speed = 5.1
if backwalk.positive:
speed = -5.1
im not sure what i did. i just started change parts of the code that was deprecated
Monster
(Monster)
January 14, 2011, 1:19am
14
you are missing “:” after the condition of “if”
e.g.:
if blah == blah :
smallTalk()
that is the rest of my code that i changed but im not sure itsright
cont = GameLogic.getCurrentController()
own = cont.owner()
#Sensors:
forward = cont.sensors[“Up”]
backward = cont.sensors[“Down”]
turnleft = cont.sensors[“Left”]
turnright = cont.sensors[“Right”]
#actucators
motion = cont.getActuator(“motion”)
#process
speed = 5
rspeed = 0.02
walk = 0
turn = 0
if walk.positive:
speed = 5.1
if backwalk.positive:
speed = -5.1
if walkleft.positive:
speed = 2.1
if walkright.positive:
speed = -2.1
motion.setLinearVelocity(0, walk, 0, 1)
motion.setDRot(0, 0, turn, 1)
GameLogic.controller.activate()(motion, 1)
Monster
(Monster)
January 14, 2011, 2:50am
16
Better use
rather than [quote] tags ;).
Your last statements has to much (). The console should tell you that :D.
I wonder why <i>turn</i> never changes.
agoose77
(agoose77)
January 14, 2011, 12:24pm
17
cont = GameLogic.getCurrentController()
own = cont.owner
#Sensors:
forward = cont.sensors["Up"]
backward = cont.sensors["Down"]
turnleft = cont.sensors["Left"]
turnright = cont.sensors["Right"]
#actucators
motion = cont.actuators["motion"]
#process
speed = 5
rspeed = 0.02
walk = 0
turn = 0
if walk.positive:
speed = 5.1
if backwalk.positive:
speed = -5.1
if walkleft.positive:
speed = 2.1
if walkright.positive:
speed = -2.1
motion.linV[0.0, walk, 0.0]
motion.dRot[0.0, 0.0, turn]
controller.activate(motion)
awesome your code works awesomely