Having a problem with gears

Im trying to implement a gear system into the vehicle wrapper. I have the gear display working fine and i thought the code looked fine also but it isnt. Can someone have a look and see if they can tell me where im going wrong? this is my first attempt at a snippet of code. Cheers


Edit: This is what i put into the keyboard script:

if (gear == 1) and gasPedal.isPositive() == True:
power = -powertrain.EnginePower
if (gear == 2) and gasPedal.isPositive() == True:
power = -powertrain.EnginePower2
if (gear == 3) and gasPedal.isPositive() == True:
power = -powertrain.EnginePower3
if (gear == 4) and gasPedal.isPositive() == True:
power = -powertrain.EnginePower4
if (gear == 5) and gasPedal.isPositive() == True:
power = -powertrain.EnginePower5

As appose to what it was:

if gasPedal.isPositive() == True
power = -powertrain.EnginePower

And i have the relevant properties assigned.

Seems like the gear variable isn’t defined anywhere before the if statements (in the full script not the code snippet before you ask). Maybe thats the problem.

I can’t touch your code either, but are your power = lines indented correctly in the real code?

So how would i identify gear? At the minute it is a property on the car. Also i didnt know indentation mattered(it doesnt on any other code i know) how much indentation should there be?

Here is the code if you need it:


Part of a car setup

Instructions on how to use it at



get the current controller

controller = GameLogic.getCurrentController()

get list of objects in scene

objList = GameLogic.getCurrentScene().getObjectList()

get manuals

keyboard = controller.getOwner()
setup = objList[“OB” + keyboard.CarSetup]
joystick = objList[“OB” + setup.Joystick]
steering = objList[“OB” + setup.Steering]
powertrain = objList[“OB” + setup.Powertrain]`
gear = objList[“OB” + player.Gear]

Keyboard control enabled?

if keyboard.KeyBoard == True and joystick.JoyStick == False:

############# Steering

get sensor named Left

steerLeft = controller.getSensor(keyboard.Left)

get senor named Right

steerRight = controller.getSensor(keyboard.Right)

turn left/right/straight

if steerRight.isPositive() == True:

turn = -steering.MaxTurn

elif steerLeft.isPositive() == True:

turn = steering.MaxTurn


turn = 0.0

save to steering manual

steering.SteeringWheel = turn

################ Gas – Brakes – Reverse

get the sensor named Gas

gasPedal = controller.getSensor(keyboard.Gas)

get sensor named Brake

brakeReverse = controller.getSensor(keyboard.Brakes)

get sensor named Emergency

eBrake = controller.getSensor(keyboard.EBrake)

gas pedal

if (gear == 1) and gasPedal.isPositive() == True:

power = -powertrain.EnginePower

if (gear == 2) and gasPedal.isPositive() == True:

power = -powertrain.EnginePower2

if (gear == 3) and gasPedal.isPositive() == True:

power = -powertrain.EnginePower3

if (gear == 4) and gasPedal.isPositive() == True:

power = -powertrain.EnginePower4

if (gear == 5) and gasPedal.isPositive() == True:

power = -powertrain.EnginePower5

brakes = 0.0

brake and reverse

elif brakeReverse.isPositive() == True:


if powertrain.Speed <= 0.0:
power = powertrain.ReversePower
brakes = 0.0


power = 0.0
brakes = powertrain.BrakeForce

no gas or brakes

power = 0.0
brakes = 0.0

emergency brake

if eBrake.isPositive() == True:

power = 0.0

ebrake = powertrain.EBrakeForce

ebrake = 0.0

save to powertrain manual

powertrain.Power = power
powertrain.Brakes = brakes
powertrain.Emergency = ebrake

player(at the top of the code) has been defined as the collision box using a property, as is done with other obj in the script.

hi doddsey_65,

First, I like the way you modified the car. It looks really sharp with the spoiler and new door panels. :yes:

Second, I’m in the process of updating that tutorial. It’s the last one on my site hasn’t been updated.

Third and most important. Gears.

To change gears you don’t need to change the engine power. What you need to do is change the maximum speed the car can go. 1st gear would have a max speed of about 20 mph. 2nd would be approximately 35 mph. etc.

I removed your code from the keyboard.py and added it to the powertrain.py That way it will work with keyboard or joystick. MPH or KPH. Switched it from changing the engine power to setting a max speed for each gear.

Take a look at the blend and if you have any questions don’t hesitate to ask.


cargamenew_gears.blend (469 KB)

cheers mate it works perfectly, i can get on with the rest now lol. So i didnt need to define gears? i am just learning python but with knowing many other codes i thought it wouldnt be too hard. Its not too different from RGSS. Also does indentation matter? In other codes its just used to help the coder. Cheers for the comment about the model. Its a bit messy at the min but i wanted to get the actual physics sorted before going into more detail with the game. Again cheers mate!

EDIT: I do have a few questions if you can help. When pulling away in 5th the car accelerates normally but it should accelerate slowly till you get the relevent revolutions. And thats the other question. I will be able to get a working speedometer displayed but what about a tachometer? I would need to let people see when to change gear if i use gears lol.

hi doddsey_65,

the gear property is defined. (I removed it from the collision box where you had it and added it at the bottom of the Powertrain manual.) Look at the code I added in powertrain.py and you’ll see

gear = powertrain.gear

In python, indentation does matter. (I use the Tab key to indent.)

Acceleration when you start in the wrong gear. That takes more code and little bit of math.

Say you are in 5th gear. You don’t want normal acceleration until you hit 40 mph. So if the speed is less than 40 mph, you’d cut the engine power. The way you’d do that is check the speed. Divide it by the speed you want full acceleration. Multiply the engine power by that percentage. Multiply that by whatever math you want.

One way to think of a tach is as a speedometer with different speed values for different gears. 1st gear: the half way point might be 10 mph and max might be 20 mph. 2nd gear: half way point might be 20 mph and max 40 mph. etc.


so…i was wrong with what i thought then lol. I have this and thought it may have worked(it doesnt):

if (gear == 5) and (speed == 0.0):
power = powertrain.EnginePower2

Where enginepower2 has been defined as 100.00
So this is gonna be alot harder than i thought.

Edit: So how would i go about this method? I have tried but dont even know where to start.
Also there is something that i have noticed, when i am in first gear and dont shift up it stays at
about 24mph which is what i wanted, but the steering is severely limited when i hold this speed.

hi doddsey_65,

Think of it like this. In a real car, the max power of the engine doesn’t change. It’s the power delivered to the wheels that changes.

In the blend I attached–in Car_1_Handling.py–look at the Engine section. You’ll see the code that I added to change the amount of power sent to the wheels. It’s based on the gear max speed we added last blend (which tells the gear you’re in) and the car speed.

Realize that this is simple code. You’ll have to tweak it to get the results you want.

About the steering. Select the Steering manual. Look at the Properties. You’ll see Speed Adjust. You can adjust the steering sensitivity.



cargamenew_gears2.blend (470 KB)

Cheers I appreciate everything youve done for me, i have it working now aswell as the speedometer,but struggling with the tachometer. I originally used the set speed script and edited it like this:

objList = GameLogic.getCurrentScene().getObjectList()
car = objList[“OBPowertrain_1”]
cont = GameLogic.getCurrentController()
own = cont.getOwner()
#the text property of this
#object equals the speed property of the car
own.Text = car.Speed / 4

But as you can tell there are problems with this, namely the higher the gear the lower the revs will be, and the fact that it wont reset to 0 when changing gear, it will just go up.

hi doddsey_65,

If you’ve got the speedometer working, you only have to make a couple of minor changes to create a tach.

Use the gear max speed that we created and set a gear min speed. Get the gear speed range, get the car speed, do the math and get the percentage of where the car speed falls inside the gear speed range. Multiply that with the tach range.

In other words. 1st gear.
If min gear speed is 0 and max gear speed is 20 mph. gear speed range is 20 mph. If tach idle is 600 rpm and tach redline is 6600 rpm. tach range is 6000 rpm.

At 10 mph, tach would be 50% or 3600 rpm.

(10 mph minus gear min speed of 0 mph equals 10 mph. Difference of 10 mph divided by gear speed range of 20 mph is 50%. 50% times tach range of 6000 rpm is 3000 rpm. 3000 rpm plus the idle speed of 600 is 3600 rpm.)

2nd gear.
min gear speed is 15 mph. max gear speed it 45 mph. gear speed range is 30 mph. At 30 mph, tach would be at 50% or 3600 rpm. At 45 mph, tach would be 100% or 6600 rpm



Okay sorry but youve lost me now. Is there anyway you could make that simpler? And tanks again for your work. You will ofcourse be credited in my game not just for the wrapper but for this aswell

hi doddsey_65,

I don’t know how else to explain it.:frowning: