Delay draws in Plugins

For developers using the Construct 2 Javascript SDK

Post » Mon Oct 28, 2013 11:00 pm

If I have a plugin that draws an object to the screen with every tick, I want there to be a way to delay the drawing by a configurable item.

So if I want it to draw l only every 1 second, there would be a 1000 millisecond variable I'd set or something similar.

It has to be done in the plugin and not through Waits in C2.

It seems from my research that by design the JS for plugins is ran every draw just like anything else in C2.

I'm looking for best practices around this. I'm looking to extend some WebGL enabled stuff as well, so any advice is appreciated as this is not my normal territory.

I've tried adding a sleep function or a function that checks for if the current clock time is 1 second later than when it ran last. Seems not to work right. Maybe I'm not looking at the right functions.

B
30
S
10
G
7
Posts: 416
Reputation: 7,239

Post » Mon Oct 28, 2013 11:14 pm

I've no idea the context of what you're doing, but setTimeout waits a number of milliseconds before executing a function. E.g.
[CODE]setTimeout(function(){alert("Hello")},3000);[/CODE]
Moderator
B
72
S
14
G
11
Posts: 900
Reputation: 11,920

Post » Mon Oct 28, 2013 11:26 pm

Lets use the spritefont type as an example.

It has a couple of draw functions for edittime and runtime. I can't mess with those because C2 needs those functions to be left alone correct?

I guess my questions is some more insight into what those functions do and how they are called. I understand all the JS inside them, but not how they are called.
B
30
S
10
G
7
Posts: 416
Reputation: 7,239

Post » Tue Oct 29, 2013 3:56 am

The sdk manual describes what they do here:
https://www.scirra.com/manual/23/runtime-functions
under "draw(ctx) and drawGL(glw)"

And it's the runtime that calls them. For more detail: in preview.js the runtime calls the layout draw function in layout.js whenever a frame is to be drawn. From the layout draw function each layer draw function is called (same file) and from there each instance's draw function is called.

You can't control when the plugin's draw functions are called but you don't have to draw anything when they are. It's quite doable to do as you mentioned to wait a time interval between draws by saving the current time and comparing against it every time the draw funtion is called. However it would only cause your object to flash one frame before the background and other objects would overdraw it after that until it flashes again.

Here's how you could do it.
1. add a variable to instanceProto.onCreate so you can save the current time.
[code]this.oldTime = 0;
this.timeInterval = 1; //in seconds[/code]
2. add this to the top of your draw and drawgl functions:
[code]var currentTime = this.runtime.kahanTime.sum;
if (currentTime >= this.timeInterval + this.oldTime)
    this.oldTime = currentTime;
else
    return;
// add drawing code below[/code]

But as I said you'd only get a passive flash of the object every interval.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Tue Oct 29, 2013 6:11 pm

Awesome, I didn't even know that kind of stuff was in the manual, :D

I'll let you know the outcome.
B
30
S
10
G
7
Posts: 416
Reputation: 7,239


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 1 guest