Extending Construct 2 with Javascript

Discussion and feedback on Construct 2

Post » Tue May 31, 2011 1:06 am

Is there any way right now for expressions to have parameters? As in:

plugin.expression(parameter)
B
51
S
10
G
7
Posts: 184
Reputation: 6,825

Post » Tue May 31, 2011 1:07 am

Is it not working? I thought I coded support for that!
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Tue May 31, 2011 1:28 am

[quote="Ashley":2z3jwi5j]Is it not working? I thought I coded support for that![/quote:2z3jwi5j]

Actually, I was asking how to if there was. There aren't any plugins that I know of currently that use it, and I'm mostly learning everything through studying other plugins.
B
51
S
10
G
7
Posts: 184
Reputation: 6,825

Post » Thu Jun 02, 2011 8:40 pm

[quote="Ashley":1m86do8j]The HTML5 exporter in Construct 2 includes Google's V8 Javascript engine. This means both the editor and runtime parts of plugins can be written in javascript. This makes it much, much easier to extend Construct - previously you had to have relatively good knowledge of C++, which is a difficult language to learn[/quote:1m86do8j]

I think C++ is easier than Javascript :|
B
5
S
2
G
8
Posts: 53
Reputation: 3,222

Post » Sat Jun 04, 2011 2:37 pm

To have parameters in plugin actions, conditions or expressions, call these before the related AddAction(), AddCondition() or AddExpression():

[code:1e1nua85]// 0 is default initial value
AddNumberParam("My param", "My description", "0");

// can pass optional default as 3rd param
AddStringParam("My param", "My description");

// accepts either string or number
AddAnyTypeParam("My param", "My description");

// comparison combo box (equal, less than etc)
AddCmpParam("My param", "My description");

// object picker
AddObjectParam("My param", "My description");[/code:1e1nua85]

You can do combo params like this:
[code:1e1nua85]AddComboParamOption("Foo");
AddComboParamOption("Bar");
AddComboParam("My param", "My description", 0); // 0 is default selection[/code:1e1nua85]

These are the same for all of actions, conditions and expressions, except expressions can only take number, string or "anytype" params.

Example:

[code:1e1nua85]AddNumberParam("Number", "Enter a number, whoop de doo.");
AddExpression(0, ef_return_number, "My expression", "My category", "MyExpression", "A description for my expression.");[/code:1e1nua85]

As with actions and conditions, expressions retrieve their parameters by ordinary javascript parameters, but following the required 'ret' parameter. For example, the above expression function would look like this:

[code:1e1nua85]// You can choose any name for the "my_number" parameter, it doesn't matter.
// However it must come *after* the "ret" parameter.
exps["MyExpression"] = function (ret, my_number)
{
ret.set_float(my_number * 2); // just return parameter doubled
};[/code:1e1nua85]

Hope that helps. I'd recommend reading through the javascript of all the other plugins to gain an understanding of the SDK right now - there aren't any docs yet.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Sun Jun 05, 2011 2:33 pm

Ashley how are the Params stored and passed to the functions. Im trying to make a reference object that allows the user to store a reference to an object and then call actions based on an event, but I dont know how to call the function with the parameters. In the old version of construct they were all passed by sending in a vector or an array I think but now you use normal function parameters and im not sure how you store them when you are adding them in the ACES.

and before you go telling me that there would be problems with calling functions like that i am well aware of them. If a user calls a function that does not exist the plugin would handle it.
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

Post » Sun Jun 05, 2011 8:09 pm

I'm not sure what you mean... parameters are passed to the functions as ordinary javascript parameters now - if you have an integer parameter followed by a string parameter, it calls the action routine with a number followed by a string. These are just ordinary javascript values, nothing special.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Mon Jun 06, 2011 2:09 am

ok so the way I want the object to work would be kinda like the function object where the user would call an add param action and then once all of the params have been added they would call a doAction event and the plugin would try to call the function. The problem I run into is passing the parameters. Because In the add param action the param would be added into an array but the functions no longer take arrays as parameters how do you pass each element of the array to the function separately when there is no defined number of parameters? Do you know if javascript has any kind of reflection?
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

Post » Mon Jun 06, 2011 11:35 am

apply (the function the runtime uses to do the same thing).
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Mon Aug 01, 2011 5:48 am

Extract from editime.js
[code]AddStringParam("\"data\"", "Data to encode", "\"\"");
AddExpression(0, ef_return_string, "Encode MD5 (Hexa)", "Encoding", "encodeMD5", "Encodes the data provided with MD5 algorithm. Hexadecimal output.");
AddStringParam("\"data\"", "Data to encode", "\"\"");
AddExpression(1, ef_return_string, "Encode MD5 (Base64)", "Encoding", "encodeMD5B", "Encodes the data provided with MD5 algorithm. Base64 output.");[/code]

I think there is an error in the way the sdk is using the parameters of the function AddExpression.

[code]AddExpression(0, ef_return_string, "Encode MD5 (Hexa)", "Encoding", "encodeMD5", "Encodes the data provided with MD5 algorithm. Hexadecimal output.");[/code]

0 : First param _ ExpressionId
ef_return_string : Flag
"Encode MD5 (Base64)" : Supposed string to be used as "name" of the expression in the ACE Table
"Encoding" : Group
"encodeMD5" : Referal name string in runtime (related expression will be found using this string). String to appear in events too.
"Encodes the data provided with MD5 algorithm. Hexadecimal output." : Explanation/comment in the ACE table.

So the error is in the fact that "encodeMD5" is used as expression name in the ACE table. Supposed name is not used at all seemingly.
Maybe I made a mistake in the understanding of this function. Please correct me if so.Kyatric2011-08-01 05:50:04
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,805

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 10 guests