Please review my game code. Is arch good? How to increase performance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robotex

I’m trying to make simple AI for my godot game.

There is my sources:

I’m using GDScript for quick development, I will replace critical parts by C++ code (but I need to find what parts are critical)

I have few questions:

  1. Where is the performance bottle neck?
  2. Is this approach good? Is it good architecture for godot project? Any comments?
  3. Do I have stability problems (memory leaks, memory fragmentation)?
  4. Can I modify GOAP part to increase it performance?
  5. What part should be rewritten?
:bust_in_silhouette: Reply From: Robotex

I found that performance problems caused by this function of pathfinding:

# When called, this method pops the next node off the PQ and examines all
# its edges. The method returns an enumerated value (target_found,
# target_not_found, search_incomplete) indicating the status of the search
func CycleOnce() -> int:
	# if the PQ is empty the target has not been found
	if PQ.empty():
		return TargetNotFound
	# get lowest cost node from the queue
	var NextClosestNode : int = PQ.Pop()
	# put the node on the SPT
	shortestPathTree[NextClosestNode] = searchFrontier[NextClosestNode]
	# if the target has been found exit
	if NextClosestNode == target:
		return TargetFound
	# now to test all the edges attached to this node
	var edges : Dictionary = graph.GetEdgesTo(NextClosestNode)
	for edge_key in edges.keys():
		var edge = edges[edge_key]
		# calculate the heuristic cost from this node to the target (H)                       
		var HCost : float = heuristic.Calculate(graph, target, 
		# calculate the 'real' cost to this node from the source (G)
		var GCost : float = gCosts[NextClosestNode] + edge.cost
		# if the node has not been added to the frontier, add it and update
		# the G and F costs
		if (searchFrontier[] == null):
			fCosts[] = GCost + HCost
			gCosts[] = GCost
			searchFrontier[] = edge
		# if this node is already on the frontier but the cost to get here
		# is cheaper than has been found previously, update the node
		# costs and frontier accordingly.
		elif ((GCost < gCosts[]) and (shortestPathTree[] == null)):
			fCosts[] = GCost + HCost
			gCosts[] = GCost
			searchFrontier[] = edge
	# there are still nodes to explore
	return SearchIncomplete

It created a lot of objects and doesn’t remove it. What is wrong here?