0 Favourites

How do I get instance variables values of objects in array

  • Hi everyone,

    just started, a few day ago, to use this great tool and so far it's going really well. I have one question though:

    I have an array that contains a number of different objects. These objects have different instance variables. What I want is to loop through this array and compare the instance variables values of these objects with some other value.

    How do I access the instance variables values of these objects?

    For the programmers out there, what I want to achieve is something like this:

    for (i = 0; i < array.length; i++)
    {
         if (array[i].instancevariable.value > SomeNumber)
                     doSomething;
         else
                     continue;
    }[/code:1d68ndsn]
    
    Thanks!
  • Your question is a bit confusing as for Construct 2's terminology.

    Array is a plugin (an object type) in itself, that, like in Javascript for example, contains values.

    You don't store objects in C2's array, only values.

    To loop through the array you can use the Array condition "For each element" that allows you to check through one to three of the array's dimensions.

    In your example of a single dimension array you use a "For each X element".

    Then you add a "Compare at X" condition using Array.CurX as the current X index.

    Arrays can have instance variables like most plugins in C2, but they are not values directly stored into the structure of the array. Those variables are compared through the system conditions.

    I hope this help you.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Yeah I was wrong, I thought any object can be stored in an array, but it seems it can just hold a number or some text (must have missed this when I was initially reading the manual). Well there is always a workaround! Thanks for your help

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