Python question, What do Def() Statements mean?

Here is an example of my player movement/Click to walk script on my player

import bge
from bge import logic
from bge import events
cont = logic.getCurrentController()
key = logic.keyboard.events
obj = cont.owner
moveto = cont.actuators["moveto"]
track = cont.actuators["track"]
movetrack = cont.actuators["movetrack"]
near = cont.sensors["near"]
 
#####PLAYER MOVEMENT AND WALKING####
 
###SPEEDS FOR VERTICAL AND DIAGNOLS###
xx = .050  #vert horz
xy = .035  #diag
rs = .055  #rotate speed
##UP##
if key[bge.events.WKEY]==2 and not key[bge.events.AKEY]==2 and not key[bge.events.DKEY]==2:
    obj.applyMovement([0,xx,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##UPLEFT##
if key[bge.events.WKEY]==2 and key[bge.events.AKEY]==2:
    obj.applyMovement([-xy,xy,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##UPRIGHT##
if key[bge.events.WKEY]==2 and key[bge.events.DKEY]==2:
    obj.applyMovement([xy,xy,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
 
##STRAFELEFT
if key[bge.events.AKEY]==2 and not key[bge.events.WKEY]==2 and not key[bge.events.SKEY]==2:
    obj.applyMovement([-xx,0,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##STRAFERIGHT
if key[bge.events.DKEY]==2 and not key[bge.events.WKEY]==2 and not key[bge.events.SKEY]==2:
    obj.applyMovement([xx,0,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##DOWN##
if key[bge.events.SKEY]==2 and not key[bge.events.AKEY]==2 and not key[bge.events.DKEY]==2:
    obj.applyMovement([0,-xx,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##DOWNLEFT##
if key[bge.events.SKEY]==2 and key[bge.events.AKEY]==2:
    obj.applyMovement([-xy,-xy,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##DOWNRIGHT##
if key[bge.events.SKEY]==2 and key[bge.events.DKEY]==2:
    obj.applyMovement([xy,-xy,0],1)
    cont.deactivate(moveto)
    cont.deactivate(track)
    cont.activate(movetrack)
##ROTATION HERE###
 
if key[bge.events.QKEY]==2:
    cont.deactivate(movetrack)
    cont.deactivate(track)
    obj.applyRotation([0, 0,rs],1)
if key[bge.events.EKEY]==2:
    cont.deactivate(movetrack)
    cont.deactivate(track)
    obj.applyRotation([0, 0,-rs],1)
 
 
 
#####ACTIVATES WALKING TO POINTER
 
if logic.globalDict["clickwalk"] == True:
    cont.activate(track)
    cont.activate(moveto)
###DEACTIVATES IF NEAR SENSOR IS POSITIVE
if near.positive:
 cont.deactivate(moveto)  
logic.globalDict["clickwalk"] = False
cont.deactivate(movetrack)  

as you can see its all basically:

if this
___do this

When im looking through scripts such as one of the most popluar mouselook scripts they have lines like this
Def(something)
_____thisthis

For some reason this scripts are hard to understand becase i dont really know what the Def()'s do. I know they run but I dont know how. When I try to pick apart the scripts theyll stop working. Today I was thinking about my above script and thought maybe def worked like this.

Instead of having all the end tracking/moving actuators per each movment key press in the script. Maybe Def() worked like this:

def(deactivate)
_____controller.deactivate.(moveto), controller.deactivate.(trackto)

and then put

if walk key
___(deactivate)

am i starting to understand or am I way off. Is there a simple example of a script with Def(that I can understand)? I just want to know how to use it if i Will need to know it, because alot of example scripts use Def()

You are close…

The def command defines a function.

Say you have the math operation:
Y = X + 10
and you had to do it a dozen times with different numbers in different places throughout a script then you could make a function
It would be like:


def addTen(num):
    newNum = num + 10
    return newNum

And then to use it you would use:

value = addTen(5)

and printing “value” would give you 15

That is a really basic operation, and I wouldn’t make a function to do that, but if you had some complex operation that needs to be done repeatedly, say for an AI script, you could create a larger function. An example function that I wrote is:


def getAngleBetween(source, target, axis):
    '''Get's the angle between source and target'''
    dis, gvec, lvec = source.getVectTo(target)
    angle = lvec.angle(mathutils.Vector(axis),90)
    angleDeg = math.degrees(angle)
    return angleDeg

To use that function you have to specify 3 different things, so a use may be:


angle = getAngleBetween(player, enemy, [0,1,0])

The first code I wrote with functions was similar to yours, and manages movement.
I created 4 functions called moveForward(), moveBackward(), moveLeft(), moveRight() and then used if/then statements like:


if key[bge.events.WKEY]==2:
    moveForward()

Functions must be defined before they are called (physically above in the script), unless you call it from another function (as in a function that choses the best enemy to attack might use the angle function)
Functions can also be seporated into “Classes” but I won’t go into detail about those, it is just a way of organizing a lot of functions that are similar.

I hope that cleared some things up. If you want some more help just ask or drop me a PM

1 Like

sdfgeoff hit it perfectly. Defining custom functions also allows you to further block out your code into more readable sections, particularly when you’re using external text editors like Geany, NinjaIDE, Notepad++, etc., as most editors can visually collapse defined functions or if-statements to allow you to visually ‘bypass’ sections. For example, you could collapse the Player’s function to get an easier look at a function below the Player’s.

Sorry for off topic.
If I has 1.py and 2.py so I want 1.py to use function in 2.py can I do this and how.
I’m just beginner with python.
sorry again for off topic

Save 2.py in a text file in the same folder as the blend file and then:

from 2 import [function name]
or
from 2 import *
or
import 2
and then
2.[funtion name]

Ahhhh, do not use numbers as names!

To learn the basics of Python I recommend to read byteofPython first. It contains a lot of explanations.

A Byte of Python Swaroop C H?
that name is for example haha.
thank you @sdfgeoff

Exactly that :slight_smile:

Gooooood.
Please don’t shock an old Monster like me :smiley:
Classic example names are foo and bar. (I was always wondering what the mean :wink: ).

Monster

Ah foo and bar prof. at my university use it too.
(And I don’t know what it mean too)

FUBAR = Fouled Up Beyond All Recognition

The first word is often substituted with an alternative that is not suitable for a reputable forum such as this. :wink:

I think foo bar is a humourous play on this acronym.

The Wikipedia has an interesting article regarding Foobar and Metasyntactic Variables. I like the German Version of the last one (it is not 08/15 :slight_smile: ).

thanks for the detail sdfgeoff. :smiley:

looks like something im going to have to dive into with test to learn well.