GaryG's Recent Forum Activity

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.
  • 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!

GaryG's avatar

GaryG

Member since 25 Jun, 2015

None one is following GaryG yet!

Trophy Case

  • 9-Year Club
  • Email Verified

Progress

10/44
How to earn trophies