• Godot HelpGUI
  • Draw polygons required by custom script in Godot editor

Hi!

Since I have to draw many polygons in the Godot editor, and all objects will have a similar structure and logic to it. Therefore, I created a "scene" which includes all components a prototypical object needs, and tried to glue it together via primitive "tool" code in GDScript.

There should only be one 2D polygon per object, that servers different purposes: It's needed for highlighting (e.g. semi-transparent layer when mouseover occurs), and for collissions. So, I thought, it would be a good idea to export the PoolVector2Array to the most outer object, and set all necessary vectors in the script to it's children, when values are changed.

tool
extends Area2D
#class_name Interactable

##### All Children
onready var SPRITE: Sprite = get_node("Sprite")
onready var COL   : CollisionPolygon2D = get_node("Collision")

var is_mouse_hovered = false

############## Collision Shape
export(PoolVector2Array) var shape setget setshape,getshape 
func setshape(new_value):
	shape  = new_value
	self.COL.polygon = shape

func getshape():
	return shape

Everything seems to function, but I can't draw the polygon in the editor anymore: The polygon-drawing tools aren't showing up. That makes sense, I guess, but how can I have this feature?

Tl;DR: I have a complex scene, which resembles a "ingame object prototype". It exposes some variables to the editor, one of them is a PoolVector2Array. Can I invoke the polygon-drawing capabilities of the Godot editor, to fill the variable?

Thanks in advance?

Post was caught in the moderation queue, also edited the formatting a bit.

Hey, y'all! I just started my first Godot project.

I have nested some objects into a scene, and want to tie them together via a tool script. For example, a 2D polygon, if changed in the Godot editor, needs to be applied to several child-node attributes (e.g. CollisionPolygon2D). Therefore, I exported a PoolVector2Array like this:


tool
extends Area2D

##### All Children
onready var SPRITE: Sprite  = get_node("Sprite")
onready var COL   : CollisionPolygon2D = get_node("Collision")

############## Shape
export(PoolVector2Array) var shape setget setshape,getshape 
func setshape(new_value):
	shape  = new_value
	self.COL.polygon = shape

func getshape():
	return shape

The object hierarchy looks, in this simplified example like this: https://ibb.co/vQnZ82F

Problem is, the PoolVector2Array is normally editable in the editor with the "create points"/"erase points" menu, and you can draw the needed shape onto the whole scene.

How can I make the Godot 2D-polygon editor available for my object? Thanks in advance, I am looking forward to explore Godot further. :)

15 days later

I've had a similar issue. I was setting up a sandbox to do some testing - just a Polygon2D and CollisionPolygon2D in a StaticBody2D for the level, however I wanted to keep the polygon and the collision poly in sync and not have to change the poly every time I changed the collider. So to get around that I used the following code:

func _ready() -> void:
	var poly: Polygon2D = $StaticBody2D/Polygon2D
	var col: CollisionPolygon2D = $StaticBody2D/CollisionPolygon2D
	poly.polygon = PoolVector2Array(col.polygon)

Note that my Polygon2D had no points defined - I would edit only the CollisionPolygon2D, and whenever my scene loaded it would copy the CollisionPolygon2D's polygon into the Polygon2D, thus keeping the two in perfect sync.

I know this isn't exactly what you were dealing with, but hopefully it will help.

I may have read the original post too fast - I don't know that it's possible, at least with GDScript, to add the polygon editing tools onto an arbitrary object. However if the goal is to define a shape once and have it sync between multiple objects then my previous post should help.

Thank you, bitshift-r!

Thats... kind of the solution I went with. It works, but I'm actually not very happy with that. I mean, on the one hand, Polygon2D-esque resources exist. But the editor doesn't let me draw them?

Weird, but in a way, it could be considered consistent, since without a Transform2D the points of the polygon don't have an "absolute coordinate" without an anchor. Maybe that's why idea just shouldn't work. Still- thank you very much for your effort! :)

3 years later