I did want better enemys ai, so i attempt to smell tracker for enemy.

Player left behind itself scent pieces and when enemy got close to one piece, will know the location of the player and go there.

on my player node this code handle the scent issue (i did use timer for it) :

var trail
var scent_array = []

onready var scent_sprite = preload ("res://sahneler/scent_test.tscn")

func _on_scent_timer_timeout():
	trail = scent_sprite.instance()
	add_child(trail)
	trail.set_as_toplevel(true)
	trail.global_transform.origin = target_position_3d
	add_to_group("scent")
	scent_array.append(trail)

output: https://streamable.com/a97aui

but, for enemy to notice the scents, i need to get the distance between enemy and every scent piece, so i did write this down: (this code on the enemy node)

var max_dis:float = 15.5

	var scent_trail = get_parent().get_node("../karakter").scent_array
	for scent in scent_trail:
			var enemy2scent = translation.distance_to(scent.translation)
			if (enemy2scent < max_dis):
				print(enemy2scent)

output: https://streamable.com/0al426

well i have a few questions. as you guys can see at the last video, objects on the scene start the glich, frozen and lag, well came close to broken, im not sure why? cause of less memory on the compiter? to much output?

and other question is, for the distance between enemy and scent pieces, i did realise after write it down but my enemys on the scene get instance so after instance there will be 5 diffrent objects, so this line of code :

var enemy2scent = translation.distance_to(scent.translation)

can get the all instanced enemys positions to scent pieces? or should i get all distanced objects positions and scent positions and distance between them?

i also have a little idea how to get the vector data using scent pieces for bait the enemies to player position.

long story sort, enemys must notice the scent pieces and with it, position of the player, and go there. Thank you for time to read this point :)

...frozen and lag, well came close to broken, im not sure why? Almost certainly because you are trying todo multiple simultaneous distance calculations per frame (to confirm this would be evident in the profiler where you can see how much delay is caused by script processes). Secondarily, your trying to print all of these checks to the console every frame as well which will be similarly impactful. Don't ask me why, but the print function is suprisingly slow.

The way I usually workaround this is to put a delay on the function as much as feasible using a simple counter, as the checks do not need to happen every ~1/60th of a second and can often be as slow as 1/6-1/2 second for AI-like behavior (human reaction time is 1/5th a second when making a firing decision for example), something like (completely from memory/untested):

var phys_delay 
var counter = 0

func ready():
# If we have multiple enemies spawning we don't want them all using the same frame, so assign a delay somewhere around ~half a second but random. Not the best way to do it, as really should track with an array to ensure it is balanced evenly but this will do for most cases. Also may not be necessary if they are spawned at intervals anyway
    randomize()
    phys_delay = randi()%35 +25 

func _physics_process(_delta):
    # ~check every half second
	if counter == phys_delay: 
           # run your scent function here and if necessary your logging
		scent_follow()
		counter == 0
	counter+=1

I try to do this as much as possible for anything that isn't going to be noticed by the person playing. Hell, I have a function that moves the moon in an outdooor night scene to keep it correctly aligned with the player that is delayed, but the only scenario the player gets fast enough to notice would be them falling off a cliff.

Beyond this, setting up a grid system where scent objects register themselves within a grid to limit the amount that the enemy needs to check, and beyond this, multithreading the checks (which while I've implemented, it is completely unclear to me if it is having any effect due to the oddities of threading lol).

Whoops totally just noticed, I think you could also reduce the checks once the enemy has detected a single scent piece (maybe you already do this?) in that at this point the enemy is aware of the player and the closest piece so no longer needs to find all the other pieces. Instead, they now have the position in the 'scent_trail' array where they became aware of the player and can path to the points moving up the scent_trail index?

Made sense in my head, now I've written it, sounds weird lol.

Well, thank you. i did as you say and script now make less calculation. as a result, it gone smoot and butter. And also did remove the scent tail with a timer, so calculations become much more less.

Now i will research the grid system you mention it, and do something about it. thanks.

well because of it was untested , i did a little change

if counter == phys_delay: 
	scent_follow()
	counter = 0  # this one changed.
counter+=1

and also i raise the delay time. thank you for your time again.

output:

https://streamable.com/wj96v1

2 years later