I want to know why this code gives an error. Why is it not possible for me to extend a Shape2D using a Collide2D? I’m creating a grid of StaticBody, however, creating several StaticBody and adding weights can be heavy. So, I created just one, which contains several CollisionShape. However, creating several CollisionShapes and adding them as children also weighs. So, the solution would be a Shape2D that I can add squares to at runtime. However, I can’t create a new Shape2D class.
It’s not possible to implement custom Shape2Ds as they are just an abstraction over the PhysicsServer2D multiple shape functions. If you want to create a grid of RectangleShape2D at runtime then do it by creating them and adding CollisionShape2D nodes to the body like:
extends StaticBody2D
@export var rows := 5
@export var cols := 5
@export var cell_size := Vector2(16, 16)
func _ready() -> void:
for col in rows:
for row in cols:
var shape = RectangleShape2D.new()
shape.size = cell_size
var collision_shape = CollisionShape2D.new()
collision_shape.shape = shape
collision_shape.position = cell_size * Vector2(col, row)
add_child(collision_shape)