Calling event editor functions from JavaScript code

For developers using the Construct 2 Javascript SDK

Post » Thu Sep 17, 2015 7:53 pm

Hello, everyone.

I'd like to access a function from my plugin, not a JavaScript function but an event editor one.

Before I start to implement the idea does someone know if it's possible?

Thanks a lot!
B
48
S
14
Posts: 111
Reputation: 3,959

Post » Fri Sep 18, 2015 2:19 pm

Guys,

I did it and I want to share the solution in case someone asks the same question in the future.
I learned to do it by studying one of Rex.Rainbow's amazing extensions: "Command Queue"
so the code is all his. I made some comments in the code to simplify the understanding for
initiates.


Code: Select all
// Check if there is Function Object loaded in the project
if (cr.plugins_.Function != null)   
{
   // Iterate through all names of the project's Object Types
   for (name in this.runtime.types)
   {
      // We only want the first instance of the Function plugin, because
      // there is only one in the project (plugin type is "object" not "world")
      var inst = plugins[name].instances[0];
      
      // Check if we found the Function object's single instance
      if (inst instanceof cr.plugins_.Function.prototype.Instance)
      {
         // Store the instance and the action in variables
         // The instance will be passed to the CallFunction when we call it
         this.functionObjInstance = inst;
         this.functionCallFunction = cr.plugins_.Function.prototype.acts.CallFunction;
         break;
      }
   }
}


You can get the conditions and expressions as well. I'll only use the action "CallFunction" in my plugin.
Then you can call the function stored in the variable.

Code: Select all
// Then later in our code we are able to call "CallFunction" and pass the instance as
// the first parameter, the function name as the second and the parameter list as the
// third parameter
this.functionCallFunction.call(this.functionObjInstance, "fn_Example", "");


Thank you, rex, for your amazing extensions and for the code to be so easy to learn from.

Best regards to all!
B
48
S
14
Posts: 111
Reputation: 3,959

Post » Sat Sep 19, 2015 1:30 am

Javascript integration
Code: Select all
if (c2_callFunction)
    c2_callFunction("name", ["param1", "param2"]);
B
109
S
27
G
278
Posts: 4,483
Reputation: 155,426

Post » Sat Sep 19, 2015 5:46 am

@rexrainbow: Thank you. Now I feel silly. I guess my effort was all in vain. Well, maybe not totally in vain because I learned a lot from your code and of javascript in general. [EDIT] I will RTFM before asking more questions. That's only fair. Thanks again for your enormous contribution. :)
B
48
S
14
Posts: 111
Reputation: 3,959


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests