0 votes

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?

Godot version 3.3 stable
in Engine by (12 points)

Can I see the full script?

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)

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)

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 :D

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

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?

Oh okay you don't need to upvote me lol

I will spend my life upvoting you >:D

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.