Where to add properties, Instance or onCreate?

For developers using the Construct 2 Javascript SDK

Post » Fri Mar 20, 2015 6:39 am

When adding properties to my object in the SDK runtime, should I create instance properties in:
(Edit: By "properties" I mean JavaScript Object properties, not C2 property-panel properties.)

pluginProto.Instance = function(type) { ... }
or
instanceProto.onCreate = function() { ... }

Currently I'm creating them inside "pluginProto.Instance" as that's what I've seen in other plugins.
Is there any situation where one is better than the other?
Last edited by fisholith on Sun Mar 22, 2015 12:53 pm, edited 3 times in total.
B
27
S
18
G
8
Posts: 327
Reputation: 6,154

Post » Fri Mar 20, 2015 6:59 am

I always put at
Code: Select all
instanceProto.onCreate = function() { ... }

to get properties.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Fri Mar 20, 2015 9:54 am

Interesting.
Thanks for pointing out that they can be declared in onCreate :)
I was going to ask about that specifically, as I wasn't sure if there was anything special needed to use it reliably. It sounds like it just works.
Between Instance and onCreate, is there a particular reason one might be preferable to the other, or is it more just a matter of style or convention?

I was trying to remember why I did one versus the other, and looking back through the original SDK templates (I mainly work from custom templates) I found the following comment inside the pluginProto.Instance body:

this.type = type;
this.runtime = type.runtime;

// any other properties you need, e.g...
// this.myValue = 0;


I think this may be why I've been putting properties there. I'm not sure if it makes a difference.

That said, the official Dictionary plugin declares properties in onCreate, and the official AJAX plugin declares properties in pluginProto.Instance. So I'm not sure if there's anything important about one vs the other, or if it's just down to preference.
B
27
S
18
G
8
Posts: 327
Reputation: 6,154

Post » Fri Mar 20, 2015 10:00 am

official AJAX plugin is singleglobal, but official Dictionary plugin is not. i.e. you night have more than one official Dictionary object.
BTW, imo, properties are reading values defined in properties table, like
Code: Select all
this.strDelimiter = this.properties[0];


Anyway, you might check that pluginProto.onCreate would be called while every instance created. pluginProto.Instance would be called once at all I think.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Sun Mar 22, 2015 12:48 pm

Interesting, and a very good point. :)

With respect to "properties", do you mean C2 property panel properties, or JavaScript object properties? In my earlier post I meant JavaScript object properties, but at the time it hadn't occurred to me that it was ambiguous. Sorry about that.

And, yes, now that I've been looking through more plugins, "onCreate" seems to be the way to go. I haven't tested to see if "pluginProto.Instance" is called on a per-instance-created basis, but I think you're right about "onCreate" being the better option for objects that are intended to allow multiple instances. In any case, "onCreate" will definitely get called each time.

Thanks again for the information and advice. You're always super helpful rex. :)
B
27
S
18
G
8
Posts: 327
Reputation: 6,154

Post » Sun Mar 22, 2015 3:54 pm

"JavaScript object properties" could be assigned any where.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Fri Apr 29, 2016 11:28 pm

I think this has been resolved at this point but I was curious if you figured anything out definitively. I have been putting properties in .instance for all my behaviors (so I noticed lol) and there doesn't seem to be any difference that I noted. There must be right?

I suck at jscript and am much more comfortable with c++ or c# so sometimes I just fly with something if I saw it that way even if I don't understand why.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Sun May 01, 2016 2:25 am

Hey @ruskul, :)

The short answer is that I now always use
instanceProto.onCreate



My reasoning
As I understand it, anything in "instanceProto.onCreate" will get run on a per instance creation basis. and so the creation of instance related stuffs should go there.

If you knew in advance that you'd only ever have one instance of your pluggin (e.g. the Keyboard plugin), in theory you could use the "pluginProto.Instance" instead, ... but ...

If there's only ever going to be one instance, you can still use "instanceProto.onCreate", and it will just get called once for your one instance. So "onCreate" seems to work just fine regardless of the situation.

Also, I have now created single-instance-only plugins using both methods, and they both work, so I use "onCreate" exclusively now. When I say "single-instance-only" I mean things like a math utility library, which you can literally only create a single instance of, again much like the Keyboard plugin.



My modified SDK
Here is a download for my modified version of the plugin SDK, with additional comments and a more graphical style of dividing up the different sections of the runtime and edit files. I'm hoping to make a pluggin creation tutorial using this version of the SDK at some point, but in the meantime you're welcome to use it for pluggin making or just reference if it helps out in either respect.

!fi_plugin_template.zip



JS structure video
This is a really good video that describes why the basic structure OOP in JavaScript looks the way it does, and the philosophy behind JavaScript's prototypal inheritance. It has an awesome side-by-side code and visual diagram approach to explaining everything.

JS Video: The Definitive Guide to Object-Oriented JavaScript (youtube - 27 min)

The video is also a companion to a website that lets you type JS code and see a visual diagram representation of the inheritance structure as you go, and it uses exactly the same diagram system shown in the videos.

JS real-time diagram Site: http://www.objectplayground.com/



JS online sandbox
And finally, there's an online JS coding environment, that I use for testing functions and code snippets while working on pluggin projects, and it has been indispensable as a JS sandbox.

It's just nice to open a webpage and be instantly able to program anything, and have that code print anything out to a console.

JS sandbox: https://repl.it/languages



Hope that helps out. :)
B
27
S
18
G
8
Posts: 327
Reputation: 6,154


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 3 guests