Manual Transmission for GTA IV
Posted by John Fineral on Friday, August 26, 2011
Under: Coding
8/25/2011 10:01 PM
I got bored yesterday and decided to start working on a mod for GTA IV that would create a manual transmission for the car. Only problem was that I had no idea about how to code for GTA IV. Luckily I had a decent understanding of C++ and the jump from C++ to C# was pretty seamless.
Here's the guide I used to learn the basics: http://www.gtaforums.com/index.php?showtopic=401577
I'll add a link to the source code later...
The mod works by checking every 10 milliseconds for gear switching, showing the gear the user is in, and setting the speed of the car.
I'll start with the gear change handler:
The code works by first checking if the user has the clutch enabled or not. The user can change this setting by setting the value of the clutch to false in the .ini file. If the clutch is enabled, it checks if the button for the clutch is being pressed. If the button is not being pressed the gear will not change. The actual gear switching is just adding and subtracting from a variable. There is a max gear of 6 and a minimum gear of 1. If the user is pressing the clutch, and the gear up button, then the car will shift into the next highest gear. I added an if statement to check if the gear is equal to 6. If this is true (the gear is equal to 6) The user will not be able to shift higher even if they are pressing the gear up button. It's the same concept for the gear down button but instead of 6 the gear must be higher than 1 to shift down.
Next, Showing what gear the user is in:
This was the easiest bit of code to write. It starts by checking to see if the user is in a vehicle. If the user is in a vehicle it draws the string ["Current Gear: "+gearState] in the upper right corner of the screen. If the statement is false nothing is shown.
Finally, setting the speed of the car:
The hardest piece of code to write by far. Even now it isn't perfected. What I have so far starts by declaring a vehicle variable "V" and setting it equal to the vehicle the player is in. Then it starts a very long if statement. First it checks to see if the player is in a car. Next it starts a switch statement based on the variable of the gear. In the case of the user being in gear one, the script checks if the car exists (to avoid errors) and if the car is not running. If both of those requirements are met, then the car is told to start. In a separate If statement, the script checks to see if the vehicle exists, as well as if the car is at or above 30MPH. If that is met, The car is set to go at 30MPH until the user shifts up or slows down. Gears 2-6 works the same but cap at higher speeds. Gears 2-6 also do not check if the engine is running. Instead they have an if statement that checks to make sure that the user is not trying to accelerate from 0MPH in a gear higher than 1. When the player leaves the car the gear is set back to 1 for the next car.
That's a big enough wall of text for now.
I got bored yesterday and decided to start working on a mod for GTA IV that would create a manual transmission for the car. Only problem was that I had no idea about how to code for GTA IV. Luckily I had a decent understanding of C++ and the jump from C++ to C# was pretty seamless.
Here's the guide I used to learn the basics: http://www.gtaforums.com/index.php?showtopic=401577
I'll add a link to the source code later...
The mod works by checking every 10 milliseconds for gear switching, showing the gear the user is in, and setting the speed of the car.
I'll start with the gear change handler:
The code works by first checking if the user has the clutch enabled or not. The user can change this setting by setting the value of the clutch to false in the .ini file. If the clutch is enabled, it checks if the button for the clutch is being pressed. If the button is not being pressed the gear will not change. The actual gear switching is just adding and subtracting from a variable. There is a max gear of 6 and a minimum gear of 1. If the user is pressing the clutch, and the gear up button, then the car will shift into the next highest gear. I added an if statement to check if the gear is equal to 6. If this is true (the gear is equal to 6) The user will not be able to shift higher even if they are pressing the gear up button. It's the same concept for the gear down button but instead of 6 the gear must be higher than 1 to shift down.
Next, Showing what gear the user is in:
This was the easiest bit of code to write. It starts by checking to see if the user is in a vehicle. If the user is in a vehicle it draws the string ["Current Gear: "+gearState] in the upper right corner of the screen. If the statement is false nothing is shown.
Finally, setting the speed of the car:
The hardest piece of code to write by far. Even now it isn't perfected. What I have so far starts by declaring a vehicle variable "V" and setting it equal to the vehicle the player is in. Then it starts a very long if statement. First it checks to see if the player is in a car. Next it starts a switch statement based on the variable of the gear. In the case of the user being in gear one, the script checks if the car exists (to avoid errors) and if the car is not running. If both of those requirements are met, then the car is told to start. In a separate If statement, the script checks to see if the vehicle exists, as well as if the car is at or above 30MPH. If that is met, The car is set to go at 30MPH until the user shifts up or slows down. Gears 2-6 works the same but cap at higher speeds. Gears 2-6 also do not check if the engine is running. Instead they have an if statement that checks to make sure that the user is not trying to accelerate from 0MPH in a gear higher than 1. When the player leaves the car the gear is set back to 1 for the next car.
That's a big enough wall of text for now.
In : Coding