HowTo read inst var of a parsed object in Behaviour?

For developers using the Construct 2 Javascript SDK

Post » Sun Nov 02, 2014 10:28 pm

Hi

I have made a behaviour where I am parsing an object as a variable.
This object has many instance variables that I need to read in my script

I tried to read the instance variables as if they were x,y,opacity variables ...etc, but it is not working.
This is what I am trying:


Code: Select all
Acts.prototype.MyFunction= function (object_){

      var inst_only = object_.getCurrentSol().getObjects();
      inst_only = inst_only[0];
      console.log(inst_only.x); // it is working, I can see my x position in console
      console.log(inst_only.MyInstanceVariable); //NOT working, giving me undefined.
      ...


I am stuck with my project at this stage, your help is highly appreciated.

thanks
Last edited by hassiba on Sun Nov 02, 2014 10:39 pm, edited 2 times in total.
B
7
S
1
Posts: 26
Reputation: 530

Post » Sun Nov 02, 2014 10:38 pm

I think I got the answer, from a similar thread:

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];


will report back if it works

EDIT:
confirmed, and it is best used in a separate call function
B
7
S
1
Posts: 26
Reputation: 530

Post » Mon Nov 10, 2014 3:30 am

Hi again

could someone please help me. the code for parsing the object variables is only working in browser but not in Android Cordova emulator on intel XDK nor on actual android mobiles!

could someone please have a look:

Code: Select all
      this.inst.update_bbox();

      var inst_only = object.getCurrentSol().getObjects();
      inst_only = inst_only[0];
      
         var varStr = "MyVariable";
         var instIndex = 0;
         var varIndex = -1;
         var inst_only = object.getCurrentSol().getObjects();
         inst_only = inst_only[0];
         var instVarNames = inst_only.instance_var_names;
         
         var retvar;
         for(var instVarNameIndex in instVarNames)
         {
            if(instVarNames[instVarNameIndex] == varStr)
            {
               varIndex = instVarNameIndex;
               retvar = inst_only.instance_vars[varIndex];
               
            }
         }
B
7
S
1
Posts: 26
Reputation: 530


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests