Testing object proximity before running an event

0 favourites
  • 8 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hello,

    I'm making a function to create objects called SPRITE in the start of the layout. The function is supposed to test if another SPRITE is too close to the supposed spawn point before creating another SPRITE in the spawn point.

    The function has Spawn.X and Spawn.Y parameters. These are changed by a loop that runs before the function and that part works fine.

    So this is how i'm trying to set up the spawning:

    Conditions:

    SPRITE.NUMBER = 0

    Action:

    Create SPRITE at Spawn.X, Spawn.Y

    (this is to spawn the first SPRITE and this part works correctly).

    Conditions:

    SPRITE.NUMBER > 0

    1. Pick all SPRITE

    2. Use system condition compare two values: Distance(0,SPRITE.Y,0, Spawn.Y) < desired minimal Y distance.

    3. Use system condition compare two values: Distance(SPRITE.X,0,Spawn.X,0) < desired minimal X distance.

    4. PickedCount = 0

    Action:

    Create SPRITE at Spawn.X, Spawn.Y

    Only the first SPRITE is created. When I toggle disabled the conditions the function works just fine. It's just that I can't seem the function to properly evaluate the spawn point distance to the previously created SPRITEs. I have also tried replacing the compare two values conditions with the pick by evaluate system condition with no luck.

    Thanks for help!

  • I don't understand your code.. If you need to check that there are no sprites near the spawn position and then spawn a new sprite, here is how you can do it:

    .

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Newly spawned Sprites will not be pickable until the creation process has finished, which Construct does at the end of the event that creates it. So you have to wait until the next top level event, or use a Wait 0 seconds, before you can use Pick All to pick the new instances.

    The "System Compare two values" will only look at one instance of Sprite and Spawn, so if you are looking for any instance too close then do something like what Dop2000 said.

  • Dop, thanks for the reply!

    AllanR, thanks for reply! Yes I noticed that the problem probably was caused by the order the events run since the event worked when it was not connected to the "on start of layout condition".

    I'm thinking about using "pick by evaluate" instead of "compare two values" and running the event with "trigger once" instead of "on start of layout" condition, which seems to work fine this far.

  • By the way, if you want an event to run if certain conditions are not met, what's the difference between using "PickedCount = 0" and using "Else" after the conditions? Don't they complete the same goal?

  • Be careful with "Trigger once", it's the most misused feature in Construct and it can cause lots of problems when used incorrectly. Don't use this condition by its own, with loops, inside of functions, triggers, with conditions that can pick multiple instances etc.

    what's the difference between using "PickedCount = 0" and using "Else"

    When nothing is picked in event #3 in my screenshot, you will not be able to check "Sprite.PickedCount=0". If you add any sub-events or actions inside this event, they will not be executed.

  • Be careful with "Trigger once", it's the most misused feature in Construct and it can cause lots of problems when used incorrectly. Don't use this condition by its own, with loops, inside of functions, triggers, with conditions that can pick multiple instances etc.

    > what's the difference between using "PickedCount = 0" and using "Else"

    When nothing is picked in event #3 in my screenshot, you will not be able to check "Sprite.PickedCount=0". If you add any sub-events or actions inside this event, they will not be executed.

    That's all very good to know.

    Does that mean "PickedCount = 0" is always useless and you should always use "Else" instead?

  • There are situation where "PickedCount=0" condition may be used. For example, if several object instances were picked, and inside the event you destroy some of them. After "Wait 0" you can check if any picked instanced are left.

    .

    But in case the parent event picked no instances, you can only use "Else". Because it basically equals "If False then { } Else { }"

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