When an Object enters Area2D I expect an object entering to be removed but nothing happens
I have tried queue_free(area) and Area2d.queue_free()
func _on_Area2D_area_entered(area):
queue_free()
area.queue_free
Like said above nothing happens when an object enters Area2d
Ok. I have made a simple project to test the collision and queue_free() process with regards to an interaction between KinematicBody and Area2D. Basically, the Area2D is the object that detects and signals a collision (KinematicBody can cause a collision, but not detect it). Therefore it is up to Area2D to detect when a BODY has entered (or in this case, when it enters a body), and then it can signal the collision, which then calls the _on_Area2D_body_entered function in the other object, and then executes queue_free() as instructed.
My node setup is as follows:
Node2D
|_Area2D - (SIGNAL ATTACHED - "_on_body_entered()" - CONNECTED TO KINEMATIC BODY)
| |_Sprite
| |_CollisionShape2D
|
|_KinematicBody2D
|_Sprite
|_CollisionShape2D
For my trial, the Area2D object is placed on same horizontal axis, and to the left of the KinematicBody
SCRIPT ATTACHED TO AREA2D:
extends Area2D
func _ready():
pass
func _process(delta):
self.position.x += 1 #So it moves into and collides with other object.
SCRIPT ATTACHED TO KINEMATICBODY:
extends KinematicBody2D
func _ready():
pass
func _on_Area2D_body_entered(body): # This is called when Area2D detects a collision
queue_free()
print("HIT!!!!!") #To doubly confirm the collision is recognized
Unfortunately, it kind of has to be this way. Since Area2D is the only object in this scenario which is capable of detecting and signalling the collision, it cannot be ordered to queue_free() itself, as Godot throws an error when attempting to do so.
Alternatively, however, if you wanted it the other way around - for the KinematicBody to move towards the Area2D object, and delete itself upon impact. simply move the whole delta process from the Area2D script and place it in the KinematicBody script above the _on_Area2D_body_entered(body) and change the self.position.x to -= 1. Leave the signal on the Area2D.
Related
Is it possible to invoke a function or connect a signal when the tree is unpaused?
for example I have this setup:
the world script:
extends Node2D
func _ready():
get_tree().paused=true
# do something
get_tree().paused=false
the thing script:
extends Node2D
func _unpaused():
# reset something when the tree is unpaused
is something like this possible?
No, I can't find a signal or virtual method for that.
I'd say use an autoload that controls paused, and make sure everything on the projects that needs paused goes through the autoload. So the autoload can do whatever you need when it changes.
I want to move my Player node (KinematicBody2D) from one scene to another. My code successfully moves Node2D node, but fails with KinematicBody2D node.
I have two Location scenes (inheriting from base Location scene) with the following structure:
FirstLocation (inherits Location)
- YSort
- Player
In the base Location scene I have two methods:
func add_player(player: Player):
get_node("YSort").add_child(player)
func remove_player() -> Player:
var player = get_node("YSort/Player")
get_node("YSort").remove_child(player)
return player
In GameWorld scene I store the possible locations inside a dictionary and the moving of the player happens inside change_location() function:
onready var _locations = {
Location.FOO: $CurrentLocation,
Location.BAR: load("res://locations/Bar.tscn").instance(),
}
onready var _current_location = $CurrentLocation
func change_location(location: int):
var player = _current_location.remove_player()
remove_child(_current_location)
_current_location = _locations[location]
add_child(_current_location)
_current_location.add_player(player)
The switching of the location works
The moving of the player also works in case the player is plain Node2D.
But when Player is KinematicBody2D then the game simply crashes, giving me no hint as to what's causing the problem.
The code works without crashing when I comment out the last line:
_current_location.add_player(player)
...but of course then the player simply doesn't get added to the other scene.
I verified that the player does get removed from the scene.
I tested with a dummy scene (which only contains KinematicBody2D as root node and a simple Sprite as a single child) instead of my actual more complex Player scene to make sure it's not related to any other code I might have in my Player scene. Node2D as root works, KinematicBody2D crashes. Must have been a fluke. Tested again and now both work, so there must be something different in my Player object.
I tried adding the Player node as a direct child of Location node (not having a YSort node in the middle) - nope, still crashes.
I tried setting the position/global_position of player before/after adding it to new scene - no difference.
I'm able to create new Player scene instance and add it to new location.
What might it be in KinematicBody2D that prevents me from moving it from scene to scene?
Found a solution, but I don't know why it works.
I was performing the location change as a response to Area2D.body_entered signal. I triggered my own signal "location_change" and then called the change_location() function in another part of the code in response to it.
Adding a tiny timeout (0.00000001 seconds) before doing the location change solved the issue. However I have no idea why, and I'm pretty sure adding timeouts is not a proper way to solve this problem.
I'm having trouble visualizing the situation. However, given that the problem happens when removing and adding a physics body, and that "body_entered" was involved, and that the solution you found was adding a time out…
Sounds like the issue was removing the physics body while Godot was still resolving physics response. And then the solution was to wait until the next frame.
You could wait until the next graphics frame with this:
yield(get_tree(), "idle_frame")
Or until the next physics frame with this:
yield(get_tree(), "physics_frame")
Which would be better than some arbitrary small timeout.
Alternatively, you could also make the signal connection deferred.
If you are connecting from the UI, there will be an "advanced" toggle in the "Connect a Signal to a Method" dialog. Enabling "advanced" toggle will reveal some extra including making the connection deferred.
If you are connecting form code, you can accomplish the same thing by passing the CONNECT_DEFERRED flag.
I'd like to access the root node of a scene, but even in its _ready() lifecycle method I get a runtime error telling the parent node is busy:
extends Node2D
func _ready():
self.get_tree().get_root().add_child(preload("res://MainMenu.tscn").instance())
# This works:
# self.get_tree().change_scene("res://MainMenu.tscn")
My current script is just changing scene for now. I know I can use this [object NodeTree].change_scene() method or root_node.call_deferred(), but I'd like to be able to observe to when the scene is fully initialized, so I can freely use its node tree.
At the time _ready runs, the parent node is not ready yet (let alone the scene root). Godot has not finished the routine that add children to the parent node. In fact, it just added your current node, the children of your current node, and is running _ready on your current node. After it has done that for your current node and its siblings, it will run _ready on your parent node, and so on, until it reaches the scene root… Then it will be ready.
Lightning_A is correct, inserting yield(get_tree().get_root(), "ready") before manipulating the root will work. yield will halt the execution and schedule it to continue after the given signal. The ready signal in this case. Which happens after _ready completed.
This is how you would use it:
func _ready():
yield(get_tree().get_root(), "ready")
get_tree().get_root().add_child(preload("res://MainMenu.tscn").instance())
Another common solution is to insert yield(get_tree(), "idle_frame") instead. The signal idle_frame happens just before Godot calls _process:
func _ready():
yield(get_tree(), "idle_frame")
get_tree().get_root().add_child(preload("res://MainMenu.tscn").instance())
Does Node's ready signal work for you? i.e. yield(get_tree().get_root(), "ready")
I am trying to get a dynamically instanced kinematicBody2D with an area 2D attached to handle mouse entered/exit inputs. I have created my area 2D with correct collision body, and have tested a similar collision body for detecting some area 2d's and this is working happily, however, the mouse detection is not triggering the function as it should.
I am unsure of why it does not appear to be detecting my mouse. I am assuming I have messed with the Masks incorrectly, and it is not on the same level, however looking at some of the documentation this is not suggested to be a problem.
I am unsure of what code to attach because it is not really coded at this point.
Any help would be appreciated.
To detect mouse events on an Area or KinematicBody, set input_pickable to true and connect to one or more of the provided signals.
KinematicBody2D and Area2D both inherit from CollisionObject2D, so they can both handle mouse input. This means you don't need to add an Area to your KinematicBody unless the area that detects clicks needs to be different than the area that detects collisions (e.g. only a small part of a larger object is clickable).
Here's how you could detect mouse events on a KinematicBody with some CollisionShape:
func _ready():
input_pickable = true
connect("mouse_entered", self, "_on_mouse_entered")
connect("mouse_entered", self, "_on_mouse_entered")
connect("input_event", self, "_on_input_event")
func _on_mouse_entered():
print("mouse entered")
func _on_mouse_exited():
print("mouse exited")
func _on_input_event(viewport, input_event, shape_idx):
var mouse_event = input_event as InputEventMouseButton
if mouse_event:
prints("Mouse button clicked:", mouse_event.button_index)
I am trying my best to build a mobile game in the godot game engine. And i have run into a problem. In a 3D game, how can i detect a mouse click on a specific object or rather where that object is on the screen. I do not understand how to use the control nodes in order to to this.
The easiest way to make a 3D object clickable is to give it a CollisionObject (such as a StaticBody) and connect to the input_event signal. For example, to detect a left-click:
extends StaticBody
func _ready():
connect("input_event", self, "on_input_event")
func on_input_event(camera, event, click_position, click_normal, shape_idx):
var mouse_click = event as InputEventMouseButton
if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed:
print("clicked")
The docs mention that touch events are similar to click events.
Note that input_ray_pickable must be true on the CollisionObject (this is the default).