Use Function.Call from my plugin; how?

For developers using the Construct 2 Javascript SDK

Post » Sun Apr 28, 2013 8:31 am

I'm tinkering with a plugin of course. What I'm wondering is how to do I effectively call

Function.Call("foo", var, var)

from my own plugin.
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Sun Apr 28, 2013 4:14 pm

- Get function handler
[code]this._call_fn = cr.plugins_.Function.prototype.acts.CallFunction;[/code]
- Get "this"
[code]this._official_fnobj = plugin.instances[0];[/code]
- Call "on function"
[code]this._call_fn.call(this._official_fnobj, name, params);[/code]
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Sun Apr 28, 2013 4:55 pm

I would advise against any sort of plugins-calling-other-plugins integration like this, because it's bad design. In general a plugin should be a self-contained unit of purpose, and not depend on or use any other plugins outside of itself.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sun Apr 28, 2013 5:31 pm

@Ashley

It has some trade-off about choosing a self-contained or a dependency plugin.
- plugin designer might create a new plugin(or behavior) copied from official plugin because that he/she want to add only one action into this official plugin.
- A self-contained is easy to use for beginner. But user might need to learn the same interface once again at other plugin (or behavior).
For example, user had already known how to use official function object, then user need to learn the same interface in 3rd plugin once again.
- the plugin will become very large if it duplicate all of dependency and hard to maintain, more bugs.

Here is my board plugin system. I agree that it has a learning gap for beginner. Each plugin has it's own job, user might learn a new plugin member easily when he/she already understand how to use the core plugin(board plugin).


Dedicate condition/action/expression is more easy to read at some cases. In these case, I will chose to have a dedicate ACE.


Edit:
I thought a well-organization architecture is the most important thing. Easy to maintain, more readable, and more extend-able.

A self-contained plugin might not be the best choice sometimes, imo.rexrainbow2013-04-29 04:35:49
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Mon Apr 29, 2013 5:06 am

I'm going to agree with Rex. The Function plugin is a fantastic extendible worthy plugin. Using C2 Function plugin as a callback form for users makes it easier for users to understand and integrate. Requiring to now create a version of Function in my own code means that my code is now larger and requires the other developers to not take advantage of already familiar official plugins.

I get both sides of the argument. I've seen Plugins in others systems like jEdit that can have layers upon layers of dependants. Where the user may only want 1 plugin they end up installing 6/7.

yet on the other hand it requires the developers of the plugin to write all the extra code that may already be developed. Increasing memory usage, chance of performance reduction.

Rex makes a case for the whole. However, Function is not considered an optional aspect of programming. Function/Method/Subroutines are staple of software development and making the C2 Function plugin wrong to use seem a little strange. However, if that's the stance your going to stick too I'll work around.

I suppose I can just copy/paste the Function plugin into a new plugin and build on top.

I appreciate the replies. Thanks Rex, I'll try that out. Don't know if I will stick to it due to the fact that might break someday :P
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Mon Apr 29, 2013 5:20 am

@jayderyu

I would not use the official function plugin to be the callback for special purpose, except the callback is only a general purpose function call.
A dedicate condition/action/expression is better, more read-able.
For example, a callback to get a value from event sheet (a use defined value) triggered by plugin. It could be done by a (official) function call and (official) set return value. However, it is better to have a dedicate condition "on get value" and dedicate action "set value". More over, the parameters of this callback could be passed by dedicate expression:CurState (just an example).


Edit:

Event sheet.
[code]+ Myplugin:on get value
    + Myplugin.CurState = 1
        - Myplugin: set value to 3
    + else
        - Myplugin: set value to 0[/code]

Plugin.
[code]// condition: on get value
this.curState = 3;
this.value = 0;
this.runtime.trigger(cr.plugins_.Myplugin.prototype.cnds.OnGetValue, this);

// action: set value
Acts.prototype.SetValue = function (value)
{
    this.value = value;
};

// expression:CurState
Exps.prototype.CurState = function (ret)
{
    ret.set_int(this.curState);
};[/code]
rexrainbow2013-04-29 05:46:18
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Mon Apr 29, 2013 5:50 am

I do agree. I prefer a dedicated coded cnd. What I'm trying to create is a C2 event listener where the developer defines there own event's. The Event being named a string.

I don't technically need to use Function, but I thought it would be far more elegant and appropriate to use an already established plugin. The alternative is a switch statement, which doesn't exist or a more bulky form

OnCustomEvent
- if EventName = "shoot" -> Function.Call("function0", data)
- if Eventname = "move" -> Function.Call("function1", data)

I had just thought I could get a way with
On Start Layout
-> CustomEvent.RegisterCallback("function0", "shoot")
-> CustomEvent.RegisterCallback("function1", "move"

getting a number isn't really needed for a callback purpose. The idea would be for data processing and triggering based on structured data. Ajax based turn games. where lists of turn actions. Having the option to get a Function to be triggered for the action type of a turn just makes some sense.

structure
Player, Action, Unit, Data
0, Move, 6, [6,4]
0, Shoot, 4, 20
1, Cover, 9, object12
...


Anyways, This was all about tinkering. As there is no official support I will probably just leave it to just tinkering :D

Thanks for the info :)
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Mon Apr 29, 2013 6:07 am

@jayderyu

Uh, do you mean that your plugin is used to communicate to each other client by passing "function object (function name and parameters)"?

I had made a similar plugin before, and I call these functions through rex_function plugin (the official function had not ready at that time).
It is a case of using general purpose function, imo. (not to get the user defined value)

For example, in event sheet.
[code]// Myplugin: action of passing "function object structure",
// send this structure (JSON format) to server
- Myplugin: call "run" (1, 3)

// Myplugin receive this structure (JSON format) from server,
// trigger the official function object
+ on function "run"
+ function.param(0) = 1
...[/code]

Plugin users could define their own "function object structure", and the handler at event sheet.rexrainbow2013-04-29 06:23:30
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Mon Apr 29, 2013 10:24 am

@jayderyu

Here is an example (worksheet) of calling official function object at 3rd plugin. This worksheet plugin will call these official functions at specific delay time, so the input of this worksheet plugin is a string which defined the delay time, function name, parameters.
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Mon Apr 29, 2013 3:46 pm

I like the worksheet plugin. It's a nifty timing based plugin. Good for scripting. That could actually work well in a turn based action parsing system. Thanks for the link.

B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests