Delay draws in Plugins

0 favourites
  • 5 posts
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I've no idea the context of what you're doing, but setTimeout waits a number of milliseconds before executing a function. E.g.

    setTimeout(function(){alert("Hello")},3000);
  • 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.

  • 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.

    this.oldTime = 0;
    this.timeInterval = 1; //in seconds

    2. add this to the top of your draw and drawgl functions:

    var currentTime = this.runtime.kahanTime.sum;
    if (currentTime >= this.timeInterval + this.oldTime)
        this.oldTime = currentTime;
    else
        return;
    // add drawing code below

    But as I said you'd only get a passive flash of the object every interval.

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

    I'll let you know the outcome.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)