0 votes

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
in Engine by (16 points)

1 Answer

0 votes

I ended up taking out the .CollisionPolygon2D and called the getglobaltransform() and getpolygon() functions on the node directly, but those KinematicBody2D nodes didn't have the getpolygon() 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.

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.