Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!   (84,894 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

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

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

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

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

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

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

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. 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

    2. Red Dead Redemption

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

    2. Site Suggestions

Sign in to follow this  
ISOFX

[C#] Timers?

Recommended Posts

ISOFX

I've looked into how to slow down loops in C#, and i found timers and the Thread.Sleep method however i tried Thread.Sleep but its similar to the Wait function, it pauses the whole script. I also tried timers, and when i put a timer in and set an interval for it, nothing happens and it plays my code as normal. I am trying to slow down a looped particle effect without pausing my whole script.

 

My Timer Code:

System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();public ParticleMechanic(){    t.Interval = 1000;    t.Tick += new EventHandler(timer_Tick);    Tick += timer_Tick;}private void timer_Tick(object sender, EventArgs e){    if (PuffParticleActive) { PlayParticleOnEntity(thePuffCar, "scr_rcbarry2", "scr_clown_appears", 1.0f); }}

I've tried several ways and i get no changes..

I want a one second gap when looping the particle effect. Also without pausing the whole script.

I get no errors when building,

Could someone help me out?

Thank You :)

Edited by ISOFX

Share this post


Link to post
Share on other sites
Unknown_Modder

In my scripts (C++), I use a very simple function:

(In a class "script"):

static unsigned long timer;

script::loops:

if (GET_GAME_TIMER() >= timer) { timer = GET_GAME_TIMER() + 1000; } // 1 second delay

Inside a looped function:

if (GET_GAME_TIMER() >= timer) {     // code here}

Share this post


Link to post
Share on other sites
Jitnaught

The timer has to be started for it to run, but since you are already using the built-in ScriptHookV .NET timer (Tick += timer_Tick;) you don't need the extra timer.

To change the interval for the built-in timer call "Interval = 1000;".

If you code isn't called it is probably because of the code within the timer, not the timer itself.

Share this post


Link to post
Share on other sites
ISOFX

Thanks, ill try what @Unknown_Modder posted in a bit, but, @LetsPlayOrDy i tried changing the interval for OnTick and i just get the same as when the interval isnt there.. i did "Interval = 1000;" and no change on the particle.. Also i started the timer when i enable the mod, so when the checkbox in the menu is pressed 3 functions will be called:

t.Start();PuffParticleActive = true;UI.Notify("Particle: ~g~ON");//and when the bool is set to false,t.Stop();PuffParticleActive = false;UI.Notify("Particle: ~r~OFF");
Edited by ISOFX

Share this post


Link to post
Share on other sites
GeorgeZhang

I used to utilize Game.GameTime , but it would require the native to be called per tick in order to retrieve the gametime. This is what I do now to create a slower loop:

int ActionCounter = 0;int interval = 1500;//1500 is about 30-40 seconds//the following inside 'OnTick'if (ActionCounter > 500000) ActionCounter = 0;//this is optionalActionCounter++;if (ActionCounter % interval == 1299){     //Actions}

Hope it helps!!

Edited by GeorgeZhang

Share this post


Link to post
Share on other sites
ISOFX

I used to utilize Game.GameTime , but it would require the native to be called per tick in order to retrieve the gametime. This is what I do now to create a slower loop:

int ActionCounter = 0;int interval = 1500;//1500 is about 30-40 seconds//the following inside 'OnTick'if (ActionCounter > 500000) ActionCounter = 0;//this is optionalActionCounter++;if (ActionCounter % interval == 1299){     //Actions}

Hope it helps!!

 

Thanks, will check when i get back. :)

Share this post


Link to post
Share on other sites
InfamousSabre

I used to utilize Game.GameTime , but it would require the native to be called per tick in order to retrieve the gametime. This is what I do now to create a slower loop:

int ActionCounter = 0;int interval = 1500;//1500 is about 30-40 seconds//the following inside 'OnTick'if (ActionCounter > 500000) ActionCounter = 0;//this is optionalActionCounter++;if (ActionCounter % interval == 1299){     //Actions}

Hope it helps!!

I was waiting for somebody to post this. Modulo is the easiest way to make a timer for anything.

Share this post


Link to post
Share on other sites
jedijosh920

I've been mainly using Game.GameTime and System.DateTime as timers, but I believe this is how the System.Timers timers work:

using System.Timers;  System.Timers.Timer timer = new System.Timers.Timer(5000); // create new instance of a timer timer.Start(); // start the timer timer.Elapsed += (Object source, System.Timers.ElapsedEventArgs e) => {    // do whatever once 5000 milliseconds has passed    timer.Stop(); }
Edited by jedijosh920

Share this post


Link to post
Share on other sites
CliffHanger

Like everyone mentioned, I find adding a timevariable++; inside the OnTick loop is the easiest solution (although it does seem to get offset as the framerate changes), you can also work with tasks if you want, since thread.sleep will not lock your current thread when using those. The only problem is that OnTick is smart enough to stop when the game is paused. Thread.sleep won't.

 

 

Share this post


Link to post
Share on other sites
milkjunk87

negative tokens nsa :myou

Share this post


Link to post
Share on other sites
ISOFX

 

I've been mainly using Game.GameTime and System.DateTime as timers, but I believe this is how the System.Timers timers work:

using System.Timers;  System.Timers.Timer timer = new System.Timers.Timer(5000); // create new instance of a timer timer.Start(); // start the timer timer.Elapsed += (Object source, System.Timers.ElapsedEventArgs e) => {    // do whatever once 5000 milliseconds has passed    timer.Stop(); }

 

Thanks bro. Just wondering, how would you use Game.Time? :)

Share this post


Link to post
Share on other sites
ShadowCoderKing

 

I've been mainly using Game.GameTime and System.DateTime as timers, but I believe this is how the System.Timers timers work:

using System.Timers;  System.Timers.Timer timer = new System.Timers.Timer(5000); // create new instance of a timer timer.Start(); // start the timer timer.Elapsed += (Object source, System.Timers.ElapsedEventArgs e) => {    // do whatever once 5000 milliseconds has passed    timer.Stop(); }

great to learn from thanks man!

Share this post


Link to post
Share on other sites
LeeC2202

One thing to be aware of when using System.Timer, is that it ignores any changes you make to Game.TimeScale.

 

So imagine your game is running as normal and you want something to happen every 5 seconds. You'd set the System.Timer interval to 5000 and that would be fine. But then in your mod, you have a function that triggers a slow-mo and that pulls the time down to 1/4 normal speed. Suddenly, 5 seconds is now 20 seconds in game time, but your timer is still going to trigger after 5 real-time seconds regardless.

 

For that reason, I always find that creating your own TimeScale-based timers are much more effective, if you are going to be making TimeScale modifications. Something like this...

    public class VariableTimer    {        public delegate void TimerExpired(object sender);        public event TimerExpired OnTimerExpired;        // Holds the countdown value        int TimerMax;        decimal TimerCounter;        bool IsRunning;        public bool AutoReset;        // Constructor, takes the interval as an input        public VariableTimer(int interval)        {            TimerCounter = interval;            TimerMax = interval;        }        public void Update(float timescale)        {            if (!IsRunning) return;            // Gets the value into 0 - 1000 range            float timerElapse = Game.LastFrameTime * 1000f;            // Reduce the timer by the elapsed time, modified by the game's timescale. Decimal seems to be more accurate            // when subtracting values less than 1, float can be a bit erratic.            TimerCounter -= (decimal)(timerElapse * timescale);            if (TimerCounter <= 0)            {                OnTimerExpired?.Invoke(this);                // Reset if the AutoReset property is true                if (AutoReset)                {                    // I add TimerMax rather than set it to that to allow for any overlap into the next timer period                    TimerCounter += TimerMax;                }                else                {                    Stop();                }            }        }        public void Stop()        {            IsRunning = false;        }        public void Start()        {            IsRunning = true;        }        public void Reset()        {            TimerCounter = TimerMax;        }        // For demo purposes only...        public int Counter        {            get { return (int)TimerCounter; }        }    }

You can just declare it with

        VariableTimer MyTimer;

And then instance it with this

            MyTimer = new VariableTimer(1000);            MyTimer.AutoReset = true;            MyTimer.OnTimerExpired += MyTimer_OnTimerExpired;

And then finally, update it in onTick() like this

            MyTimer.Update(TimeScaleValue);

That will give you a timer that adapts to any timescale changes and will always ensure that the interval is game-time related. TimeScaleValue is something you have to keep track of yourself, because Game.TimeScale has no getter, it is a set only property.

 

I use a simpler version of this kind of thing in my OnFootCinematic Camera mod, to ensure the cameras always expire after the same amount of game time, no matter how the user adjusts the timescale. Someone might find this code useful though, so I thought it was worth posting.

Edited by LeeC2202

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

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