How do I loop over all instances of type to affect another..

Get help using Construct 2

Post » Thu Jun 25, 2015 7:26 am

If I have for example:

for each Human -> Human.setFriendID(random(10))

it works the way I expect, assigning a value to the FriendID of each instance of type Human, presumably because the subject type of the action and the subject type of the loop are the same (Human).

However if I have:

for each Human -> Base.addTargetPosition(Human.x, Human.y)

where Base is an object of which there is only one instance, it only adds one Human to the list.

So in general, how can I loop over all instances of a particular type, but use some property of that instance to affect a single object of another type?

(only my second day using C2, so sorry if this is obvious)
Thanks!
B
2
Posts: 3
Reputation: 156

Post » Thu Jun 25, 2015 10:59 am

Don't really understand what you are trying to do?

Where does addTargetPosition(X, Y) come from? And what list are you referring to?

And when you say use one property of one type to affect another, I think you might have to be a bit more specific. Because you do it by testing a 1+ conditions of one object and if they are true you do something to the other.

For instant:
Object_1.color = Green
For each Object_1
---Pick nearest Object_2
---Object_2.Destroy
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Thu Jun 25, 2015 11:37 pm

In general, how can I loop over all instances of a particular type, but use some property of that instance to affect a single object of another type?

I'll try another example:

Code: Select all
Create Object2 // there is exactly 1 instance of type Object2
Create Object1
Create Object1 // there are now 2 instances of type Object1

// let's say Object1 has an instance variable "Code", which is a number

// Initialise Code to a random number
For each Object1
--Object1.Code = random(1,10)

// also say Object2 has an instance variable "CodeSum", which has a default value 0

For each Object1
--Object2.CodeSum += Object1.Code // offending line


Since there are 2 instances of Object1 and only 1 of Object2, it can't pair them up. What happens is that Object2.CodeSum is equal to the Code value of the first Object1 instance. What I want is a method where I can loop through each instance of Object1 and add its Code to the single instance of Object2's CodeSum, without trying to pair them up.

Another example in approximate C++ code:

Code: Select all
Object2 onlyOne;
for ( std::vector<Sprite *>::iterator it = sprites.begin(); it != sprites.end(); ++it )
{
   Sprite *sprite = *it;
   if ( sprite->type == OBJECT_1 )
   {
      onlyOne.CodeSum += sprite->Code;
   }
}


I suppose if C2 does arrays I could add the Object1 instances to it then loop over it using an index, but it seems like there should be an easier way.
B
2
Posts: 3
Reputation: 156

Post » Fri Jun 26, 2015 1:10 am

Code: Select all
// Initialise Code to a random number
For each Object1
--Object1.Code = random(1,10)



// also say Object2 has an instance variable "CodeSum", which has a default value 0

For each Object2 <-----------------------


Sub Event:

For each Object1 <-----------------------
--Object2.CodeSum += Object1.Code // offending line

Try that.

But this will work:


Local Variable AddAll = 0

For each Object1 <-----------------------
--Add to Local Variable AddAll Object1.Code


For Object2 <-----------------------
--Set CodeSum to Local Variable AddAll
B
5
S
1
Posts: 68
Reputation: 515

Post » Fri Jun 26, 2015 10:13 am

GaryG wrote:In general, how can I loop over all instances of a particular type, but use some property of that instance to affect a single object of another type?

Since there are 2 instances of Object1 and only 1 of Object2, it can't pair them up. What happens is that Object2.CodeSum is equal to the Code value of the first Object1 instance. What I want is a method where I can loop through each instance of Object1 and add its Code to the single instance of Object2's CodeSum, without trying to pair them up.


Sorry I fail to see the problem.

In your example you set Object1.code to a random number.

Then you go through each of them and add it to Object2.Codesum

To me it seems to work as you want it to? what do you mean with "Without trying to pair them up"?
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Sun Jun 28, 2015 1:13 am

Okay, it turns out the issue was that for each only loops over objects created in the editor rather than those created at run time using the create object action. In my specific case since I know the maximum number of objects of each type in a level in advance I could work around it by creating them all first in the editor and filtering with other conditions in the loop to determine which were actually being used on each specific level. It would be nice if for each looped over all instances or if there were some alternative that did, but for now at least I have found a way :)
B
2
Posts: 3
Reputation: 156

Post » Sun Jun 28, 2015 11:26 am

GaryG wrote: It would be nice if for each looped over all instances or if there were some alternative that did, but for now at least I have found a way :)

For each loop through all instances regardless of where they are created editor or runtime.

If it doesn't it most likely because your picking is wrong somehow I would guess.

You have to be sure that For each is placed correctly:
<Object Conditions>
For each <Object>
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828


Return to How do I....?

Who is online

Users browsing this forum: Animate, Bro7hers, ezgaming420 and 3 guests