stef538 Posted May 1, 2020 Share Posted May 1, 2020 When I try to access another function with a Wait function the whole script just stops and waits untill the Wait() is over. I even tried calling it with a new. //Here we call the function new Breakdown.BreakDown(veh); //This is the actual function internal class BreakDown { private Vehicle veh; public BreakDown(Vehicle veh) { this.veh = veh; if (BreakdownInteger >= 2) { random = new Random(); int time = random.Next(5, 10); time = (time * 1000); veh.FuelLevel = 0; veh.IsEngineRunning = false; veh.IsDriveable = false; Wait(time); veh.FuelLevel = 100; veh.IsEngineRunning = true; veh.IsDriveable = true; BreakdownInteger = 0; } else { BreakdownInteger++; } } } I also tried this with a simple static function but I have the exact same issue. Link to comment Share on other sites More sharing options...
LeeC22 Posted May 1, 2020 Share Posted May 1, 2020 That's how wait works... it literally tells everything to stop and wait for the specified period of time. stef538 1 Link to comment Share on other sites More sharing options...
stef538 Posted May 1, 2020 Author Share Posted May 1, 2020 Lol I feel like a dork, but in this case I just want Class BreakDown To wait an X time, i'll figure something out. Thanks Link to comment Share on other sites More sharing options...
LeeC22 Posted May 1, 2020 Share Posted May 1, 2020 (edited) You'll find a whole host of methods for doing what you want. Some people wait until Game.Gametime = Now + WaitPeriod before they continue, I prefer to use countdowns but basically, you branch inside your function based on flag conditions and your timer/countdown sets those conditions. It means you have to keep calling the function each frame until all conditions have been met. This is a quick kind of skeleton function: void DoFunction() { // Convert the Frame time to milliseconds int elapsedTime = (int)(Game.LastFrameTime * 1000); if (!FuncInit) { // Do initial stuff here... // Set a 1/2 second wait TimerCountdown = 500; FuncInit = true; } else { TimerCountdown -= elapsedTime; if (TimerCountdown < 0) { // Do rest of stuff here when the timer has counted down } } } So FuncInit is initially set to false and that causes it to do the init stuff and you set the wait timer. When FuncInit is set to true, it drops through into the waiting section, which is counting down the timer. When that timer gets below zero, it does the rest of the stuff you need to do. You'd probably have another flag that tells you when the whole function is complete and you'd call DoFunction() each frame until that flag is set. Edited May 1, 2020 by LeeC22 stef538, SayagoHren and Jitnaught 3 Link to comment Share on other sites More sharing options...
stef538 Posted May 4, 2020 Author Share Posted May 4, 2020 Hmm it doesn't seem to work in my case because TimerCountDown never gets below zero. public void RemoveDirt(Vehicle Vehicle) { // Convert the Frame time to milliseconds int elapsedTime = (int)(Game.LastFrameTime * 1000); if (!FuncInit) { // Do initial stuff here... Vehicle.DirtLevel = 15f; // Set a 1/2 second wait TimerCountdown = 500; FuncInit = true; } else { TimerCountdown -= elapsedTime; if (TimerCountdown < 0) { if (Vehicle.DirtLevel >= 0.0f) { Vehicle.DirtLevel -= 0.05f; Notification.Show(Vehicle.DirtLevel.ToString()); RemoveDirt(Vehicle)); } // Do rest of stuff here when the timer has counted down } } } Link to comment Share on other sites More sharing options...
LeeC22 Posted May 4, 2020 Share Posted May 4, 2020 (edited) You have to keep calling the function each frame to let the timer run down. This is why I mentioned the other flag. Any variables that are not declared inside the function need to be declared as Global variables. In onTick(), set RemovingDirt to true when you initialise the process. if (RemovingDirt) DoFunction(); And then your function void DoFunction() { // Convert the Frame time to milliseconds int elapsedTime = (int)(Game.LastFrameTime * 1000); if (!FuncInit) { // Do initial stuff here... PlayerVehicle.DirtLevel = 15f; // Set a 1/2 second wait TimerCountdown = 500; FuncInit = true; } else { TimerCountdown -= elapsedTime; if (TimerCountdown < 0) { // Do rest of stuff here when the timer has counted down PlayerVehicle.DirtLevel -= 0.05f; UI.Notify(PlayerVehicle.DirtLevel.ToString()); PlayerVehicle.DirtLevel = 0; RemovingDirt = false; } } } Whenever you use any kind of countdown or timer system, you have to keep calling the function until the timer has elapsed, or the countdown hits the target. I use PlayerVehicle instead of Vehicle as a variable, because Vehicle is a Type keyword and it can cause issues using Types as variable names. The exception to that rule seems to be enums, where Microsoft recommend using the enum name as the variable name. Your choice of course but it's just something to be aware of. Edited May 4, 2020 by LeeC22 stef538 1 Link to comment Share on other sites More sharing options...
stef538 Posted May 4, 2020 Author Share Posted May 4, 2020 I tried your approach before posting but that just seems to crash my game instantly. Link to comment Share on other sites More sharing options...
LeeC22 Posted May 4, 2020 Share Posted May 4, 2020 using System; using GTA; namespace DirtyCarCleanerTest { public class cDirtyCarCleanerTest : Script { private Ped PlayerPed; private Vehicle PlayerVehicle; public bool FuncInit { get; private set; } public int TimerCountdown { get; private set; } public bool RemovingDirt { get; private set; } public cDirtyCarCleanerTest() { Tick += onTick; Interval = 0; } private void onTick(object sender, EventArgs e) { // Exits from the loop if the game is loading if (Game.IsLoading) return; if (Game.Player.Character != PlayerPed) PlayerPed = Game.Player.Character; if (PlayerPed.IsSittingInVehicle() && PlayerVehicle != PlayerPed.CurrentVehicle) { PlayerVehicle = PlayerPed.CurrentVehicle; RemovingDirt = true; UI.Notify("Initialising Dirt Function"); } if (RemovingDirt) { DoFunction(); UI.ShowSubtitle(string.Format("Timer: ~y~{0}", TimerCountdown)); } } void DoFunction() { // Convert the Frame time to milliseconds int elapsedTime = (int)(Game.LastFrameTime * 1000); if (!FuncInit) { // Do initial stuff here... PlayerVehicle.DirtLevel = 15f; // Set a 1/2 second wait TimerCountdown = 500; FuncInit = true; UI.Notify("Setting Dirt Level"); } else { TimerCountdown -= elapsedTime; if (TimerCountdown < 0) { // Do rest of stuff here when the timer has counted down if (PlayerVehicle.DirtLevel >= 0.05f) { PlayerVehicle.DirtLevel -= 0.05f; RemovingDirt = false; UI.Notify("Setting Dirt Level " + PlayerVehicle.DirtLevel.ToString()); } } } } } } This works fine, I don't even know how anything in there could crash the game to be honest. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now