Access inst object in instance function?

For developers using the Construct 2 Javascript SDK

Post » Wed Apr 16, 2014 9:35 am

How do I get instance of an object type in instance function, such as Instance.onCreate? I know there is a type.instances array, but how to sort the particular instance out?
B
23
S
9
G
3
Posts: 114
Reputation: 3,794

Post » Sun Apr 20, 2014 11:08 pm

As an example say we wanted to check the Text value of each Text Plugin Object Type Instance. You could use this statement to access the value:
Code: Select all
this.type.instances[index].text;
If you look at the implementation of the Text Plugin, you'll find in the runtime.js that the Instance.onCreate has a 'text' member variable. If you wanted to look at the "Instance Variables" that you added at Edit Time you could use these statements:
Code: Select all
var instIndex = 0;
var varStr = 'text2';//made up instance variable added in the editor
var varIndex = -1;
var inst = this.type.instances[instIndex];
var instVarNames = inst.instance_var_names;
for(var instVarNameIndex in instVarNames)
{
   if(instVarNames[instVarNameIndex] = varStr)
   {
       varIndex = instVarNameIndex;
   }
}
var instVarValue = inst.instance_vars[varIndex];
B
9
S
2
G
1
Posts: 41
Reputation: 708

Post » Mon Apr 21, 2014 9:33 am

I'm not sure the question in the OP makes sense. You refer to the created instance with 'this'. If you want to refer to a different instance, then which are you after? You need to explain exactly what you're trying to do in detail.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 1 guest