Python help for a beginner

ok, i thought i should start python as of this week because my brother managed to start python but is haveing the same trouble we both made this code, what is wrong with it.

cont = GameLogic.getCurrentController ()
own = cont.getOwner()

if the Wkey is pressed

forward = cont.getSensor[“W”]
motion = cont.getActuator(“motion”)
speed = 1

Firstly, is this 2.49 or 2.57

Looking at the script it looks 2.49
Several things:
Don’t bother learning the 2.49 python, as it changed in 2.57

I suspect you are actually trying to use a 2.49 script in 2.57, and that is one reason it is not working.

Anyway I will go an with how to do it in 2.57:

In 2.57 it is very different. The first two lines are replaced with:

import bge    #Tells blender it is a script for the game engine

cont = bge.logic.getCurrentController()    #Replaces the cont line
own = cont.owner    #Replaces the Own Line

your next three lines of actual code don’t make much sense.

While you would have seen no errors, this is because you define everything, but don’t tell it what to do.
So what you wrote actually says forward is this sensor.
You never actually tell it to do something with it.

The reason for this is that you have to define everything before you can use it.

To define things you go:

forward = cont.sensors["W"]
motion = actuators["motion"]
speed = 1

You also need to divide up the actuator so that python knows what’s what:

linX, linY, linZ = lin_Speed

Then set all of them to 0 by default:

linX = 0
linY = 0
linZ = 0

So you got that nearly right, just the change in python from 2.49 caught you out.

Now that you have defined everything you have to tell it what to do with these things.

So you can go:

if W == true:
    linY = Speed

You also need to define how much force the actuator is going to be able to add:

servo.forceLimitX = [-100,100,True]
servo.forceLimitY = [-100,100,True]
servo.forceLimitZ = [-100,100,True]

Now is another part you missed, activating the motion.
A simple enough line:

lin_Speed = linX, linY, linZ
motion.linV = lin_Speed
cont.activate(motion)

you just put the three values back into the actuator

For this to work you will need the actuator to be of type “servo motion”

Now if I have got everything right it should work.
Feel free to ask questions it I didn’t explain enough

sdfg is correct that you’re probably writing 2.49 script with Blender 2.5. The only two lines you need to fix are the first two - you can look at the console to see any errors or mistakes, by the way. The console doesn’t appear by default - to enable it, select the Console menu choice in the Help menu.

how about this :

import Bge module
cont = bge.logic.getCurrentController() #Replaces the cont line
own = cont.owner #Replaces the Own Line

if the Wkey is pressed

forward = cont.Sensors[“W”]
motion = actuators[“motion”]
speed = 1

linX = 1
linY = 0
linZ = 0

nothing happens

import bge
cont = bge.logic.getCurrentController()    #Replaces the cont line
own = cont.owner    #Replaces the Own Line
# if the Wkey is pressed
forward = cont.sensors["W"]
motion = cont.actuators["motion"]
speed = 8
linX = 0
linY = 0
linZ = 0


if forward.positive:
    linY = speed
else:
    linY =  0  
lin_Speed = linX, linY, linZ
motion.linV = lin_Speed
cont.activate(motion)
print(motion.linV)

agoose77 is correct with his code - it should work. Again, though, if you’re having errors, you should look at the console - it will tell you where the errors are.