How do I detect than an object touch an other copy of itself but with a specific instance variable?

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I have tried using overlapping and LOS condition to know if a copy of the sprite is near, but I wasn't able to make it detect only if the variable of this copied object is for exemple: 1.

    In short

    I would like to know how to do this:

    If object is overlapping and that overlapping object instance variable = 1

    then

    change this object colour...

    Thank you

  • Just to clarify you want to detect collisions between instances that have a variable=1 and other instances with variables<>1?

    Easiest way to reference another instance is to use a family with just that sprite type

    So say you have a Sprite added to a family called other, and you added the instance variables to the family you could do this:

     Other: variable<>1
    Sprite overlaps other
    Other: variable=1
    — sprite: set color to red

    The difficulty with this is you need to add the variables to the family. Which can be a bit laborious to change if you already have a bunch of instance variables and events using them.

    An alternate idea is to use dummy sprites that you create on the fly.

    every tick
    — dummy: destroy
    
    Sprite: variable =1
    — Sprite: spawn dummy
    
    Sprite: variable <> 1
    Sprite: overlaps dummy
    — sprite: set color to red

    With this method you may need to do more when you spawn the dummy. The idea is you want the dummy sprites to match the position,size,angle and shape of the source sprite.

    A third way could be to manually loop over each pair of instances. With it you’d get the iid of both instances, and you’d do expressions like sprite(i0).x to access values to compare. If you want to modify an instance you’d use the pick nth instance condition. The loop would look like this:

    For “i0” from 0 to sprite.count-2

    For “i1” from loopindex+1 to sprite.count-1

    Unfortunately the biggest disadvantage of this method is you can’t use the overlapping condition so you’d have to do your own collision detection. Which may or may not be complicated depending on the shapes.

    A final option would somehow use the js api to do it. But that is completely uninteresting to me.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I tried your first option, but could make it work...

    This is contexte:

    The squares represent territories, the yellow ones have the variable ''Unowned'' and the reds ''Owned'' but they all have the variable ''Adjacent''. So if a yellow square touch the red (owned) ones the ''adjacent'' variable would change to 1 (meaning it's touching) and then if its equal 1 I change the colour of those touching.

    So I need to know how to change the variable ''adjacent'' of the unowned squares in dirrect contact with the reds.

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