Line of Sight issue

0 favourites
  • 8 posts
From the Asset Store
solution for games like "fruit ninja" and drawing applications
  • I'm using the following code

    If object "Green" has LineofSight to object "Red" - Set Value to 1

    Else - Set value to 0

    This works fine until I make copies of Object"Green". If one of the green objects loses LOS to all red objects, the value still won't change if a different copy of "green" still has LOS to a red object.

    I'm pretty sure I know why this happens but I can't figure out how to get around it.

    I can't just say "If Green does not have LOS to Red, set value to 0" because it has the same problem, if a copy still has LOS, it wont change.

    I've tried using For Each but that didn't work either.

    Am I missing something?

    Thanks for any help

  • "Else" in this case doesn't mean "all other instances that don't have LOS". If at least one instance has LOS, then the Else block will not be triggered.

    Assuming that you are setting the value 0/1 to an instance variable (not global), you can use "For Each", or you can set 0 to all instances first and then change it to 1 for instances that have LOS, like this:

    On every tick (or whatever other event) -> Green Set variable to 0
         If Green has LOS to Red -> Green Set variable to 1
  • Hey thanks for the help. This works great.

    I'm a little confused though.

    Why does the code, "If green has LOS to object - set value to 1" work for multiple copies, but won't work if you invert it?

    I've been so frustrated for the past few days wrestling with this function XD It would be nice to know for the future

    thanks again

  • Actually, inverted condition should work too.

    You can do this:

    green has LOS to object -> set value to 1

    green has NO LOS to object (inverted condition) -> set value to 0

    The first event will pick all green instances that has LOS.

    The second event will pick all instanced that don't have LOS. (EDIT: this is wrong, see my next comment)

    Once you understand the concept of picking in Construct, it should all become much clearer.

  • Ok thanks for all the help.

    I think I was misunderstanding what I was looking at in my code, and am currently too tired/brainfried to sort it all out.

    I'll look at it again later and try to figure things out

    thanks again for all the help its greatly appreciated

  • Ok I think I realized why I was confused.

    If you make the code:

    If Green has LOS to Red - set value to 1

    (Invert) If Green has LOS to Red - set value to 0

    This will not work if one Green has LOS to Red while a Green copy does not (at least not in my test)

    However, if you just reverse the order so it's:

    (Invert) If Green has LOS to Red - set value to 0

    If Green has LOS to Red - set value to 1

    It works correctly with all copies.

    Again I might be missing something because I'm tired but I keep running into walls with this and Ive been trying to get it figured out for days now.

    I'm going to sleep soon so sorry in advance if I don't get to respond to anyone who might help but I will check tomorrow

    thanks again

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hmm, you are right, and I was wrong.. Inverted "Green has LOS to Red" condition doesn't mean "pick all Green instances that don't see any Reds".

    It picks Green instances that don't have LOS to at least one Red instance.

    So, for example, if Green instance sees 10 out of 11 Red instances, it will still be picked by this event.

    Anyway, to avoid confusion I suggest using For Each:

    For each Green
       Green has LOS to Red -> Green set var to 1
       Else -> Green set var to 0
  • Wow interesting.

    I think that explains my confusion. Definitely feels good to know this now.

    Thanks again for all your help

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