Is this a 2.41 Bug? Joystick script

Hi all

I’got a nice 4 axis jaystick for my birthday, and am writing this script to control moving or walking through a landscape.
All is well until I upgrade from blender 2.37 to 2.41

The script worked (and still does) in 2.37, but it does not in 2.41
I get the following error:
File “”, line68, in ?
NameError: name ‘jfmedge’ is not defined

It looks to me like 2.41 doesn’t remember the defined variables from earlier frames like 2.37 did.
Did anyone experience anything simmilar?
should I report it as a bug? (or is this a design choice?)

to use the script, create an object (or an empty), make it an actor, and create all the needed sensors(see the script), connect them to a python controller(with the script below loaded) and connect that to an actuator called move.

in 2.37 add a timer property called MyTime
in 2.41 rip out the if statement with the MyTime property check
connect a camera if you like and off you go!

any suggestions and comments are welcome
It’s not neat code (sorry I’m a mechanical engineer)
but I have fun with it, I hope you will to

Grts Smitje

sorry for the long listing

#joy 0.1 joystic walk-around
#Smitje 22-01-2006

moving an object around using 3 axis of the joystick
forward           move
sideways          move
twist handle      turn

it uses separate upper and lower threshholds j##edge in each direction
and linearly interpolates between a low and high movement rate between them

import Blender
contr = GameLogic.getCurrentController()
owner = contr.getOwner()
move = contr.getActuator('move')

if owner.MyTime < 0.05:
	# initiation (somewhat crude) try tweaking the time if it doesnt work
	print" "
	print" "
	print"Joy 0.1"

	jturnslow = 0.001      #slow turning speed
	jturnfast = 0.05       #fast turning speed
	jstedge = 5000         # joystick position at which slow turning starts
	jftedge = 30000        #         "           "      fast turning starts
	jturnrico = ((jturnfast - jturnslow) / (jftedge - jstedge)) # used in " (x - stedge)*rico + turnslow"

	jmoveslow = 5          #slow moving force
	jmovefast = 10         #fast moving force
	jsmedge = 5000         # joystick position at which slow moving starts
	jfmedge = 30000        #         "           "      fast moving starts
	jmoverico = ((jmovefast - jmoveslow) / (jfmedge - jsmedge)) # used in " (x - mvedge)*rico + moveslow"

	jsideslow = 5          #slow side force (for movinr sideways)
	jsidefast = 10         #fast moving force
	jssedge = 5000         # joystick position at which slow moving starts
	jfsedge = 30000        #         "           "      fast moving starts
	jsiderico = ((jsidefast - jsideslow) / (jfsedge - jssedge)) # used in " (x - sideedge)*rico + sideslow"

	joystick = contr.getSensor('stick1')
	jSide = joystick.getAxisValue()[0]    # + =right     - =left
	jFront = joystick.getAxisValue()[1]   # + =back      - =front
	jPower = joystick.getAxisValue()[2]   # + =back      - =front
	jTwist = joystick.getAxisValue()[3]   # + =right     - =left

	jb1 = contr.getSensor('jb1')
	jb2 = contr.getSensor('jb2')
	jb3 = contr.getSensor('jb3')

	hatUp= contr.getSensor('hatUp')
	hatDn= contr.getSensor('hatDn')
	hatLe= contr.getSensor('hatLe')
	hatRi= contr.getSensor('hatRi')	

	tempRot = [0,0,0,1]
	tempForce = [0,0,0,1]

	#move forward/backward
	if jFront < -jfmedge:
		#print " move forward fast"
		tempForce[1] = ( -jmovefast )
	elif jFront < -jsmedge:
		#print " move slow"
		tempForce[1] = (-((-jFront-jsmedge)*jmoverico+jmoveslow))
	elif jFront > jfmedge:
		#print " move back fast"
		tempForce[1] = ( jmovefast )
	elif jFront > jsmedge:
		#print " move back slow"
		tempForce[1] = ((jFront-jsmedge)*jmoverico+jmoveslow)

	#move sideways
	if jSide < -jfsedge:
		#print "move left fast"
		tempForce[0] = ( jsidefast )
	elif jSide < -jssedge:
		#print "move left slow"
		tempForce[0] = (((-jSide -jssedge)*jsiderico+jsideslow))
	elif jSide > jfsedge:
		#print "move right fast"
		tempForce[0] = ( -jsidefast )
	elif jSide > jssedge:
		#print "move right slow"
		tempForce[0] = (-((jSide-jssedge)*jsiderico+jsideslow))

	if jTwist < -jftedge:
		#print "turn left fast"
		tempRot[2] = ( -jturnfast )
	elif jTwist < -jstedge:
		#print "turn left slow"
		tempRot[2] = (-((-jTwist-jstedge)*jturnrico+jturnslow))
	elif jTwist > jftedge:
		#print "turn right fast"
		tempRot[2] = ( jturnfast )
	elif jTwist > jstedge:
		#print "turn richt slow"
		tempRot[2] = ((jTwist-jstedge)*jturnrico+jturnslow)

	if jb1.isPositive():
		print "joystick "
		print "side ",jSide,", front ", jFront,", power ", jPower,", twist ", jTwist

	if hatUp.isPositive():
		print "hat up"
	if hatDn.isPositive():
		print "hat down"
	if hatLe.isPositive():
		print "hat left"
	if hatRi.isPositive():
		print "hat right"
#	if jb3.isPositive():
#		print "jstedge", jstedge
#		jstedge = 5000


GameLogic.addActiveActuator(move, 1)

Blender.parentPos = owner.getPosition()
Blender.parentRot = owner.getOrientation()

hmmm… not sure, but by looking at ur scrip, if the first “IF” statement fails, jfmedge is not defined, and the “ELSE” statment compares a variable with jfmedge which has not yet been defined??

2.41 is more strict on the lifetime of variables see


This is -exactly- what I’ve been hoping for! I haven’t tried it yet, but hope to incorporate it into a project I’m working on in 2.41.

I haven’t studied the code yet (I’m not a programmer either), but how difficult would it be to add a “move backwards” function?

Nice work! Thanks!

Thanks for the replies!

Monster for the other thread, At least I know more people have spotetd the change in behaviour.

bigkahuna for your enthusiasm,
you can move backwards: pull the joystick towards you
twist to change direction and push sideways to … move sideways
if you dont have twist it shouldn’t be to hard to change the code for a simpler joystick.

I’m planning to add camera control to the hat so you can look around while moving, but I’m allso working on other things so I don’t know when it will apear.

cheers Smitje