Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
time counter

hadoken
Guest
Quote
2022-06-18 21:27:06

Meant as support for @coa and emerged at the same time when he had found a solution himself posting anyway an additional alternative also counting the CopperCube variable "time" by adjustable count rate in milliseconds plus you can set an initial value from where to start to count:

/*
<behavior jsname="behavior_HadokenCCVarCount" description="behavior_HadokenCCVarCount">
<property name="CountRate_Ms" type="int" default="1000">
<property name="CC_var_time" type="string" default="10">
</behavior>
*/

behavior_HadokenCCVarCount = function()
{
this.init = 0;
}

behavior_HadokenCCVarCount.prototype.onAnimate = function()
{
////////////////////// executing initially only once:
if(this.init == 0)
{
this.time = Math.round(this.CC_var_time);
ccbSetCopperCubeVariable("time", this.time);
this.count = ccbGetCopperCubeVariable("time");
this.now = (new Date()).getTime();
this.endTime = this.now + this.CountRate_Ms;
this.init = 1;
}
//////////////////////

this.now = (new Date()).getTime();

if(this.now > this.endTime)
{
this.count += 1;
ccbSetCopperCubeVariable("time", this.count);
this.endTime = this.now + this.CountRate_Ms;
}

// debug:
print("CopperCube variable ' time ' = " + ccbGetCopperCubeVariable("time"));

}



coa
Registered User
Quote
2022-06-18 23:40:20

thanks again Hadoken I learned alot about making behaviors making this feature


okeoke
Registered User
Quote
2022-06-19 06:50:20

Hi,

I don't think this solution will really work with lag. In fact, I don't also think that seconds are the real seconds here:)

I believe, onAnimate function is executed on screen draw, which in normal case happens every ~17 ms. Check this part:

if(this.now > this.endTime)
{
this.count += 1;
ccbSetCopperCubeVariable("time", this.count);
this.endTime = this.now + this.CountRate_Ms;
}


So let's say we start at 0 ms for simplicity (even though it's actually int representation of the current local time):
1. On the first run this.now is 0 and the code is not executed
2. On the second run it's 17 and nothing happens still
3. On 60th run it's 1020 ms, and now it enters the if block. On the last line of it, we're adding another countRate value (which is 1000) and get 2020ms - so our second is shifted by 20 ms in the first loop. If expect a precise second to count - condition should be "this.now === this.endTime". If you lag, which means your framerate drops, or run 30 fps this shift will increase even more.

From the other hand, if your application froze for 1000 ms this second will be lost from the counter because of this.now + this.countRate_ms. You can easily test this by alttabing from game and alttab back then.

I guess, on the last line you should do this.endTime += this.CountRate_Ms;

Or why not to use a built in timer, by just assigning the difference between game start and current time in seconds to some variable, like:
behavior_timer = function () {
this.initTimeMs = null;
};

behavior_timer.prototype.onAnimate = function (node, timeMs) {
if (this.initTimeMs !== null) {
this.initTimeMs = timeMs;
return;
}
ccbSetCopperCubeVariable('timer', Math.floor((timeMs - this.initTimeMs) / 1000));
}



coa
Registered User
Quote
2022-06-20 19:15:54




gree
Registered User
Quote
2022-11-01 17:39:34

can anyone tell what the "0.62" do in this snippet from @hadoken?

What's the main difference between hadokens 1st code compaired to the latest version above (also from hadokens)?
I got good results with a custom version of this one, not sure tho how they differ...

Thanks in advance.



hadoken wrote:
this one should do the job:

/*
<behavior jsname="behavior_HadokenDeltaTimeAsCounterDemo"description="behavior_HadokenDeltaTimeAsCounterDemo">
</behavior>
*/

behavior_HadokenDeltaTimeAsCounterDemo = function()
{
this.counter = 0;
this.init = 0;
}

behavior_HadokenDeltaTimeAsCounterDemo.prototype.onAnimate = function()
{
this.currentTime = new Date().getTime();
if(this.init == 0)
{
this.lastTime = this.currentTime;
this.init = 1;
}
this.DeltaTime = (this.currentTime - this.lastTime) * 0.062;
this.lastTime = this.currentTime;

this.counter += Math.round(this.DeltaTime);
print("this.counter = " + this.counter);
}




hadoken
Guest
Quote
2022-11-01 18:30:24

@gree

under normal circumstances in the given example by multiplying delta time (this.DeltaTime) by an extra 0.062 you get a nice value of about 1 for good use regarding fps and counter increase ... think that's all


gree
Registered User
Quote
2022-11-01 18:32:46

@hadoken
thanks for the note. I've used some bits and bites from your script. It kinda works for me. Thanks again. Without I would have been lost :)


gree
Registered User
Quote
2022-11-03 19:01:53

@hadoken
just to clear things up a little bit for myself; the "extra 0.062"....speaking of "exact" this is like "non-exact" by 0.062ms?
So, this way the time orients to be as close to a frame; but the timing isn't correct anymore; because it's shifted by 0.062ms?

I'd rather prefer exact timing than any frame relationship. Or is this "relation" needed?


I'm also having troubles outputting multiple-Deltatimes as 'sum' within the console.log. It "print" outputs correct; but in he console.log I'm getting 'NAN'.

Example:
works in console.log + print:
this.counter = this.DeltaTime/100;

does work in print output but not in console.log:
this.counter += this.DeltaTime/100;


any idea why this reacts kinda weird?
Thanks.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Int?rnational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact