0 votes

So I'm having some difficulties connecting body_enter of a script-generated Area2d, to the main script. Basically, I have a function scripted to initialize a barrel (kinematicbody2d, sprite, area2D, collision and collision shape). Would the problem be that a node cannot be connected at run time? Or maybe I'm not creating a CollisionShape2D correctly? If anyone could enlightenment me on these issues, I'd be very happy!

       func _spawnBarrel():
        var kine = KinematicBody2D.new()
        var a2d= Area2D.new()

        var col2d = CollisionShape2D.new()
        var rec2d = RectangleShape2D.new()

        var sprite = get_node("spr_barrel")


func barrel_entered( body ):
in Engine by (12 points)

is the area monitorable? looking here in my examples all area2D has monitoring enabled

Thanks for the tip, althought set_monitoring(true) did not give me an output, I did not know about it so this gives me hope Ive missed something obvious.

First issue seems to be that there is a syntax error on your get_node() call, it's lacking a closing double quote.

Out of curiosity, is there a specific reason while you want to build the whole scene from script instead of simply instancing a barrel from a PackedScene?

The script is meant to create all objects, to see how far we can push object creation using gdscript. Using packed scenes would be cheating. I still have not figured out how to create a functional Area2D via script, but it'll come. I'd like to add I ran the game with collisions visible, and confirmed it is present and follow the parent.

I am also having trouble creating a CollisionShape2D with gdscript and found this topic while searching for advice. If you solve it and post the answer, I'd greatly appreciate it.

1 Answer

0 votes

I was looking for a way to replace(with GDScript) the size of an RectangleShape2D that is a shape of an CollisionShape2D.
I think this solution works(at least for my case).


|--CollisionShape2D(add dummy shape)


var ready = false
func _ready():
        get_node("Area2D").connect("area_enter", self, "_on_Area2D_area_enter")
        get_node("Area2D").connect("area_exit", self, "_on_Area2D_area_exit")
        get_node("Area2D").connect("input_event", self, "_on_Area2D_input_event")
        ready = true

    var rect = RectangleShape2D.new()
    var tex_size = get_texture().get_size()

    rect.set_extents( tex_size/2 )
    var old_shape = get_node("Area2D/CollisionShape2D").get_index()

    get_node("Area2D/CollisionShape2D").set_shape( rect )
    Physics2DServer.area_set_shape( get_node("Area2D"), old_shape, rect.get_rid() )
by (175 points)
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.