Getting variable from a child of a node in array (and some Nav2D)

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

Hello there - beginner here, and I thank you in advance for the help.

I’m working on cutting out polygons from a NavigationPolygonInstance, and I’m trying to make a multiuse() function out of the singleuse() function from this tutorial: https://www.youtube.com/watch?v=uzqRjEoBcTI

I’ve tagged my KinematicBody2D nodes as “Defenders” and they are in a YSort and I have the following script attached to my Navigation2D on the same level as the YSort.

I get all of the nodes grouped as “Defenders” and put them into my DefendersOnField array. I then pass that array into my multiuse(array) function.

Now my problem is the get_polygon() function on the line

var polygon_bp = DefenderNodes[i].CollisionPolygon2D.get_polygon()

I’m guessing that’s not how I’m supposed to get the child of the node I’m working on, but I am not sure.

The error I’m getting on that line is "Invalid get index ‘CollisionPolygon2D’ (on base: ‘KinematicBody2D (Entity-Defender.gd)’).

extends Navigation2D

func _ready():

	var DefendersOnField = get_tree().get_nodes_in_group("Defenders")
	multiuse(DefendersOnField)

func multiuse(array):
	var DefenderNodes = array

	for i in DefenderNodes.size():
		var newpolygon = PoolVector2Array()
		var polygon = $NavigationPolygonInstance.get_navigation_polygon()
		var polygon_transform = DefenderNodes[i].CollisionPolygon2D.get_global_transform()
		var polygon_bp = DefenderNodes[i].CollisionPolygon2D.get_polygon()
		for vertex in polygon_bp:
			newpolygon.append(polygon_transform.xform(vertex))
		polygon.add_outline(newpolygon)
		polygon.make_polygons_from_outlines()
		$NavigationPolygonInstance.set_navigation_polygon(polygon)

	var navPolyInstance = $NavigationPolygonInstance
	navPolyInstance.enabled = false
	navPolyInstance.enabled = true
:bust_in_silhouette: Reply From: panderbeers

I ended up taking out the .CollisionPolygon2D and called the get_global_transform() and get_polygon() functions on the node directly, but those KinematicBody2D nodes didn’t have the get_polygon() function. So I filled the array DefenderNodes with the CollisionPolygon2D’s of the defenders instead of the defenders themselves.

It took away the error so I consider this solved, but this code doesn’t work as intended. Still something wrong in case someone is trying to use this.

edit: please ignore this comment

Galvatron | 2022-08-11 17:54