Getting closest pair of floats from preset values

Godot Version



I’m using a procedural tiled terrain generation with FastNoiseLite (with 2d noise sampling). Besides the terrain generation in itself, I have two more noisemaps called tileHazard(range -1 – 1) and tilePower(range 0 – 1) for changing the tile’s type. Take for example: if a tileHazard is high, i would place a hazardous trap tile that would decrease player’s health if he steps on it, but then also ive got a power noise map which i can use to upgrade or downgrade that trap tile, if the power is low, then its just a small spike that can be survived when stepped on, however if the power is at its maximum, it could instantly kill the player.

The tiles themselves are stored in a resource “tileSet” with variable power and hazard variables:

I need to compare the sampled values of the hazard and power noise maps to all of the tiles’s hazard and power values and find the closest matching pair of those to then get a tile by its hazard and power values to place at the location (or another solution which you may figure out)

edit: found a solution, writing code…

I dont know for sure whenever this is a good solution or not, but it fits my desires and works how i intended:

here, instead, im comparing the “summed distance” between every tile’s hazard and power and the sampled hazard and power from the noise map. This code can definitely get optimized but you can see and understand it a bit better like this