Your original code was only missing this to pair with lines 10, 17, 23, and 29:
var topLeft = duplicate()
#create a new resource by duplicating the current
topLeft.shape = topLeft.shape.duplicate(true)
var topRight = duplicate()
#create a new resource by duplicating the current
topRight.shape = topRight.shape.duplicate(true)
var bottomLeft = duplicate()
#create a new resource by duplicating the current
bottomRight.shape = bottomRight.shape.duplicate(true)
var bottomRight = duplicate()
#create a new resource by duplicating the current
bottomRight.shape = bottomRight.shape.duplicate(true)
Because all your code relied on a shape that was being shared each time the code was run by multiple objects, you got an unexpected result. You need to use duplicate(true) to create a new Resource that is unique to that object.
Godot Docs: Resource : Method - duplicate()
If you were to move this code up from the CollisionShape2D
while keeping the same node Hierarchy, you'd simply update your paths to...
old_extents = shape.extents
becomes
old_extents = $CollisionShape2D.shape.extents
bottomRight.shape = bottomRight.shape.duplicate(true)
becomes
bottomRight.get_node("CollisionShape2D").shape = bottomRight.get_node("CollisionShape2D").shape.duplicate(true)
If you separate the sprite from the CollisionShape2D like your newer code implies, you'd just repeat the same positioning, duplicating and scaling code for it.
An optimization to the code would be to not make topLeft, topRight, bottomLeft, and bottomRight from scratch.
All 4 of these objects are the same. Make 1 new object (I called mine Clone
) to make your edits (new shape, new shape dimensions), and duplicate that object 3 more times.
This example is going off of the original code:
#make the first clone
var clone1 = duplicate()
clone.shape = clone.shape.duplicate(true)
clone.shape.extents = clone.shape.extents / 2
#make the rest of the clones
var clone2 = clone1.duplicate()
var clone3 = clone1.duplicate()
var clone4 = clone1.duplicate()
To optimize further - don't even make 4 variables, make 2.
1. Make Stamp - define it's parameters:
var Stamp = duplicate()
Stamp.shape = Stamp.shape.duplicate(true)
Stamp.shape.extents = Stamp.shape.extents / 2
2. Make Clone from Stamp: var Clone = Stamp.duplicate()
3. Give it the top left position (which your code uses completely indepentent code for). get_parent().add_child(Clone)
4. Clone = Stamp.duplicate()
5. Give clone (which is now a new duplicate of Stamp) the top right position
6. get_parent().add_child(Clone)
7. repeat steps again for bottom left
8. for bottom Right - you no longer need Stamp, so set Stamp to bottom right position
9. get_parent().add_child(Stamp)
Now you have 4 children, and you used 2 variables to hold instances, rather than 4.
These 4 objects can share the same shape, because the necessary method to make your code work is to not edit the original shape, but to make a new shape and use the previous shape's dimensions as a reference for positioning. If each individual object needs to be able to subdivide only itself, then each object would need to create a new shape for itself.