Plugin-wide oncreate function

For developers using the Construct 2 Javascript SDK

Post » Tue Jun 24, 2014 6:12 pm

What is the best way to implement a function just once when the plugin loads, regardless of the number of classes and instances?

For example,

I want to load the facebook api just once. However multiple instances will be used to place 'like' boxes..........., so I can't place the init code under the classes or instance oncreate(would load it multiple times)
www.h1k3.tech

We are here for you!
B
87
S
22
G
16
Posts: 715
Reputation: 16,785

Post » Tue Jun 24, 2014 6:57 pm

You could just create a function and a var. And when an instance is created check of the var is true. If not then execute the function and set the var to true so the next time an instance is created the function wont be called again. Still requires at least one instance to be created but it will only be triggered once.

Code: Select all
var runonce = false;

function Myfunction(){
 //do something
}

instanceProto.onCreate = function(){
    if(runonce==false){
         runonce = true;
         Myfunction();
    }
}
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Tue Jun 24, 2014 7:00 pm

Roflmbo, ohhh my god. Face-palm lol thank you. I was too close to the issue, I was way overthinking it.
www.h1k3.tech

We are here for you!
B
87
S
22
G
16
Posts: 715
Reputation: 16,785

Post » Tue Jun 24, 2014 7:06 pm

and here i was thinking i completely miss read the question because it seemed to easy.. haha... we all have those moments :)
B
20
S
7
G
1
Posts: 221
Reputation: 2,077


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests