0 Favourites

reference objects by index like an array

This forum is currently in read-only mode.
  • let's say you have a sprite called "monster"

    I make a loop that pastes 20 monsters in a row at runtime

    then I want to have a loop that does something along the lines of

    if monster(loopindex) is dead then monster(loopindex-1) move here

    I know it's possible in a roundabout way to reference a certain monster by index

    by creating a private variable

    called monsterindex

    and then making two conditions like

    loop i

    if monsterindex = loopindex

    then your actions

    but it's difficult and cumbersome to do something where I select the current loop index

    and the next four monsters, and do something slightly different with each

    so is it possible to just reference instances by some type of sequential index?

    like an array of objects?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Why don't you just do:

    +Monster.Value('isDead') = "true"
      ->Move wherever
    [/code:3pdaaefr]
    
    That will only affect the monsters that are dead.  No need for a loop.
    
    Or if you really need to use a loop then:
    
    [code:3pdaaefr]
    +For each Monster
    +Monster.Value('isDead') = "true"
      ->Do stuff
    [/code:3pdaaefr]
  • Check out the loop category of conditions in the system object - "For", "For Each", "For Each Ordered", etc.

  • I was using the monster dead as an example for simplicity's sake

    I actually need to do some interpolation

    123456789

    let's say the player clicks and drags number 3 down

    I want to interpolate 3.y (loopindex)and 1.y(loopindex-2),

    and 3.y(loopindex) and 5.y(loopindex+2) to find new positions for 2 and 4

    1   56789
    -2-4-----
    --3------
    [/code:ad8enkr1]
    
    would there be a way to do something like that?
    
    edit: I just thought of a way that might work, but I won't get to test it out until after work, so maybe create an array that stores the unique IDs of each object instance.  would there be a way to say set UID.x to something, or you can't access objects by their UIDs?
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)