Forum C# How would you translate this to C# ?

How would you translate this to C# ?

Postby whitershores » July 3rd, 2012, 8:59 pm

I am embarassed to ask this, as this is probably a really noob question, but I am also a noob in C#.

So how would you translate this to C# ?:

Code: Select all
_lastTimeUpdated = getTimer();   
gameTimer=new Timer(1000 / _timerSpeed);   
gameTimer.start();
gameTimer.addEventListener(TimerEvent.TIMER, gameloop, false, 0, true);


- This is obviously the main Gameloop timer. If there is a more preferred way to do this, perhaps with consideration for the fact that it is running on the server, also let me know!
whitershores
Paid Member
 
Posts: 88
Joined: June 21st, 2011, 4:19 pm

Re: How would you translate this to C# ?

Postby Henrik » July 3rd, 2012, 10:36 pm

http://playerio.com/documentation/refer ... e#AddTimer

So somewhere in the GameStarted method you do

Code: Select all
AddTimer(gameLoop, 1000);


And then somewhere else you can define gameLoop:

Code: Select all
private void gameLoop() {
    //Code goes here...
}
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How would you translate this to C# ?

Postby whitershores » July 6th, 2012, 5:23 pm

Thanks Henrik!

What about this bit? Getting the time since the app started in milliseconds ?
Code: Select all
_lastTimeUpdated = getTimer();
whitershores
Paid Member
 
Posts: 88
Joined: June 21st, 2011, 4:19 pm

Re: How would you translate this to C# ?

Postby Henrik » July 6th, 2012, 9:53 pm

Code: Select all
DateTime start = DateTime.Now;
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How would you translate this to C# ?

Postby whitershores » July 15th, 2012, 5:35 pm

This returns a Date object ??
Code: Select all
DateTime.Now


Code: Select all
DateTime.Now.Millisecond

- and this returns a Milisecond of the second between 0-999

This is not suitable to get the timedelta between updates??

I want to do this:
Code: Select all
var currentTime:int = getTimer();
var currentTimeDeltaInMiliSeconds:int = currentTime - _lastTimeUpdated;
whitershores
Paid Member
 
Posts: 88
Joined: June 21st, 2011, 4:19 pm

Re: How would you translate this to C# ?

Postby dreamora » July 15th, 2012, 5:51 pm

What are you trying to do in detail?
And why the delta aspect there, whats its detailed purpose?

Also do we talk about client or server side here? As flash does not exist on the server it seems we talk about client in which case the stuff is different.

I don't think the direct mapping there is really required for C#, depending on the platform in mind its not even possible.
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: How would you translate this to C# ?

Postby Henrik » July 15th, 2012, 7:01 pm

whitershores wrote:This returns a Date object ??
Code: Select all
DateTime.Now

It's a DateTime struct, but yes. :-)

whitershores wrote:This is not suitable to get the timedelta between updates??

This is how you calculate the time between two points in your code:

Code: Select all
DateTime start = DateTime.Now;

//Lots of processing here...

TimeSpan delta = DateTime.Now - start;
Console.WriteLine("Time spent: " + delta.TotalMilliseconds + "ms.");

A DateTime minus another DateTime results in a TimeSpan object. TimeSpan then represents a length of time, and has a lot of methods you can use to find out how long it is, just check out the docs:

http://msdn.microsoft.com/en-us/library ... espan.aspx
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How would you translate this to C# ?

Postby whitershores » July 16th, 2012, 2:40 pm

Hi Henrik!

Thanks thats pretty much what I was after.

However :)

Code: Select all
int time = Environment.TickCount;
//Lots of processing here...
int Delta = Environment.TickCount - time


- I've found this before you answered, but to my surprise, at least on my home computer this seems to return a DIFFERENT value than the method you described, even though they should both be returning the Milliseconds passed between?
Which one is more accurate? And which one is more accurate on the server?
whitershores
Paid Member
 
Posts: 88
Joined: June 21st, 2011, 4:19 pm

Re: How would you translate this to C# ?

Postby whitershores » July 16th, 2012, 2:45 pm

What about the StopWatch class?

csharp/stopwatch-whitelisted-t1758

Is this whitelisted by now ?


Btw I have done a test comparing all 3 ( Tick/DateTime/StopWatch) and they ALL returned a different value..
whitershores
Paid Member
 
Posts: 88
Joined: June 21st, 2011, 4:19 pm

Re: How would you translate this to C# ?

Postby Henrik » July 16th, 2012, 4:17 pm

whitershores wrote:- I've found this before you answered, but to my surprise, at least on my home computer this seems to return a DIFFERENT value than the method you described, even though they should both be returning the Milliseconds passed between?

Different how? The resolution of the system timers are about 15ms if I remember correctly.

What do you need your timing information for?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How would you translate this to C# ?

Postby Henrik » July 16th, 2012, 4:17 pm

whitershores wrote:What about the StopWatch class?

csharp/stopwatch-whitelisted-t1758

Is this whitelisted by now ?

What happens when you try to use some code with it in the development server?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm


Return to C#



cron