Forum C# Setting timers.

Setting timers.

Postby mfranzs » March 24th, 2011, 2:46 am

Hello- I need to set a timer that changes how long it takes to register every tick. For example, I want the timer to take 60 seconds to trigger the first time, then 30 seconds the next time.

How can I either:
Make a timer that only runs once.
or
Make a timer that I can overwrite with a new timer, effectively canceling the old timer
or
Remove timers

Thanks :D
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Setting timers.

Postby Henrik » March 24th, 2011, 8:57 am

mfranzs wrote:Make a timer that only runs once.

http://playerio.com/documentation/refer ... leCallback

mfranzs wrote:Make a timer that I can overwrite with a new timer, effectively canceling the old timer

That's not possible.

mfranzs wrote:Remove timers

http://playerio.com/documentation/refer ... timer#Stop
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Setting timers.

Postby kustrle » March 24th, 2011, 6:58 pm

mfranzs wrote:Make a timer that I can overwrite with a new timer, effectively canceling the old timer

Code: Select all
if(timer!=null)timer.Stop();
timer=AddTimer(function,1000);

Can't you do it like this?
kustrle
 
Posts: 10
Joined: February 11th, 2011, 10:16 pm

Re: Setting timers.

Postby mfranzs » March 24th, 2011, 10:35 pm

Thanks so much for your replies!

I still have an error though :/

Here is my code-

Code: Select all
Action callback = delegate { onTick(); };//onTick() is called later down
int time = 50;
public PlayerIO.GameLibrary.Timer ScheduleCallback (Action callback , int time);

A field initializer can not reference the non-static field, method, or property 'Mygame.gameCode.onTick'

But when i change
Code: Select all
public void  onTick()
        {


to
Code: Select all
public static void  onTick()
        {


All sorts of errors on the variables form :(

Help please!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Setting timers.

Postby Henrik » March 25th, 2011, 9:13 am

Static methods are "global", they aren't tied to a specific instance of a class, they're tied to the class itself. This means that they can't access instance variables of a class, so if you for example want to make your onTick() method static, and it loops over the Players list, you have to pass that list in as an argument to the method, and you have to return all your data, you can't modify instance variables from it.

But it's probably easier to keep your onTick() method non-static, and call it properly. It's kinda hard to see in what context you're using it, but if you're starting it in GameStarted or something, it could look like this:

Code: Select all
[RoomType("MyRoomType")]
public class MyGame : Game<MyPlayer> {
   public void onTick {
      //whatever...
   }

   public int time = 50;
   public Timer timer = null; //Keep a reference to the timer
   public override void GameStarted() {
      //Do some other stuff...

      //Start timer
      timer = ScheduleCallback(delegate { this.onTick(); }, time);
   }
}
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Setting timers.

Postby mfranzs » March 26th, 2011, 2:15 am

Great, thanks!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Setting timers.

Postby mfranzs » March 26th, 2011, 4:15 am

Okay... so, ScheduleCallback seems RIDICULOUSLY unreliable.

If I set trig to 281, and run:
Code: Select all
timer = ScheduleCallback(delegate { this.onTick(); }, trig);


Then I have onTick() running in 52 seconds...

This happens every time..

Why is this so unpredictable? Any ideas?

:(
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Setting timers.

Postby Oliver » April 15th, 2011, 11:34 am

I can't reproduce your error.

With this code:

Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;

namespace MyGame {
   public class Player : BasePlayer {
      public string Name;
   }

   [RoomType("MyCode")]
   public class GameCode : Game<Player> {
      // This method is called when an instance of your the game is created
      public override void GameStarted() {
         var now = DateTime.UtcNow;
         ScheduleCallback(delegate {
            Console.WriteLine( (DateTime.UtcNow - now).TotalMilliseconds  + "ms");
         }, 281);
      }
   }
}


I get this result:

Code: Select all
=====[  Room Started: MyCode  ]==========
>User bob (1): Joined game
280.0004ms


Which is really precise, only off by 1 ms.

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am


Return to C#



cron