Generated Area2D "area_enter" does not connect to script / Collision2Dshape extends incorrectly?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sheepcounter
:warning: Old Version Published before Godot 3 was released.

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")

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

tiernich | 2016-04-02 19:26

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.

sheepcounter | 2016-04-02 21:30

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

Akien | 2016-04-05 11:16

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?

Akien | 2016-04-05 11:18

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.

sheepcounter | 2016-04-05 19:59

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.

saguaro | 2016-04-09 20:53

:bust_in_silhouette: Reply From: alexholly

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() )