Where to add properties, Instance or onCreate?

0 favourites
  • 8 posts
From the Asset Store
Add SubRip (SRT) subtitles to your videos in Construct 3
  • 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?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I always put at

    instanceProto.onCreate = function() { ... }[/code:1oiu46ex]
    to get properties.
  • 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.

  • 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

    this.strDelimiter = this.properties[0];[/code:36ndt30h]
    
    Anyway, you might check that pluginProto.onCreate would be called while every instance created. pluginProto.Instance would be called once at all I think.
  • 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.

  • "JavaScript object properties" could be assigned any where.

  • 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.

  • Hey Ruskul, <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    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:

    Subscribe to Construct videos now

    (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. <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)