Invincible Vehicle [C#] Help
-
Hello! I'm very new with modding in C# and I've been stuck trying to get this Invincible Vehicle code into my GTA 5 trainer menu. I have the code, but it was originally made as a standalone script, so I've been trying to fit the code to work inside of my trainer and I have a little bit of a idea on what to do, but still need a little help to be pointed in the right direction. Once the menu loads in, all features work fine. But whenever I get to the Invincible Car button, it does nothing. I'm 100% sure its the code, but I've been trying for the past couple hours to fix it but cant find the problem. I've put a OnTick because I want the code to run every second after the button is pressed. Here's my code:
////BUTTON CODE BELOW:
void VehicleRepair() { vehicleRepair = new UIMenuItem("Invincible Vehicle: " + isVehicleRepaired.ToString()); vehicleMenu.AddItem(vehicleRepair); playerMenu.OnItemSelect += (sender, item, index) => { if (item == vehicleRepair) { isVehicleRepaired = !isVehicleRepaired; if (isVehicleRepaired) { void OnTick(object EventArgs) { if (isVehicleRepaired) { if (Game.GameTime > GameTimeReference) { if (Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, Game.Player.Character, false)) { car.Repair(); int GameTimeReference = Game.GameTime + 1000; vehicleRepair.Text = "Invincible Car: " + true.ToString(); } } } } } } }; }
/////END
This code is under public class Class1 : Script
Vehicle car;
int GameTimeReference = Game.GameTime + 1000;bool isVehicleRepaired;
/////Any help would be greatly appreciated please go easy on me
-
In your code you are just repairing the current vehicle, you haven't set anything that will make the vehicle invincible.
Adding this below the car.Repair(); should work:
car.IsInvincible = true;
-
It doesn't look like you're actually running it in a Tick. To run in a Tick you need to do something like this
public Class1() { Tick += Class1_Tick; } private void Class1_Tick(object sender, EventArgs e) { if (invincibleVehicle) { //get vehicle and set as invincible/repair it } }
And like Ben said, set the car to invincible.
-
Thank you both for your responses
So the code I put up wasn't working... as in it didn't even repair the vehicle. So setting it to be invincible didn't work either.
so I've changed around some code, but i'm still having trouble understanding. I have figured out a few things, and I took the advice of JitNaught and Ben, so heres what i have:private void Class1_Tick(object sender, EventArgs e) { if (isVehicleRepaired) { car.Repair(); car.IsInvincible = true; GameTimeReference = Game.GameTime + 1000; vehicleRepair.Text = "Invincible Vehicle: " + true.ToString(); } else { car.IsInvincible = false; vehicleRepair.Text = "Invincible Vehicle: " + false.ToString(); } } void VehicleRepair() { vehicleRepair = new UIMenuItem("Invincible Vehicle: " + isVehicleRepaired.ToString()); vehicleMenu.AddItem(vehicleRepair); playerMenu.OnItemSelect += (sender, item, index) => { if (item == vehicleRepair) { isVehicleRepaired = !isVehicleRepaired; if (isVehicleRepaired) { car.IsInvincible = true; } } }; }
my question is inside the brackets of this line of code:
if (isVehicleRepaired)
{
car.IsInvincible = true;
}do i have to make it run the Tick that I just made? Basically on the button press, it would run the Tick. But i have 0 clue on how to do that.
-
playerMenu.OnItemSelect
This is the problem. It should be
vehicleMenu
notplayerMenu
.
-
oh wow, thank you. I completely jumped over that lmao
Now all i have to figure out is whats wrong with the code, because it pops a error in game when I press the button.
Edit:
Also, could you help me figure out how to make the Invincible Vehicle button when pressed, run the Tick? All is appreciated
-
What is the error?
If you added the
Tick += Class1_Tick;
line, the Tick should already be running. You don't need to run the Tick when you change the invincible option.
-
Yes, i figured out the problem but in game it pops up a error as soon as the menu loads. The error is in the beginning of the Tick. But in visual studio there are no issues, so i have to fiddle around with the tick to see what works. Still is very confusing to me lmao, thanks for your help, i hope im not asking any stupid questions (:
-
My guess of the problem is you're not checking if the player is in a vehicle, then getting the vehicle in the Tick. You seem to be using a global variable (
car
), which will not work because once the player changes vehicles the value will be incorrect.
-
I have the if statement:
if (Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, Game.Player.Character, false))
inside of the Tick, How could i make Vehicle car; into a local variable (if I even change it to a local variable)
-
Do something like this
private void Class1_Tick(object sender, EventArgs e) { //get the current player character Ped playerPed = Game.Player.Character; //don't do anything if the player doesn't exist or is dead if (playerPed == null || !playerPed.Exists() || playerPed.IsDead) return; //don't do anything if the player isn't in a vehicle if (!playerPed.IsInVehicle()) return; //get the player's car Vehicle car = playerPed.CurrentVehicle; //don't do anything if the player's car doesn't exist if (car == null || !car.Exists()) return; //your code that does stuff with the car }
-
Yes! Thank you so much, I edited your script a little to fit more of what I would like it to do, instead of the tick running all the time, could I make it so that whenever the Invincible Vehicle button is pressed in the menu, it would run the Tick?
-
Running it only when pressing the button defeats the purpose of having the Tick. If you really want to only enable the Tick after pressing the button, move the
Tick += Class1_Tick;
to your button press code. I advise against this.
-
Whats the issue with moving Tick += Class1_Tick; to the button press code?
-
One issue is that every time you press the button it will add the Tick again.
-
Hmm... Well the reason why I wanted the Tick was because I wanted the car.Repair(); line to run every second...
Basically I this is the button now:void VehicleRepair() { vehicleMenu.AddItem(vehicleRepair); vehicleMenu.OnItemSelect += (sender, item, index) => { isVehicleRepaired = !isVehicleRepaired; Ped playerPed = Game.Player.Character; if (playerPed == null || !playerPed.Exists() || playerPed.IsDead) return; if (!playerPed.IsInVehicle()) return; Vehicle car = playerPed.CurrentVehicle; if (car == null || !car.Exists()) return; if (Game.GameTime > GameTimeReference) { car.IsInvincible = true; car.Repair(); GameTimeReference = Game.GameTime + 1000; } }; }
Now when I press the button in game, it fixes the car. But what i want it to do is run that car.Repair(); line every second but i have no clue how to do that without the Tick..
-
Keep the tick always running, just check the current GameTime against your reference to determine whether you should run your repair code or not.
Pseudo-code, in Tick:
if (game.gametime > nextTimeRunCode) { //do repair vehicle stuff }
-
I'm a little confused with the line of code you gave me. Do you mind explaining a little more in depth what it does?
-
Basically what it does is it compares the current time to the time you're supposed to run the code next. If the current time is greater, that means it has been 1 second, and you should run the code.
Although I see the code you provided already has the same thing. I must have missed it yesterday. But like I said, that code will not run every second if it is not in a Tick. A Tick is the only way to get a recurring action to happen.
Edit: support > supposed
-
Okay now its all starting to come together. Thanks for your help and advice, I'll make it so it would only repair the vehicle, because as you previously said pressing the button defeats the purpose for a Tick, and i'm trying to make a standalone menu mod. I appreciate your patience