+1 vote

How do I save a collision polygon as a resource to be loaded later in code.
I am trying to change the collision polygon of an Area2D when I change the sprite to a different one.

Godot version godot 3.2.3
in Engine by (2,017 points)

2 Answers

0 votes
Best answer

Right click your one of your folders in the File System section of your editor New Resource -> ConvexPolygonShape2D or ConcavePolygonShape2D. Define your polygon in the editor per usual and save the resource. Use a CollisionShape2D not a CollisionPolygon2D and load the polygon resource you created. In code you're probably going to need to do something like this:

var sprite_polygon_dictionary = {}
onready var my_sprite = $Sprite
onready var my_collision_shape = $CollisionShape2D

func load_sprite_dictionary(sprite_texture_array, polygon_paths_array):
    for index in range(sprite_texture_array.size()):
        sprite_polygon_dictionary[sprite_texture_array[index]] = load(polygon_paths_array[index])

func change_to_sprite(sprite):
    var polygon = sprite_polygon_dictionary.get(sprite)
    if polygon:
by (3,900 points)
selected by

When I create a new resource and double click it its not opening in the editor

It opens in the Inspector panel (default on the right side) like where you would edit positions of nodes in the editor.

Okay, thanks.

0 votes

A collision polygon is simply a node that can be saved and instantiated as a scene. If you want to save only the "polygon" property (PoolVector2Array), then I do not know how easy it is to do this without additional file-handling code, but it does not make sense, since only points will remain in the save file anyway, you can make sure of this by saving the node (right-click on the node in the list of scene tab nodes -> save the branch as a scene) and opening the scene in a text editor

by (90 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 Frequently asked questions and 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 [email protected] with your username.