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. Forum Support

    3. Suggestions

C# using timers and intervals


Recommended Posts



I wanted to create a script with different timers/ticks. Now it's crashing all the way.. And I don't know what timer I need to pick. Like in Scripthook for GTA 4 I can remember to create a new timer and set it's interval etc. (like a winforms timer). Now it's different.


I want to have intervals between different script files in my mod. 


So for example:


.cs file 1: interval = 45000

.cs file 2: interval = 15000

.cs file 3: interval = 35000


When I am using just the this.Interval it doesn't listen to the 35000 for cs file 3, but it looks like cs file 3 cannot run the timer/tick yet because in another script file the interval is still going on. You understand? 


So i was starting using winforms timers like in older scripthook. Then I was able to create specific timers for each void or file (exactly what i need). Every file and void needs to have it's own timings and all needs to be executed in time for that void. With winforms it's possible. 

Just a simple test with displaying text from different voids on certain timings works. But when I set these timers on my whole mod with spawning vehicles etc. the game crashes instantly.


What's the best way to create specific timer/ticks for each seperate void and file?


Sorry if it isn't clear, I'm almost asleep.. 



Link to comment
Share on other sites

Below my code:


    public class ElapseTest2 : Script
        Timer timer1 = new Timer();
        Timer timer2 = new Timer();
        Timer timer3 = new Timer();

        int i = 0;
        int i2 = 0;
        int i3 = 0;

        Vehicle police_car_1;
        Vehicle police_car_2;
        Vehicle police_car_3;

        Vehicle getawaycar;

        Ped cop_01_driver;
        Ped cop_01_passenger;
        Ped cop_02_driver;
        Ped cop_02_passenger;
        Ped cop_03_driver;
        Ped cop_03_passenger;

        Ped getaway_01;
        Ped getaway_02;
        Ped getaway_03;
        Ped getaway_04;

        List<Vehicle> vehicles = new List<Vehicle>();
        List<Ped> peds = new List<Ped>();


        public ElapseTest2()
            timer1.Interval = 20000;
            timer2.Interval = 40000;
            timer3.Interval = 80000;

            timer1.Tick += FirstElapsedVoid;
            timer2.Tick += SecondElapsedVoid;
            timer3.Tick += ThirdElapsedVoid;

            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;

        private void FirstElapsedVoid(object sender, EventArgs e)
            UI.Notify("Message from 1st void ElapseTest2 raised every 20 sec." + " " + i.ToString());

            police_car_1 = World.CreateVehicle(VehicleHash.Police, Game.Player.Character.Position.Around(6.0f));

        private void SecondElapsedVoid(object sender, EventArgs e)
            UI.Notify("Message from 2nd void ElapseTest2 raised every 40 sec." + " " + i2.ToString());

        private void ThirdElapsedVoid(object sender, EventArgs e)
            UI.Notify("Message from 3rd void ElapseTest2 raised every 80 sec." + " " + i3.ToString());


So.. When I run this code without spawning the police car it works as expected. But when I write down a spawning line it crashes instantly.. The log says nothing. The whole game shuts down and I haven't got any feedback or error.

I am using a Windows Forms Timer object because the basic Script.Tick doesn't give me what I want. I want to set an interval for each method/event seperately, but with the default Script.Tick is it impossible.

Link to comment
Share on other sites

Well I really don't understand it anymore haha.. I really need to use seperated intervals for each event/method.. otherwise I can throw my mod away or I can make it a mess with workarounds what I don't prefer.. 


I made some other code, like adding player money instead of spawning a car and I get strange results when I 'debug' with the UI.Notify


        private void Timer_Elapsed(object sender, EventArgs e)
            UI.Notify("Message from 1st void ElapseTest2 raised every 5 sec." + " " + i.ToString() + " " + Game.Player.Character.Money.ToString());
            Game.Player.Character.Money += 10;

I already have in-game money of million bucks, but when I display my current money balance as above into my UI notify I'm getting a 300 bucks.. And it also adds 10 to the value, but my game money doesn't increase.. Like it is using it as a variable only in the event method.. 

Link to comment
Share on other sites

Already found a possible solution! Easier as I thought, but I can set an Interval for each method in the method itself.. hehe.. And just using the Script.Tick of scripthook


        public ElapseTest2()
            Tick += Timer_Elapsed;
            Tick += FirstElapsedVoid;

        private void Timer_Elapsed(object sender, EventArgs e)
            this.Interval = 30000;

            UI.Notify("Message from 1st void ElapseTest2 raised every 15 sec." + " " + i.ToString() + " " + Game.Player.Character.Money.ToString());

        private void FirstElapsedVoid(object sender, EventArgs e)
            this.Interval = 30000;

            UI.Notify("Message from 1st void ElapseTest2 raised every 30 sec." + " " + i.ToString());

Gonna test it now

Really.. I can't edit my posts.. the first interval needs to be 15000 ofcourse

Still doesn't work as expected.. When the interval of Timer_Elapsed is set on 15000ms and the interval of FirstElapsedVoid is set on 30000 both will display after 30 sec.. I want to display the first after 15 sec. and the second after 30.. not both after 30 sec. I think it needs to be asycn of each other, but really don't know how.

Link to comment
Share on other sites

Thought it worked.. But it seems to overwrite the whole Interval of the whole class to the last set interval. I want to set it seperately but there is no documentation about it so can't go further now.

Link to comment
Share on other sites

  • 1 month later...

Just put this together in the last hour or so, see if this has anything that helps. https://drive.google.com/file/d/1NABuBaf59-L1DNYsSPVYen31JPPdRwfF/view?usp=sharing


I don't like using System timers, I prefer to use my own inside a game. This is a barebones project that demos the two classes inside, GTATimerStack and GTATimer. It's pretty simple as a design but it's flexible enough to allow you to have as many timers as you like, all at different intervals. It shows how to use shared event handlers and those lambda things as well. You can expand on what's already there, to add additional functionality if you want it.


Make sure you change the path in the Post Build-event section to match your game installation path if you want the dll copied into your game automatically. That's in Project > TimerStackTest Properties > Build Events > Edit Post-build...


Edit: *Looks at post date, looks at last-visited date, shakes head and sighs deeply*

Edited by Guest
Link to comment
Share on other sites

  • 2 years later...
Ironia Official
On 3/21/2019 at 10:26 PM, Guest said:

*Looks at post date, looks at last-visited date, shakes head and sighs deeply*

Hi, i'm going to be crazy... When i play gta, if the frame slow down, my scripts doesn't work right. for Example the function apply force work fine at 40fps, but if the fps drop to 30, applyforce is too slow. I read some of your post for syncro interval with game time but i didn't understand, can you help me please?

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

  • 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.