Need a little help with targeting system / pick nearest ...

0 favourites
  • 11 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I was implementing a targeting system in my game, but I've got stuck when finding the next target.

    About my code:

    Local static number "targetMode" is the type of targeting the player is using as it follows:

    [1] When activated, targets the closest enemy at that moment. When that enemy is destroyed or offscreen, the targeting mode is disabled.

    [2] When activated, targets the closest enemy at that moment. When that enemy is destroyed or offscreen, targets the next closest enemy.

    [3] When activated, continuously targets only the closest enemy.

    Function "toggleLockOn" activates and stops the targeting system.

    Function "setTarget" should target the closest enemy at that moment.

    Function "releaseTarget" stops the targeting system.

    Function "targetIntro" is the nice effect when the target appears onscreen.

    The issue is that after I destroy an enemy that is targeted, the target remains in the same place, so I think the problem is in the function "setTarget" witch picks the same object even if the object should be destroyed.

  • You are picking by target.targetID (events 16,21,27) but if that target is destroyed, there is nothing to pick so the "destroyed" logic is never being called. You need to shift that to trigger either on the enemy collision, or enemy On destroyed...

  • blackhornet

    My logic was the following:

    -> Pick the enemy that has the same UID as the target.targetID

    -> If that enemy is destroyed or is not on screen anymore then call "setTarget" in order to find the next target.

    Can you show me how to implement it correctly in C2' events ?

    Also, do you know how can I check if there is any enemy on the screen ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Triggers happen outside of the event loop so you can't use them as conditions as you did in 17,21,27. They generally need to be top level, and you do you logic below it. This is closer to what you want: event 43.

    In 46, you need to release and then re-target. You may need to shuffle for your rules.

  • blackhornet

    Thank you, but still it doesn't work how it should. If I destroy an enemy that is not targeted then I destroy the targeted enemy, the target remains in the same place in "targetMode" 1 and 2 .

    I tried to add "[invert] Object with UID target.targetID exists", after "enemy - On destroyed" but no luck ... That brings me to the question: How can I check when an object with a specific UID is destroyed ?

    For some reason, the "setTarget" function still picks the UID of the destroyed object.

  • Some refinements.

  • blackhornet

    Thank you again, but is still not working how it should:

    targetMode=1 -> target is destroyed when any enemy is destroyed (not only the targeted one)

    targetMode=2 -> works as intended

    targetMode=3 -> target is destroyed when an enemy is destroyed if the playerBox doesn't move

    I can't figure how to do it correctly... I tried "[invert] Object with UID target.targetID exists" (if that object doesn't exist) then call function "setTarget" (depending on "targetMode").

    Either there is an error in my logic, or there is a bug in C2, because I tried a lot of walkarounds, but no luck so far

  • It's the annoying "can't pick until next top level event" feature of C2 getting in the way. Using visibility instead of create/destroy makes things easier.

  • blackhornet

    Thank you It works perfectly

    Can you explain me the following:

    -> What do you mean by "It's the annoying "can't pick until next top level event" feature of C2 getting in the way. Using visibility instead of create/destroy makes things easier." ? I see that you've made the enemy invisible when is destroyed as a walkaround.

    -> Event 36: Why did you add "wait 0 seconds" (skip one frame) ?

  • The wait can go, I think. That was part of my flailing with the Create issue.

  • blackhornet

    Thank you for your help

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