Lets start super basic-

Open your blender, and split the screen, making the right side of the screen the python text editor. Now type in the following:

print "hello"

Name this script "starter".. Now select the default plane and give it an always sensor connected to a python controller("starter").
Press P in the 3d window, then hit escape and check out your blender dos window, it should read 'hello' a ton of times.. Now this was just the standard classic print command, its alot more useful than just typing out hackneyed strings.
Erase that line, and type in:

print dir(GameLogic)

press P in the game window, then look at your dos window. dir = directory if you havent made the connection, and GameLogic is all of the python commands for realtime blender, dir prints out most of them. Now youll see a list of GameLogic commands, the mainly used one is "getCurrentController". So now make your line look like this:

print dir(GameLogic.getCurrentController)

Press P and look at your dos window. Youll notice that you can not only print the dir of GameLogic, but all of the subcommands as well. This is very useful when you want to get a directory of commands for actuators and sensors. It will print out the directory like 20+ times because the ALWAYS sensors keeps running the script, so just look at the bottom grouping.


Wondering what getCurrentController means?
It gets the information of any object connected to the python controller, for this example, our default plane is connected to the controller.
What is it useful for?
Well once we have the controller, we can acquire the sensors and actuators and change them. And we can also get the owner object of the controller, and changes its properties, position, rotation, anything. So lets start off with a few more lines, make your script look like:

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

'cont' is just the variable name(you can change it) so we dont have to type out GameLogic.getCurrentController() everytime we need it. And there is a set of parenthesis at the the end this command because theres an error otherwise(lol i never really knew why, but some things need empty parenthesis to work, so just go with it). Anyway, so now 'cont' is the variable that contains all the controller information.
Now, own, that variable gets the OWNer of the python CONTroller, in other words, it gets the object that contains the python controller. Those two lines are the most basic and should appear at the top of most scripts(any that require object or logic brick information).. Also remember, spacing doesnt matter, you can have one space(x = 1) or none (x=1)

For fun, below these lines, type:
print dir(cont)

Press p, look at the dos window. Youll notice its given you some juicy commands to work with. So lets use one. Add a new actuator to the default plane, a motion one, and connect the python controller to it. Rename the actuator to "move". Now in the text editor, below all the lines, type in:

move = cont.getActuator("move")

This line gets the actuator connected to the python controller, and allows us access to play with it. Heres where dir could come in more handy.
below the move line, type:

print dir(move)

Press p, look at dos screen, and notice a nice set of things. Now choose your poison and lets move on, for sake of the tute, ill choose to use DLoc.
Get rid of the print line, and add this new line:

speed = move.getDLoc()[1]

This line gets the Y speed as written in the actuator(which is 0.0 right now).
Whats the "[1]" for? well there are 4 values in the DLoc section of the actuator, x,y,z,and local(little button at the end). Python counts starting from 0, so we want the Y value of
the actuator, which would be 1(ex: x=[0], y=[1], z=[2], and local=[3]). The variable 'speed' produces a list of all the info in the DLoc section of the actuator, if you print dir(speed), it will return the list [0.0,0.0,0.0,1], so using [1] after getdloc would return the 2nd value in a list, which is 0. Anyhoo, lets move on.

What we want, is for our plane to gradually gain speed, and since our script is 'always' running, each time it runs, we want to add a little more speed. SO, below the 'speed' line, type:

speed = speed + 0.001

This adds 0.001 to whatever value speed is. Now we need to update the actuator with the new speed. Type this line below the last one:

move.setDLoc(0.0, speed, 0.0, 1)

Now our Y value will match our speed variable =) One last thing, we need to make this new value active in the game. So below, type:

GameLogic.addActiveActuator(move,1)

This lines adds the actuator 'move' back in, and the 1 is for whether or not to turn the actuator on, or leave it off and just update the value. We'll keep it on.. Now press p, and if all is well, your plane should gradually gain in y speed.


So your final motion script should look like this(remember, everything is case sensitive):

cont=GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getActuator("move")
speed = move.getDLoc()[1]

speed = speed + 0.001
move.setDLoc(0.0, speed, 0.0, 1)
GameLogic.addActiveActuator(move,1)



Now lets get crazy with this script.
Add a new property to the plane, name it "go", and give it value of 0.
Add a keyboard sensor, name it "up" and press the up key in the prompt thing. Connect it to the python controller. Add another keyboard sensor, name it "down" and press down in the prompt thing. Connect that to the python controller as well. Now, third line down, below the 'own' variable, type these two lines:

pressup = cont.getSensor("up")
pressdown = cont.getSensor("down")

These two lines get out keyboard sensors. Now, add these lines below the 'speed' variable(but above the speed = speed+0.001)

if pressup.isPositive():
       own.go = 1
elif pressdown.isPositive():
       own.go = 0

So if the press up sensor is positive(is pressed), it changes the owners property "go" to 1. And if pressing down is true, make the property "go" = 0.

Now we will make motion only happen if go = 1. So, rearrange the bottom of the script to look like this:

if own.go == 1:
       speed = speed + 0.001
       move.setDLoc(0.0, speed, 0.0, 1)
       GameLogic.addActiveActuator(move,1)
else:
       speed = 0
       move.setDLoc(0.0, speed, 0.0, 1)
       GameLogic.addActiveActuator(move,0)

Ok, some explaining, we'll start with the top. If the property go is equal to 1, then execute the script regularly, adding speed and moving our plane, ELSE if go does not equal 1, set the speed back to zero and turn the actuator off.


So heres what the REAL final script looks like:

cont=GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getActuator("move")
speed = move.getDLoc()[1]

if pressup.isPositive():
       own.go = 1
elif pressdown.isPositive():
       own.go = 0

if own.go == 1:
       speed = speed + 0.001
       move.setDLoc(0.0, speed, 0.0, 1)
       GameLogic.addActiveActuator(move,1)
else:
       speed = 0
       move.setDLoc(0.0, speed, 0.0, 1)
       GameLogic.addActiveActuator(move,0)





One last simple run down. The first 4 lines set our variables needed. The next two 'if' statements determine whether 'go' will equal 0 or 1, and the next two statements turn motion on if go = 1, and reset motion to 0(and turn if off) if go = 0.

Here is some more basic info to remember:

the list-
lists are used alot. Its simply imformation, or variables held together by the square brackets. For example, getDLoc produces a list like [0, 0, 0, 1]. For more information on lists, read up on the tutorial docs that come with python.

booleans(if, elif, else, while)-
~the 'if' statements is pretty simple. IF this, then do this..
~elif if is used after an if statement, it means (if not that, but this, do this) ex: if not pressing up, but pressing down, own.go = 1. You can have multiple elifs following an if stament as well. Read up on the python docs for more info
~else simply means (if all else fails, do this)
~while is used for soemthing like: while own.go ==1: value = value + 1.. as long as own.go is equal to 1, it will always add one to the value 'value'. using the 'while' boolean never works for me though, always freezes blender so stear clear

WHY are there two == signs? well, booleans have different comparison characters:
'==' means "equal to"
'>=' means "greater than or equal to
'>' means "great than"
'<=' means "less than or equal to
'<' means "less than"
'!=' means "does not equal"
'and' is used between comparisons, like 'if this > that and this <= theother: do this'
'or' is used between comparisons too, like 'if this > that or this > theother: do this'

*remember, everything is case sensitive, and dont forget to put colons(:) at the end of your boolean statements, and be sure to tab in the next lines as well


-blengine