Forum C# Timer

Timer

Postby avol » December 22nd, 2010, 2:10 pm

Hello,

How can I pass AddTimer reference to other class.
For example I initialise a new class from main GameCode class like this:
engine a = new engine(reference);
So I could use AddTimer in other class.
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm

Re: Timer

Postby TobiasW » December 22nd, 2010, 2:32 pm

Hey there,

you would pass a reference to your GameCode instance, so:
Code: Select all
Engine a = new Engine(this);


And in your engine you can access it like this:
Code: Select all
public class Engine {
     public Engine(GameCode game) {
          game.AddTimer(...);
[...]


Hope that helped,
Tobias
User avatar
TobiasW
 
Posts: 59
Joined: August 29th, 2010, 12:31 am

Re: Timer

Postby avol » December 22nd, 2010, 6:14 pm

I tried using this keyword, but it throws error:

Error 1 Keyword 'this' is not available in the current context D:\Users\Avol\Desktop\game\Serverside Code\Game Code\Game.cs 25 54 Game Code.VS2010

my code part looks like this:

public class GameCode : Game<Player> {
public Player[] players = new Player[45];
public npc_engine NpcEngine = new npc_engine(this);
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm

Re: Timer

Postby TobiasW » December 22nd, 2010, 8:47 pm

You'll need to create/assign it in the GameCode constructor - which you probably have to create first.
User avatar
TobiasW
 
Posts: 59
Joined: August 29th, 2010, 12:31 am

Re: Timer

Postby avol » December 22nd, 2010, 9:58 pm

thanks, that helped :)
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm

Re: Timer

Postby TobiasW » December 23rd, 2010, 12:17 am

Great!

Oh, and by the way, you probably don't have to create your players array - Game<P> already has a property "Player" which gets automatically updated.
User avatar
TobiasW
 
Posts: 59
Joined: August 29th, 2010, 12:31 am

Re: Timer

Postby avol » December 23rd, 2010, 9:59 am

thanks, thats helpfull too :)
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm

Re: Timer

Postby avol » December 26th, 2010, 4:30 pm

one more thing, how do I check is timer running?
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm

Re: Timer

Postby TobiasW » December 26th, 2010, 7:14 pm

avol wrote:one more thing, how do I check is timer running?

As far as I know, you can't. Why do you want to do this?
User avatar
TobiasW
 
Posts: 59
Joined: August 29th, 2010, 12:31 am

Re: Timer

Postby avol » December 26th, 2010, 9:00 pm

I have few timers who need to work together somehow.

I have a movement_interval Timer which is running. ( moves a target from one random place to other )
And attacker_interval which will be ran when a player attack target, ( findsa target to go next to and attack it )

So when attacker_interval starts running, movement_interval stop working and after attacker_interval stopped in starts again. I don't want to link them together becouse they are in different classes and different parts of code ( My OOP will be messy ).

So I came to a solution to make attacker_interval null - this is how I check is it running. ( Ofcourse I stop it before making null.)
avol
 
Posts: 11
Joined: July 24th, 2010, 11:00 pm


Return to C#