Author Topic: TNCounter Issues (Ticks & IBinarySerializable)  (Read 5116 times)

Proton

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
  • Alberta, Canada
    • View Profile
    • Time Gamers
TNCounter Issues (Ticks & IBinarySerializable)
« on: February 09, 2017, 11:36:48 PM »
I'm still on 3.0.6, but looks the same in 3.0.7.

TNCounter.cs
var time = System.DateTime.UtcNow.Ticks * 0.0000001;

But there are 10,000 ticks per ms, so this should be:
var time = System.DateTime.UtcNow.Ticks * 0.0001;
Reference: https://msdn.microsoft.com/en-us/library/system.timespan.tickspermillisecond(v=vs.110).aspx

This causes the counter to get the wrong value the first time it is used on the client after being sent from the server.

It was tricky to figure out since it doesn't happen if you are in the room when a packet gets sent with a Counter in it (because the packet is just forwarded directly), bit it will happen if it is one that was saved on the server since the server serialization causes this issue.

It's also nice to have a ToString for TNCounter (helped with debugging & looks nice in the TNObject Unity inspector):
  1.     public override string ToString() {
  2.         return string.Format("{0:f2} [{1},{2}] {3}/sec", value, min, max, rate*1000);
  3.     }
   
   
I also had to modify TNSerialier.WriteObject:
else if (obj is IBinarySerializable && (obj is Counter)==false)

Otherwise the Counter just gets serialized like IBinarySerializable instad of using the special efficient GetPrefix identifier (24). Somehow IBinarySerializable didn't work either, perhaps because I had the type fully qualified as TNet.Counter, not sure didn't investigate that further.

Now everything seems to be working nicely :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNCounter Issues (Ticks & IBinarySerializable)
« Reply #1 on: February 13, 2017, 11:43:58 AM »
Hmm... you're right. It also shouldn't be a double... I think I copied it from somewhere that expected a value in seconds instead of milliseconds. I will make the change, thanks! The serialization change too, thanks again!

Proton

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
  • Alberta, Canada
    • View Profile
    • Time Gamers
Re: TNCounter Issues (Ticks & IBinarySerializable)
« Reply #2 on: February 13, 2017, 10:32:15 PM »
Yeah, I also changed 'mTimestamp' from double to long.

The other change was to make the 'rate' in seconds instead of milliseconds. It seems much more natural to think of a counter/timer in change/sec. Kind of a hacky way, but I just changed the constructor to use 'this.rate = rate * 0.001;'. I never change rate directly after that, just replace the entire counter if I want to change it. I have the luxury of ignoring edge cases :P

So far we've used TNCounter as a network synchronized timer for the boss, he regenerates his health after the timer runs out (the blue bar): https://gfycat.com/SandyHandyIcelandicsheepdog

Using TNCounter is really efficient and makes the code easy to follow. I think it will be great for active abilities too!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNCounter Issues (Ticks & IBinarySerializable)
« Reply #3 on: February 18, 2017, 07:45:30 PM »
mTimestamp being long is right too, yes. This also affects serialization as it was storing it as a double before. I've made all the changes on my end for the next update.

I'm glad you found it useful already. :)

I'm curious about your game too. Can you tell me more about it?

P.S. The rate should already have been in seconds -- the delta should be converted when you retrieve the value:
  1.         public double value
  2.         {
  3.                 get
  4.                 {
  5. #if STANDALONE
  6.                         var time = System.DateTime.UtcNow.Ticks / 10000;
  7. #else
  8.                         var time = TNManager.serverTime;
  9. #endif
  10.                         if (mTimestamp != time)
  11.                         {
  12.                                 var delta = (time - mTimestamp);
  13.                                 if (delta < 0) delta = 0;
  14.                                 mTimestamp = time;
  15.                                 mValue += delta * 0.001 * rate; // <---
  16.                                 if (mValue < min) mValue = min;
  17.                                 else if (mValue > max) mValue = max;
  18.                         }
  19.                         return mValue;
  20.                 }
  21.                 set
  22.                 {
  23.                         mValue = value;
  24. #if STANDALONE
  25.                         mTimestamp = System.DateTime.UtcNow.Ticks / 10000;
  26. #else
  27.                         mTimestamp = TNManager.serverTime;
  28. #endif
  29.                 }
  30.         }

Proton

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
  • Alberta, Canada
    • View Profile
    • Time Gamers
Re: TNCounter Issues (Ticks & IBinarySerializable)
« Reply #4 on: February 23, 2017, 04:19:31 AM »
I'm curious about your game too. Can you tell me more about it?

We're working on Time Warpers, it's a sequel to Time Clickers which is in the 'cookie clicker' genre. Time Clickers started out as an adver-game for Time Rifters, but turned out to be even more popular than the game it was advertising :P. So Time Warpers combines the best aspects of both games + multi-player. Something like an incremental Borderlands.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNCounter Issues (Ticks & IBinarySerializable)
« Reply #5 on: February 24, 2017, 08:59:26 AM »
Nice, I really like the lighting in it. And multiplayer will make any game so much better, that's for sure. Even a mediocre game can gross millions with stable multiplayer. Not that I know from experience or anything... <_< *cough*