C
C#13mo ago
xTagz_

❔ How to make a line of code run every x seconds?

I have this function:
public override void Process()
{
Suspect.Tasks.PlayAnimation("amb@world_human_aa_smoke@male@idle_a", "idle_a", 1, AnimationFlags.None); //Line that i want to be run every x seconds
Thread.Sleep(1000);
if (STPFunctions.isPedStopped(Suspect))
{
End();
}
base.Process();
}
public override void Process()
{
Suspect.Tasks.PlayAnimation("amb@world_human_aa_smoke@male@idle_a", "idle_a", 1, AnimationFlags.None); //Line that i want to be run every x seconds
Thread.Sleep(1000);
if (STPFunctions.isPedStopped(Suspect))
{
End();
}
base.Process();
}
This function repeats as fast as it can, and i want to keep that but make one line run only once every x seconds. This may sound weird but i think you will understand.
32 Replies
Omnissiah
Omnissiah13mo ago
you need Timers
xTagz_
xTagz_OP13mo ago
What is a timer? How do i make it? Can you give me an example code for this function?
sibber
sibber13mo ago
are you using a specific gui/game framework?
xTagz_
xTagz_OP13mo ago
Uhhh RagePluginHook
Omnissiah
Omnissiah13mo ago
if you write Timer in the ide it should auto-suggest something
sibber
sibber13mo ago
are you modding a game?
xTagz_
xTagz_OP13mo ago
Yeah gta
sibber
sibber13mo ago
ah does gta allow mods in its ToS? i forget
xTagz_
xTagz_OP13mo ago
Porbably yes theres like milions of them There are whole mod engines/frameworks for gta
RumTery
RumTery13mo ago
U can create float variable out of this function and add deltaTime to it every time function is called. If variable's value is greater than 1, u set it to 0 and call your EverySecondMethod
sibber
sibber13mo ago
yup it is for singleplayer and rp servers
xTagz_
xTagz_OP13mo ago
Lemme try
sibber
sibber13mo ago
this is not unity
RumTery
RumTery13mo ago
deltaTime could be provided by framework, or u can create stopwatch out of this function and use it's ElapsedSeconds
xTagz_
xTagz_OP13mo ago
It isn't provided
sibber
sibber13mo ago
im not familiar with any modding libraries but generally id recommend using a PeriodicTimer
RumTery
RumTery13mo ago
So create stopwatch somwhere near float variable. Add it's ElapsedSeconds to varialbe and call Restart every time
sibber
sibber13mo ago
no need, use a periodic timer its also async
xTagz_
xTagz_OP13mo ago
RPH just provides functions for doing stuff with the game, no timing and stuff. WTF is a periodic timer
sibber
sibber13mo ago
ill write an example when i get to my pc
xTagz_
xTagz_OP13mo ago
Won't work i gotta use .net 4.8
sibber
sibber13mo ago
ah
xTagz_
xTagz_OP13mo ago
yeah i know but it isn't my decision, its just RPH stuff
sibber
sibber13mo ago
use a regular timer then or Task.Delay maybe on a threadpool thread too
xTagz_
xTagz_OP13mo ago
Ok, ty
sibber
sibber13mo ago
np
RumTery
RumTery13mo ago
But not in Process function
sibber
sibber13mo ago
wait do you want the function to return?
xTagz_
xTagz_OP13mo ago
yes
sibber
sibber13mo ago
ok yeah then Task.Delay and Task.Run or a timers.timer
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server