Access Object Properties from Plug-in

0 favourites
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • I have a question about accessing another object's properties from a plug-in. This seems like it should be simple, but I have been going over all of the documentation and forums and haven't found an answer.

    I am creating a plug-in which will store the properties of other objects as text for the key, then a separate value as the value, e.g. Hash["Sprite.X", 100].

    What I would like to then do is update that object's property by converting the hash key to a reference, then set that property to the value, e.g. Sprite.X = 100.

    I have everything down except the knowledge of how to change the object's property from a plug-in. I can't seem to figure out how to reference the other object. Are we even allowed to do this?

  • Wastrel

    You need to get the reference of "Sprite" first.

    This post might help you after you got the reference of object.

  • Generally you just need to use an object parameter, which gives you the object type. From the object type you can get the first picked instance, or just use the entire instance list.

    Most properties are just simple javascript properties on the object, e.g. once you have an instance then you can just say

    inst.x = 0;
    inst.set_bbox_changed(); // see SDK manual for why you need this

    Each plugin has different properties, so you'll need some way to identify the plugin type, or detect the presence of expected properties.

  • rexrainbow & Ashley, thanks for your replies. I see now where the disconnect was in my thinking. I was approaching the problem from the wrong angle.

    I think I'll look more into the behavior/plugin combo.

    Thanks for your help!

  • Each plugin has different properties, so you'll need some way to identify the plugin type, or detect the presence of expected properties.

    Ashley or rexrainbow, is there some easy way to do this?

    I have been reading the SDK manual, but I'm not clear from where the set of properties and/or their types can be retrieved. I tried using instance.properties[index], but this gives the value of the property, and there appears to be no way to determine the type using properties.

    Basically, what I need to be able to do is take an object reference and a string representation of one of the properties of that object, then use those to reference an external object (which I can do now). By external object, I mean a plugin other than my plugin.

    I then need to determine if the property reference is a native property of the object, an instance variable, or a property of a behavior. I think I am close to figuring this out, but I may need a gentle push in the right direction.

    I also need to determine the property type, which I can't seem to figure out at all.

    Any help would be appreciated.

    Thanks!

    BTW, I decided not to use the plugin/behavior combo after all. It didn't seem to help with the direction I was trying to take.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Your best bet is to use Chrome's debugger, and to browse the runtime.js of plugins you're interested in. See their onCreate methods - that's where they create all the properties the object will use. You shouldn't use the properties array directly - that really ought to be deleted after onCreate(), but I left it in anyway.

    You cannot access properties by the string of their name, because the minifier renames all properties on export and will break that.

    Instance variables are stored in an array property called instance_vars IIRC, and behaviors are stored in an array called behavior_insts. The debugger will tell you more.

    It's quite unusual to need to know the types of a property - usually you know what it is by assumption (if you get the 'width' property, for example, obviously you're going to get back a number and not a string). However if you need to dynamically determine types, use Javascript's typeof operator.

    What are you trying to do? Sounds like you'll have a hard time, especially because of the minifier.

  • Ashley, I'm working on some plugins to support a larger goal of creating a suite of AI tools. This particular plugin will manage data transformations, e.g. the constructor will define a transform with properties from various objects, along with a value to which the property should be changed.

    When the constructor is defining the transform, they will choose an object, and the property of that object to be changed, and enter the value to which it should be changed.

    I forgot about minification. :-< It does indeed break the plugin.

    Since there are no static property definitions, like a PID or something, and using the name won't, I suppose I could use the index number of the property, since I assume that doesn't change.

    Also, maybe this is something that would be better handled using a plugin/behavior combo.

    Thanks!

  • Using an index number will be extremely brittle, because if the dev adds a new property to the top, all the other indices are wrong! I do this myself with official plugins and behaviors quite regularly.

  • Ashley, I understand what you're saying. I need to be able to dynamically reference a property in an object, but I can't use a string due to minification, I can't use an index because they can change without notice.

    I will continue to search for a way to do this, because I'm confident it can be done. I realize you are very busy and I really appreciate the time you've taken to answer my questions.

    Thanks!

  • Wastrel

    When you first got the instance reference, keep it in local variable. Remember to remove it when instance destroyed, see official "pin behavior" for more detail.

  • BTW, as Ahsely said, you should use debug console when you develop plugin. In firefox, you could use firebug.

  • Ashley & rexrainbow, I have been using firebug all along, but during the process of writing this plugin, it became clear to me how much I still don't understand about JavaScript in general, and the C2 implementation specifically. It has been quite a learning experience. :D

    I believe I finally had a eureka moment yesterday, and it all started making a lot more sense. After re-reading all of these posts (for the hundredth time), I believe that I am not being clear as to what I am trying to accomplish.

    Perhaps this will make it a bit more clear:

    1. The constructor opens a new project.

    2. They add a Sprite & Keyboard object to the layout.

    3. They add my plugin (let's call it MyPlugin) to the layout.

    4. In the event sheet, under the On start of layout condition, they add the definition action from my plugin (MyDefineAction). MyDefineAction contains three params: Object, Property, Value.

    5. For Object, they select Sprite. (Using the AddObjectParam function.)

    6. For Property, they select Sprite.Angle. (At this point I am using the AddAnyTypeParam function, but this just refers to the value, not the property index.)

    7. For Value they enter 90.

    8. They add a Keyboard-->On Space Pressed condition.

    9. Under this condition, they add the MyPlugin.MyExecuteAction action.

    10. They export the project. During the export, the MyDefineAction function in the run time adds all MyDefinitionAction actions to a hash table, which includes the Object reference, the property reference, and the value.

    11. When Space is pressed, the property in the referenced object is changed to Value.

    What I am looking for is a way at step 6 to capture the index of the property. Then at step 11, I would reference the object and property index to change its value. I can't predetermine what the property will be, so I need to use a reference passed from the edit time, rather than the hard coded property name.

    To answer concerns about the index being unsuitable, the constructor will define an existing object and property at edit time. The index number won't change after export or when the project is run. If the plugin programmer modifies the properties in the future, it won't matter, because the property index would be determined dynamically at edit time, after the plugin has been added to the project.

    I apologize for the wall of text, and also if you already understood what I was trying to accomplish. I just feel I am very close to getting this to work.

    Thanks!

  • I'm not sure I understand the purpose of this plugin. Why would you do your example instead of just a 'Sprite - set angle' action directly? What does it offer over the Function object even?

    The only way you can do step 6 that I can think of is to create a giant combo box with a list of every property across all plugins that you can think of. Then you'd have a giant switch-case of the combo index to the actual property that needs to be set or retrieved. That will survive minification and object properties being reordered, but is a pretty lousy user experience. Your plugin idea is kind of against the grain of how C2 is designed.

  • Your plugin idea is kind of against the grain of how C2 is designed.

    Understood. I will scrap this and try a different tact. Thanks for your patience.

  • Just how do I open the chrome debugger for edittime.js files? I thought the C2 editor was a windows app. So it is possible to debug edittime using a debugger?

    Sorry if this is a stupid question but how would I go about doing this?

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