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

  • 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!

  • 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

  • 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:

    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[/code:21tmy9g2]
    
    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:21tmy9g2]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;
    	}
    }[/code:21tmy9g2]
    
    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.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • // Initialise Code to a random number
    For each Object1
    --Object1.Code = random(1,10)[/code:2gamflam]
    
    // also say Object2 has an instance variable "CodeSum", which has a default value 0
    
    [b][u]For each Object2[/u][/b] <-----------------------
    
    Sub Event:
    
    [b][u]For each Object1[/u][/b] <-----------------------
    --Object2.CodeSum += Object1.Code // offending line
    
    Try that.
    
    But this will work:
    
    Local Variable AddAll = 0
    
    [b][u]For each Object1[/u][/b] <-----------------------
    --Add to Local Variable AddAll Object1.Code
    
    [b][u]For Object2[/u][/b] <-----------------------
    --Set CodeSum to Local Variable AddAll
  • 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"?

  • 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

  • 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>

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