Saving collision polygon as a resource.

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

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.

:bust_in_silhouette: Reply From: Mak

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

:bust_in_silhouette: Reply From: timothybrentwood

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:

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

magicalogic | 2021-05-04 19:31

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

timothybrentwood | 2021-05-04 20:14

Okay, thanks.

magicalogic | 2021-05-04 20:27