Godot Version
4.3
Question
I am trying to spawn an Entity, but before it gets drawn to the screen I want to make sure that it does not overlap with another Entity.
Currently I have this, the entity that will be spawned:
class_name Gate
extends Node2D
@onready var sprite = $Sprite2D
func get_size():
var size: Vector2 = sprite.texture.get_size()
return size * transform.get_scale()
And this, which spawns the entity:
extends Node2D
## This is called when its time to check whether or not entities overlap
func do_gates_overlap(gate1: Gate, gate2: Gate):
var rect1 = Rect2(gate1.position, gate1.get_size())
var rect2 = Rect2(gate2.position, gate2.get_size())
return rect1.intersects(rect2)
As you can see, the do_gates_overlap
function needs to access the get_size
function of Gate, which in turn needs to access the Sprite2D
node of the scene. However, when instantiating the Gate
its _ready
function has not yet been called, which means Sprite2D
is not yet available, causing the other functions to crash.
I have tried calling the _ready
function manually after instantiating the entity, but that will cause problems since the function is then executed twice (once by me, once by the engine). It is not shown in the code above, for better clarity, but the _ready
function contains some signal registration, which errors when it is executed more then once.
My question: How would I go about checking whether or not my entities overlap, without using any nodes? Or alternatively, how can I disable the automatic execution of the ready function?