Draw polygons required by custom script in Godot editor

notdjbobonotdjbobo Posts: 2Member
edited May 22 in GUI

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?

Answers

  • notdjbobonotdjbobo Posts: 2Member
    edited May 22

    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. :)

  • MegalomaniakMegalomaniak Posts: 2,473Admin

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

  • notdjbobonotdjbobo Posts: 2Member

    @Megalomaniak said:
    Post was caught in the moderation queue, also edited the formatting a bit.

    I am terribly sorry! I thought there was a weird lag, and I accidently double posted.

    https://godotforums.org/discussion/22937/how-to-invoke-the-2d-polygon-drawer-in-the-editor#latest

  • MegalomaniakMegalomaniak Posts: 2,473Admin

    I'll just merge the topics.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file