Jump to content

Hey. I'm currently making a mod for a game and need to know how to make Timers in C# (But in the way I need to use them)

 

I have a method that adds to a score. But, every second I want to reset that score to 0. How do I go about doing this?

 

I thought of trying to do it like this:

PLA9cyq.png

 

But as far as I can tell by looking at it, which line of code exactly is executing resetScore(); ???

(I know there are errors, that's why I need help)

 

QBNcgx1.png

 

I know I need to add an argument when I call this method but there is no documentation on what to put when in a situation like this.

 

Can anybody help? 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
https://linustechtips.com/topic/941340-timers-in-c/
Share on other sites

Link to post
Share on other sites

Check out the `System.Timers.Timer` documentation on MSDN, specifically the `Elapsed` callback.

 

https://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed(v=vs.110).aspx

 

private void resetScore(object sender, ElapsedEventArgs e)
{
    // blah
}

You don't ever call this method yourself, setting `Enabled` to true on the timer calls it every second for you

Link to comment
https://linustechtips.com/topic/941340-timers-in-c/#findComment-11475713
Share on other sites

Link to post
Share on other sites

43 minutes ago, Xelvanox said:

Check out the `System.Timers.Timer` documentation on MSDN, specifically the `Elapsed` callback.

 

https://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed(v=vs.110).aspx

 


private void resetScore(object sender, ElapsedEventArgs e)
{
    // blah
}

You don't ever call this method yourself, setting `Enabled` to true on the timer calls it every second for you

Thank you. I'm not too familiar with C# yet and was expecting the timers to be slightly similar to Java's timers (where the timer has its own 'method' and you can call any other functions inside of it) 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
https://linustechtips.com/topic/941340-timers-in-c/#findComment-11475869
Share on other sites

Link to post
Share on other sites

2 hours ago, EvilCat70 said:

Hey. I'm currently making a mod for a game and need to know how to make Timers in C# (But in the way I need to use them)

Unity 3D mod? 

2 hours ago, EvilCat70 said:

I have a method that adds to a score. But, every second I want to reset that score to 0. How do I go about doing this?

If Unity 3D:
 

float timer = 0.0f;
int score = 0;
Update () {
  timer += time.deltatime;
  if (timer >= 1) {
    score = 0;
  }
}
2 hours ago, EvilCat70 said:

I thought of trying to do it like this:

PLA9cyq.png

 

But as far as I can tell by looking at it, which line of code exactly is executing resetScore(); ???

(I know there are errors, that's why I need help)

 

QBNcgx1.png

 

I know I need to add an argument when I call this method but there is no documentation on what to put when in a situation like this.

 

Can anybody help? 

Not quite sure what your code does. I haven't seen your class for Timer nor ElapsedEventHandler though those are likely API classes from the API you're using. 

 

What game are you modding or what engine is it running on? If unity 3D-the code I wrote out is basically how I'd do it if working directly in Unity. Time.DeltaTime is a special function from Unity 3D's API. 

Link to comment
https://linustechtips.com/topic/941340-timers-in-c/#findComment-11476203
Share on other sites

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

×