Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | sheepcounter | |
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