Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
time counter

coa
Registered User
Quote
2022-06-18 14:00:19

Hi im trying to make a time based counter that adds 1 each sec that is not affected by lag.?


coa
Registered User
Quote
2022-06-18 14:05:09

I tried this code i made but i get error and i cant find it

error is: SyntaxError: missing ) after condition


function count()
{
var now = (new Date()).getTime();

startTime = (new Date()).getTime();
endTime = startTime + 1;


while(now!>endTime){
var now = (new Date()).getTime();
}
count = ccbGetCopperCubeVariable("timeshoot");
count=count + 1;
ccbSetCopperCubeVariable("timeshoot", count);
}
count();



sven
Registered User
Quote
2022-06-18 14:20:14

while(now!>endTime){

You cant use "!" there also there is no need for that because if you want to run while until now is smaller than endtime then you can use just < instead of > or <=

https://www.w3schools.com/js/js_...


coa
Registered User
Quote
2022-06-18 14:24:13

yes i found the bug but the script does not do the job


sven
Registered User
Quote
2022-06-18 14:34:21

wrote:
yes i found the bug but the script does not do the job


this is not a good way to do that because your function will freeze the game while in while loop -it does the job but not the way it would be useful.

Its a good way to learn javascript anyways.


coa
Registered User
Quote
2022-06-18 14:39:29

Well im always learning trying to figure this out.


sven
Registered User
Quote
2022-06-18 14:45:21

You can check out this project i made while ago..
there i use timers and this may give ideas:

First overlay displays seconds and game framerate shouldnt affect it.

https://5v3n.itch.io/coppercube-...

it has behaviour that it uses behavior_GTime


hadoken
Guest
Quote
2022-06-18 14:49:32

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);
}




just_in_case
Moderator
Quote
2022-06-18 14:53:21

If you want you can simply use every few seconds do something behavior and set 1000ms as the time and can then set to change a variable there, for example
variable name = Timer;
operation = Add;
value = 1;

also make sure to set this variable to zero before first drawing.

Now what this will do is it will add a value to the timer every 1 second.

For doing it with code.

You wanted to achieve something like do something later action.

I don't know where you are executing your code. So can't tell much.
But you need to have either an OnFramevent or OnAnimate prototype if working inside a behavior.

First you need to set your startTime and endTime[b] outside the onframe or onAnimate function.

and then inside you onAnimte or onFrame event you need to compare it with the currentTime [b]now
if the "now" is greater than your endtime you can increase the counter by +1.


var startTime = (new Date()).getTime();
var endTime = startTime + 1000;

function onFrameDrawing()
{
var now = (new Date()).getTime();
if(now > endTime)
{
count = ccbGetCopperCubeVariable("timeshoot");
count = count + 1;
ccbSetCopperCubeVariable("timeshoot", count);
}

}


something like this should work. Hope that helps


hadoken
Guest
Quote
2022-06-18 14:53:36

Sorry for my previous post which increases every cycle, I think I've overlooked "every sec".
Of course, you still would have to put my result in relation to passed seconds in time


just_in_case
Moderator
Quote
2022-06-18 14:54:50

Ahaan, What a timing @hadoken, You commented while I was typing my response. @Coa, if you want you use the behavior posted by @hadoken as well.


coa
Registered User
Quote
2022-06-18 16:09:16

thanks all for your help.

I needed it to add 1 each 0.1 sec so your script hadoken did the trick


coa
Registered User
Quote
2022-06-18 19:00:19

Here is what i made and use from all your help
but i have one problem i cant fix i was trying to add <property name="timems" and i would only get 0 when calling this.timems variable what im i doing wrong
/*
<behavior jsname="behavior_count" description="behavior_count">
<property name="timems" type="int" default="1000">
</behavior>
*/


timems=1000;

behavior_count = function()
{
var now = (new Date()).getTime();
this.endTime = now + timems;

}

behavior_count.prototype.onAnimate = function()
{
var now = (new Date()).getTime();
if(now > this.endTime){

count = ccbGetCopperCubeVariable("time");
count = count + 1;
ccbSetCopperCubeVariable("time", count);
this.endTime = now + timems;
}

}



coa
Registered User
Quote
2022-06-18 19:30:25

i found out why it did not work


coa
Registered User
Quote
2022-06-18 19:38:07

here is the final code free to use and modify if you want
it has property value timems that you can change in the cc editor

if you dont know you need to save the code in text editor as the same name of the behavior


/*

<behavior jsname="behavior_count" description="behavior_count">
<property name="Timems" type="float" default="1000" />
</behavior>
*/


behavior_count = function()
{


var now = (new Date()).getTime();
this.endTime = now + 1;


}

behavior_count.prototype.onAnimate = function()
{


var now = (new Date()).getTime();
if(now > this.endTime){

var count = ccbGetCopperCubeVariable("timeshoot");
count = parseInt(count);
count = count + 1;

print(count);
ccbSetCopperCubeVariable("timeshoot", count);
this.endTime = now + this.Timems;

}

}









Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inter?ational" (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