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

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
Guest

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 Guest

Share this post


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

I have a question...i want my ped doing two animations and at the last frame i gave him weapons....here is the code

 TaskSequence moine = new TaskSequence()

  moine.AddTask.RunTo(dolum.Position,false,9000);
  moine.AddTask.PlayAnimation("pickup_object", "pickup_low", 8f, -1, false, 0f);

  Script.Wait(3000);

  companion.Weapons.Give(WeaponHash.Musket, 2, true, true);

 

 

It deosnt work....always give the weapon erlier or later after the last animation

Edited by NOTHING2

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.

Sign in to follow this  

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