How do I get instance variables values of objects in array

Just started using Construct 2? Post your questions here

Post » Mon Apr 21, 2014 7:23 am

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:

Code: Select all
for (i = 0; i < array.length; i++)
{
     if (array[i].instancevariable.value > SomeNumber)
                 doSomething;
     else
                 continue;
}


Thanks! :D
B
4
Posts: 2
Reputation: 274

Post » Mon Apr 21, 2014 8:14 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,795

Post » Mon Apr 21, 2014 8:37 am

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 :)
B
4
Posts: 2
Reputation: 274


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests