Changing shape of collisionshape2d while creating new instance.

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

Hello, I have a small problem, I’m creating a few nodes with Area2D → CollisionShape2D and giving them a circle shape.
Every instance should have different collision size.
When I’m doing it by code, area2d and colshape2d are created, but circle shape is not.
When I’m doing it in editor (while making shape unique), everything is existing, but I can’t change radius with code, before and after creating node.

I have tried different scripts, like:

newCircleShape = CircleShape2D.new()
newCircleShape.radius = x
newCollisionShape.shape = newCircleShape

I saw examples on google like this line:
$CollisionShape2D.shape.radius = some_value
should also work, but I’ve tried using it in every possible way and still it’s doing nothing.

Do I have a stroke, or it’s just not possible?

Can I see the full script?

exuin | 2021-04-27 20:27

It’s kinda messy and half of it is deleted, but I can recreate longest version of it by now:

for i in regionsAmount:
    newRegion = regionScene.instance()
    newRegion.set_name(nodeName[i])
    newRegion.position.x = xValue
    newRegion.position.y = yValue
    newArea2D = Area2D.new()
    newArea2D.position = Vector2.ZERO
    newRegion.add_child(newArea2D)
    newCollisionShape = CollisionShape2D.new()
    newCollisionShape.position = Vector2.ZERO
    newCircleShape = CircleShape2D.new()
    newCircleShape.radius = hereItsRadiusValueFloat
    newCollisionShape.shape = newCircleShape
    newRegion.get_child(0).add_child(newCollisionShape)
    self.add_child(newRegion)

Matshiro | 2021-04-27 20:37

Pasted your code into a new project and it worked. What’s regionScene?

Edit: Specifically I used this code, which offsets the collision shapes so I can see them. Maybe all of your shapes are on top of each other?

extends Node2D


func _ready():
	for i in 3:
		var newArea2D = Area2D.new()
		newArea2D.position = Vector2(i * 60, 100)
		add_child(newArea2D)
		var newCollisionShape = CollisionShape2D.new()
		newCollisionShape.position = Vector2.ZERO
		var newCircleShape = CircleShape2D.new()
		newCircleShape.radius = i * 20
		newCollisionShape.shape = newCircleShape
		newArea2D.add_child(newCollisionShape)

exuin | 2021-04-27 20:46

Yeah, it works by offsetting it by some amout, but I was planning to make them in the same place, so now I know that my idea was just not possible.

Well, now I’ve created different solution to my problem without collision shapes (tbh I’m at 50% of making it, but it looks good)

Thank you very much :smiley:

Matshiro | 2021-04-27 21:00

No, your idea is possible. I just offset their locations so I could see them.

exuin | 2021-04-27 21:09

Okay, simple mistake: order of adding nodes.
I was adding nodes in reverse order that’s why it was not working as it should.
Now everything is working again.

So thank you again.
Can I give you upvote or something?

Matshiro | 2021-04-27 21:32

Oh okay you don’t need to upvote me lol

exuin | 2021-04-27 21:34

I will spend my life upvoting you >:D

Matshiro | 2021-04-27 21:37