[Tutorial] Making an Enemy Follow Multiple Sprites 🐍

The code gathers all the player-enemy distances, sorts them in an array to find the shortest distance, then for each player sprite, compares it’s distance to the shortest distance in the array to find the closest player.
This player sprite is stored in the enemy sprite data and set as a follow target. All of this happens every game update so that the enemy can register a new closest player as fast as possible.


The array before being sorted. After the sort, the 52.47… distance will be in the 0th spot in the array.

The distances are recorded to such an absurd number of precision that I’ve managed to use this method to register sprite overlaps as the default block can be quite buggy (especially when lagging or when dealing with multiple sprites)
Registering sprite overlaps is the same as finding the closest sprite, but it checks if the shortest distance is also short enough to be overlapping with the player.

Code for the visuals


Have fun spamming B with these ones as well (just looks cool lol)

btw you need better arrays for the sorting block (oh and also sprite utils for like everthing else lol)

21 Likes

“I am a good coder”
“I am a good coder”

Literally anything involving arrays:

13 Likes

that’s why im sharing this, it’s pretty simple but shows the power of arrays!

4 Likes

Wow this is a really detailed tutorial / extension! And very useful, with great graphics! :smiley_cat: :+1:

5 Likes

At least tell us what extensions to download.
Peak tutorial however.

6 Likes

ahem.

3 Likes

Oh right
I’m blind.

3 Likes

Oh no oh no ARRAYS!? I have no clue how these work :sob:

3 Likes

They’re really useful when you get to know them!

7 Likes

OMG I had no idea there were array tutorial TYSM!!!

5 Likes

You’re welcome!! : D I’m glad more people can get to use them now!

4 Likes