ambiera home ambiera home

Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name:  Password:    
Ambiera Forum > CopperCube > Programming and Scripting
question about frame rate drop
Author Text

2017-11-17 23:59:57

Hi! I have a question.

Let's say I have a cube that moves in a certain direction every 1 ms either through javascript or with the built in functions, running at 40 frames per second like this...

Then let's say I use a 'do later' action to stop the movement after 1500ms. We then get a result like this.

But, let's say say so much stuff is happening in your game, that the frame rate drops down to a measly 10 frames per second. (extreme scenario) Well... then we get this result!!

So if you are moving objects around manually in your game, and your frame rate drops... expect disaster!
Why does this happen? And why does this NOT happen when I move an object using the 'move animated' check box? It's almost as if the 'move animated' check box has taken frame rate drop into account and works perfectly every time. Any ideas? Thanks!

Registered User
2017-11-19 07:19:01

Hello tim12345,

I guess your issue is related to a JavaScript counter issue.

I've faced before when creating my on screen map. whenever I was updating the character location on the map the fps was dropping very fast.

I've verified former scripts done and I've found the following lines that fixed my issue.

this.counter = 0; // I think this is to reset the timer counter.


two lines when doing the action/function

this.counter = this.counter+1;

and only do the action when the timer is changed

if (this.counter==1){

This has really improved the fps drop actually.

I'm wondering if someone of our scripting experienced users can explain this to us

2017-11-19 10:15:07

If you have less frames, than that function can be executed as many times as if you have a lot of frames, and since you are moving your object by a fixed length, this happens.

The built-in behaviors are measuring the time and move objects by the time which has gone, so they are frame-independent. You could do this too, in your javascript functions, if you like.

2017-11-19 19:13:04

thanks for the answers guys!

I think it is important for users to be aware of the difference between frame-dependent behaviors and frame-independent behaviors... especially when combining the 2, because you could run into some major problems as the frame-rate fluctuates.

for example: if you have a game that uses just a regular frame-independent fps camera provided by coppercube, and the frame rate drops or rises, it will still take just as long to move from point a to point b but will have "choppy" movement if the frame rate drops, or a smoother movement if the frame rate rises.

if you have a game that uses a very simple custom built camera that is frame-dependent, and the frame-rate drops or rises, the camera will slow down and take longer to get from point a to point b if the frame rate drops, and faster to get from point a to point b if the frame rate rises. You may have noticed this old NES games... where when too much stuff was happening at once, it would go into a "slow motion" like state.

since i like to combine the two, I will cap my frame rate, and be studying fixed timesteps, to see if I can find a solution. but I am a mere javascript simpleton... so this could take a while. i'll let you know if I find a solution!

Registered User
2017-11-20 10:40:56

I think there is also a point to consider with the "Do Later" function/script.

Let's say that when I go on proximity for an object A I'll display a 2D Overlay and when I go away from the object A I'll hide the 2D Overlay
and if the Overlay appears it will disappear after let's say 10 seconds.

So If you go near the object A the first time the overlay will appear and let's say within the 10 seconds you go far from the object A so it will hide the Overlay then let's say that you approach near the object again within the 10 seconds limit and there are 4 seconds remaining so the Overlay will hide after 4 seconds and if there is only 1 second remaining it will hide up

So I guess there should be a way to exit the Do Later function not sure how to do this ?

** I'm not 100% sure of what I say .. probably I'm right probably not **

2017-11-21 21:03:15

if you are suggesting a frame-dependent timer… excellent! that would work but only if the rest of the game was frame-dependent. so in that case instead of saying 'dolater' after so many seconds… you would say 'dolater' after so many frames. but I will be exploring how to move an object independent from frames.

but anyways...hum… i think to cancel the function you would have to use ccbUnregisterOnFrameEvent(this.registeredFunction);
within the function itself and some added variables i think… functions are currently my weakness. I will study them more!

2017-12-01 19:22:49

Update: Ohhhkaaay I have found a solution! It was very simple! I learned it by studying other coppercube .js files. Maybe nobody will find this useful, but I will share it anyways!

In order to move an object manually through the 'every few seconds' command independent of frames... I first created a behavior .js file with this code and put it in the extensions folder:

<behavior jsname="behavior_DeclareDeltaTime" description="Declare Delta Time">
< </behavior>
behavior_DeclareDeltaTime = function()
this.LastTime = null;
behavior_DeclareDeltaTime.prototype.onAnimate = function(node, timeMs)
var node1 = this.NodeToScale;
if (this.LastTime == null)
this.LastTime = timeMs;
return false;
var delta = timeMs - this.LastTime;
this.LastTime = timeMs;
if (delta > 200) delta = 200;
ccbSetCopperCubeVariable("delta", delta)
return true;

This creates a new coppercube behavior in the editor. Now I apply this to any object in the scene (doesn't matter which one) and all it does is calculate the delta value and send it off to a coppercube variable called "delta".

Then what I did was create an action .js file with this code and put it in the extensions folder:

/*  <action jsname="action_MovePositionByDelta" description="Move Position by Delta">
<property name="NodeToMove" type="scenenode" />
<property name="SpeedX" type="float" default="0" />
<property name="SpeedY" type="float" default="0.06" />
<property name="SpeedZ" type="float" default="0" /> </action>
action_MovePositionByDelta = function()
action_MovePositionByDelta.prototype.execute = function(NodeToMove)
var delta = ccbGetCopperCubeVariable("delta");
var oldPos = ccbGetSceneNodeProperty(this.NodeToMove, "Position");
newXspeed = this.SpeedX*delta;
newYspeed = this.SpeedY*delta;
newZspeed = this.SpeedZ*delta;
vecMove=new vector3d(newXspeed, newYspeed, newZspeed);

And what this does is calculates a frame independent speed using the delta coppercube variable...

Now there is a new action which looks like this...

Here we can choose an object to move and enter a speed value for xyx coordinates. The speed value should be very small... something like 0.06.

Alternatively, you can just use javascript to do this using basically the same code.

So now if I have a cube that moves in a certain direction every 1 ms either through javascript or with the new coppercube action, running at 40 frames per second like this...

And I use a 'do later' action to stop the movement after 1500ms. We then get a result like this.

And let's say say so much stuff is happening in your game, that the frame rate drops down to a measly 10 frames per second... well... now we get this result!!

Problem solved! Very simple! Maybe there is a simpler way, but I will use this method for the time being. Now if you'll excuse me! I have to make some repairs to my game...

2017-12-01 19:39:08

p.s. this line does not belong in the first set of code:
var node1 = this.NodeToScale;

i accidentally put it in there, you can delete it, it does nothing.

2017-12-02 05:54:00

Yes, that's also a nice solution :)

Registered User
2017-12-12 12:01:48

well done tim12345

I've got an idea as well to overcome the do later unwanted behavior

We can set a variable to watch while it is 1 for example do continue the timer loop if not then reach the end and exit the loop not sure yet how to achieve using Java Script

Create reply:

Posted by: (you are not logged in)

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




Forum Codes

Feature Code
Link [url] [/url]
Bold [b]bold text[/b]
Image [img][/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]



Software: Ambiera Light Forum. © 2008-2018 Nikolaus Gebhardt, Ambiera e.U.