Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. PC
      2. Gameplay
      3. Missions
      4. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
      4. Frontier Pursuits
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. Diamond Casino & Resort
      2. DLC
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Grand Theft Auto Series

    3. GTA 6

    4. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    5. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA IV Mods
    6. GTA Chinatown Wars

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA SA Mods
    10. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA VC Mods
    11. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA III Mods
    12. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

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

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

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

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

    2. Forum Support

    3. Site Suggestions

KelFlakes

C# using timers and intervals

Recommended Posts

KelFlakes

Hi,

 

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

 

Thanks!

Share this post


Link to post
Share on other sites
KelFlakes

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());
            i++;

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

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

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

        }
    }

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.

Share this post


Link to post
Share on other sites
KelFlakes

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++;
        }

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

Share this post


Link to post
Share on other sites
KelFlakes

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());
            i++;
        }

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

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

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.

Share this post


Link to post
Share on other sites
KelFlakes

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.

Share this post


Link to post
Share on other sites
Guest
Posted (edited)

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


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