Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Happy Holidays from the GTANet team!

[C#] Wait() seems to delay whole script


stef538
 Share

Recommended Posts

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

That's how wait works... it literally tells everything to stop and wait for the specified period of time.

Link to comment
Share on other sites

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

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 by LeeC22
  • Like 3
Link to comment
Share on other sites

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

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 by LeeC22
Link to comment
Share on other sites

I tried your approach before posting but that just seems to crash my game instantly.

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.