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()
        kine.set_name("barrelStart")
        var a2d= Area2D.new()
        a2d.set_name("barrelCollision")

        var col2d = CollisionShape2D.new()
        var rec2d = RectangleShape2D.new()
        rec2d.set_extents(Vector2(10,10))
        col2d.set_shape(rec2d)
        col2d.set_name("barrelCollisionShape")
        a2d.add_child(col2d)

        var sprite = get_node("spr_barrel")
        add_child(kine)
        kine.add_child(a2d)
        remove_child(sprite)
        a2d.add_child(sprite)
        kine.set_pos(Vector2(320,0))
        sprite.show()

        get_node("barrelStart/barrelCollision).connect("body_enter",self,"barrel_entered") 



func barrel_entered( body ):
    print("IT HAS BEEN ENTERED INTO")
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).

Tree

|Sprite(script)
|-Area2D
|--CollisionShape2D(add dummy shape)

Script

var ready = false
func _ready():
    if(!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.