Solved Nearest entity to location

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

PatoFrango

Active Member
Jul 12, 2017
240
14
18
Hi,

I'm sorry if I'm overposting, but how can I detect the nearest entity to a location/entity? Is there like a "nearest" expression? I've done some research and I saw a few people using that, but it didn't work for me. If there isn't, what would be the best way of doing so? I've tried coding it manually but the moment I thought of using multi-dimensional arrays (which to me seems to be the most logical way), I realized that Skript doesn't have such things. At least as far as I know.
 
code_language.skript:
function nearestEntity(loc: location) :: entity :
    set {_nearest} to first element of all entities in world of {_loc}
    loop all entities in world of {_loc}:
        if distance between loop-entity and {_loc} is less than distance between {_loc} and {_nearest}:
            set {_nearest} to loop-entity
    return {_nearest}
 
code_language.skript:
loop all entities in world of {_player}:
    loop-entity is not {_player}
    set {_dist} to (distance between {_player} and loop-entity *1000) + (random integer between 0 and 999)/1000
    set {_list::%{_dist}%} to loop-entity
broadcast "%the first element of {_list::*}%"

Skript mechanics :emoji_sunglasses:
 
Status
Not open for further replies.