Calling C2 function from plugin

For developers using the Construct 2 Javascript SDK

Post » Sat Nov 29, 2014 7:09 pm

Hi,

Is it possible to program a plugin so that it gets the name of a C2 function passed, and then calls that function, including passing of parameters to the function and retrieving return values?

If so, is there documentation or examples how to do this?

thanks,

Dan
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Sat Nov 29, 2014 7:12 pm

I just noticed that Function is actually a plugin.

So, it should be possible to retrieve all relevant function plugin instance data from another plugin, and thereby refer and also call functions ....
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Sat Nov 29, 2014 8:19 pm

Here's a snippet to look at:

Acts.prototype.SequencerAddSynchronizer = function (callbackName)
{
assert2(cr.plugins_.Function != null, "The 'BHT Step Sequencer' plugin requires the 'Function' plugin.");

// Check that the Function plugin exists.
if (cr.plugins_.Function != null)
{
var callFnc = cr.plugins_.Function.prototype.acts.CallFunction;

var name, plugin;
var runtimeTypes = thisInstance.runtime.types;
for (name in runtimeTypes)
{
plugin = runtimeTypes[name];
if (plugin.plugin.acts.CallFunction == callFnc)
{
this.Official_fnobj = plugin.instances[0];
}
}
}
this.CallbackSynchronizer = callbackName;
}

Acts.prototype.SequencerRemoveSynchronizer = function (callbackName)
{
this.CallbackSynchronizer = null;
}

Acts.prototype.SequencerClear = function ()
{
this.Stop();
this.Clear();

if (this.CallbackSynchronizer != null)
{
var params = [-1, -1, ""];
cr.plugins_.Function.prototype.acts.CallFunction.call(this.Official_fnobj, this.CallbackSynchronizer, params);

var fake_ret = {value:0,
set_any: function(value){this.value=value;},
set_int: function(value){this.value=value;},
set_float: function(value){this.value=value;},
set_string: function(value){this.value=value;},
};
cr.plugins_.Function.prototype.exps.ReturnValue.call(this.Official_fnobj, fake_ret);
console.log(fake_ret);
}
}
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Sun Nov 30, 2014 6:59 am

Of course, see the manual of function plugin, "Javascript integration" section.
B
109
S
27
G
278
Posts: 4,483
Reputation: 155,426

Post » Sun Nov 30, 2014 7:10 am

Thanks Rex,

what I am thinking about is to create support a simple MapReduce function: https://code.google.com/p/mapreduce-js/

With the C2 functions passed as parameters.

I wonder if this can be done. Perhaps its already doable with your javascript function plugin

Dan
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Sun Nov 30, 2014 7:33 am

@grossd

I guess it had not be made yet in this forums.
B
109
S
27
G
278
Posts: 4,483
Reputation: 155,426

Post » Sun Nov 30, 2014 8:01 am

@Rex,

It now occurred to me that it should be pretty easy to rewrite the short MapReduce algorithm with C2. C2 has the ability to pass functions to function, as strings, and have the called. So, beside the neat packaging as a plugin, there seems no need to actually write a plugin for that. A separate event sheet should suffice.

Dan
B
8
S
4
G
1
Posts: 205
Reputation: 1,354

Post » Sun Nov 30, 2014 8:25 am

@grossd

It depends on you.
In my point of view, "MapReduce" might not be easy to understand or to be used in real cases for normal users.
B
109
S
27
G
278
Posts: 4,483
Reputation: 155,426

Post » Sun Nov 30, 2014 8:33 am

Perhaps you are right,

In my case I have a following structure (All are dictionaries, and "varying game section" stats an Array, say, someone drops something X times during a game section, and i capture when (in seconds since game section start) each drop happened):

User-Groups->Users->UserGamePlayedDate-Time>TotalGameStats->GameSectionsStats->VaryingGameSectionStats

I need to analyze this data from various perspectives, such as improvements over time of individual users; average comparisons between user goups, etc. and graph it using charts (I am using the canvas chart plugin).

Programming everything individually with loops can be pretty tedious. I am hoping that a map reduce implementation will make the code shorter and simpler.

Dan
B
8
S
4
G
1
Posts: 205
Reputation: 1,354


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 1 guest