Access Object Properties from Plug-in

For developers using the Construct 2 Javascript SDK

Post » Tue Jun 26, 2012 10:09 pm

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?
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Wed Jun 27, 2012 1:41 am

@Wastrel

You need to get the reference of "Sprite" first.
This post might help you after you got the reference of object.rexrainbow2012-06-27 04:11:38
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Post » Wed Jun 27, 2012 10:59 am

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
[code]inst.x = 0;
inst.set_bbox_changed(); // see SDK manual for why you need this[/code]
Each plugin has different properties, so you'll need some way to identify the plugin type, or detect the presence of expected properties.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Wed Jun 27, 2012 6:11 pm

@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!
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Fri Jun 29, 2012 7:08 pm

[QUOTE=Ashley]Each plugin has different properties, so you'll need some way to identify the plugin type, or detect the presence of expected properties.[/QUOTE]
@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.Wastrel2012-06-29 19:26:48
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Fri Jun 29, 2012 8:33 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Fri Jun 29, 2012 9:06 pm

@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!
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Fri Jun 29, 2012 9:35 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Fri Jun 29, 2012 11:27 pm

@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!Wastrel2012-06-29 23:53:01
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Sat Jun 30, 2012 1:52 am

@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.
B
97
S
22
G
178
Posts: 4,121
Reputation: 104,049

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests