[SOLVED]Target selection (multiplayer)

Hi guys!
I’ve been working on this one for a few days now and although I found a few possible ways, I couldn’t make them work to the end…
I’m planning a multiplayer game with 4 players (ships).
I’d like to implement the “simple” mecanic : when the player presses a button, the nearest enemy ship is highlighted and if he presses a second time, the second nearest enemy is hightlighted and so on…
Although I read some threads on this forum, I couldn’t make it work so I decided to try the array solution.
My algorhythm is not perfect; it’s working well enough but I’m stuck at some point.
Let me explain. I’ve got 4 ships and each ship has a “target circle” stuck to it and hidden at player spawn.

  1. I’d like to fill an array with the distances to the other ships
  2. The shortest distance will give me the ID of the nearest enemy ship
  3. Make the Ring Selector visible

That would be my first 3 steps.
I’ve done 1 and 2 but I’m stuck at 3. I’ve got the ID of the nearest ship stored in a (desynchronized) scene variable called TargetShip_ID. But I can’t find a way to make the 3rd step happen…

Thanks for reading and enlightening me!

What about taking ownership of Target, that way every time Tab is pushed it will look for the closest Target *not owned by the player pushing tab, and each time it finds that closest Target it takes ownership so that Target won’t be picked again next time.

You can also append it to an array then, while highlighting it, if you really need an array.

The bad thing about this is - it won’t really work if players can all push tab at the same time. But I saw no details in your post to guide me on this matter.

Also when the turn is over, you will have to tell the players to pick the Target closest to them and take ownership of it - if it’s for some reason important that they own the target stuck to them again. And also to implement logic for turning off the effect when you’re done and other routine housekeeping.

1 Like

Hey! Thanks for your answer! Sorry, I might not have given enough details!
Yeah, I had trouble with that ownership thing because I would have liked the Target Selector sprite to stay “local”.
And I should have said that the aim (for now) is to allow the local ship to strafe around (circle around) the targeted enemy, so yeah, all players should be able to push the tab button at the same time, potentially.
What I can definitely do is clean my code a bit and instead of trying to show/hide something (the target highlight sprite) that belongs to another player, I can try and create a sprite (maybe not multiplayer object) at the nearest picked position…and move it to the next array index with each Tab press.
Thanks again for your insights! That’s always very valuable!

1 Like

Something like that should work, and would also allow easier object picking.

1 Like

Hi everyone!
I’ve worked a little (a lot actually) on my target selection problem and I really needed to test everything first hand. Here are some conclusions about what I learnt.
PROBLEMATIC : I need to get an array of my enemies from the closest to the farthest with their respective ID.

N°1 : we can add elements in ARRAYS even if it doesn’t contain any children yet.
N°2 : With Arrays containing Structures. I think it is NOT possible to copy/ asign the value of a structure directly to another Array. We must copy all the children of the Structure to the other array one by one. [I’d really like confirmation on that].

That being said, after working on my algorhythm, it’s working although it’s not perfect because quite redundant.
For example : I create 2 arrays : an array called “Unsorted” which stores the distance and ID of the enemies and a second array called “Sorted” which contains the enemies sorted by distance with their respective ID.

To improve, I could only do 1 array and sort at the same time.

And I have to say that I’m lucky I only plan 4 characters at the same time otherwise I would have to programmatically find a solution to my conditions where I check the indices in the sorting algorhythm.

Moreover, I’m sure there is a solution with the action : pick the nearest object but I couldn’t make it work!

Anyways, here is the thing at this stage :

Hey!
I think I found a way easier solution…in 20 min…I hate myself!
Well, I tested things out just a little and it seems to work… with the “pick the nearest” action and a simple boolean…

Hi guys!
Just so you know, I’ve finished implementing my targetting system : it was way more difficult than I imagined! But I got it to work so I’m quite proud of my little self!
I’m sure someone more clever could optimize this but I’ll leave it like that (at least for now!)

So :

  1. I create a array of enemies, which I sort by distance.
  2. When the player presses Tab for the first time, I create a target (RedTarget) which is a red circle around the nearest enemy and more presses of the Tab button go through the other enemies (by distance).
  3. When there are no more enemies, it deactivates.

Well, I’ll turn to the player actions now :

  1. facing the targeted enemy
  2. Strafing around the targeted enemy!

I’m sure it’s going to be another challenge!

Thanks for reading!

1 Like