Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Solved] Time interval

Featured Replies

Hi everyone

 

I want to run a function (void) every 10s after going on duty. I'm pretty new to C# so I have no idea how I would do that. I was looking for something similar to setInterval() in JavaScript. After looking online, I found a solution with Timer but that seems to either crash GTA or throw an error. 

 

private static void Functions_OnOnDutyStateChanged(bool OnDuty) {
	if (OnDuty) {
      
		doStuff(); // run this every ten seconds
      
    }
}

 

Edit: 

I found this: https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=netframework-4.7.1 and tried it but GTA will crash:

private static Timer timer;
private static void Functions_OnOnDutyStateChanged(bool OnDuty) {
	if (OnDuty) {

		timer = new Timer();

		timer.Interval = 10000;

		timer.Elapsed += Timer_Elapsed;

		timer.Enabled = true;

	}
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e) {
	doStuff();
}

 

Thanks to anyone who took the time to read this

Edited by HeyPalu

  • Author

Alright turns out this actually does work. My function just seems a little too much to be run multiple times or at least only running a game log won't crash GTA. 

21 hours ago, HeyPalu said:

I found this: https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=netframework-4.7.1 and tried it but GTA will crash:

private static Timer timer;
private static void Functions_OnOnDutyStateChanged(bool OnDuty) {
	if (OnDuty) {

		timer = new Timer();

		timer.Interval = 10000;

		timer.Elapsed += Timer_Elapsed;

		timer.Enabled = true;

	}
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e) {
	doStuff();
}

 

  • The title was changed to [Solved] Time interval
  • Management Team

@HeyPalu It is likely crashing because the timer callback does not happen on the same thread as the game's script. If you do not want to create your own timer class, you could simply have a variable that keeps the game time you started something and then compares it to the current game time + interval.

Please do not PM me unless really necessary (knowing you helps). If you think you need my attention in a topic, tag me.

  • Author
1 hour ago, LMS said:

you could simply have a variable that keeps the game time you started something and then compares it to the current game time + interva

Alright thanks for your response. I know what you mean but I do not know how to write that in C#. I would love to implement it into my plugin though; So, if you have the time, I would really appreciate it if you could show me a code example or tell me where I could find one. 

I submitted a pull request to the github repo, this should help out with the crashes & such caused by the Timers.

While I do not have time to go into depth explaining what timers does and such, what I added should at least solve somewhat the issue of the crashing/freezing of GTA, I included a link in the commit description showing RagePluginHook documentation that maybe might explain what I am trying to say better instead.

My plugins by me are on this site in addition to my own site as well.

Do Not PM Me for Support! follow the instructions for getting support with any mods. 

  • 1 month later...
  • Author

Solution

 

Thanks to @Echooo I finally got it working 

GameFiber IntervalFiber = GameFiber.StartNew(Interval);

private static void Interval() {
  // CurrentlyOnDuty is updated in the duty change handler
    while (CurrentlyOnDuty) {
        doStuff();
        GameFiber.Wait(30000);
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.