Brackey's enemy walking off ledge/empty path solution

Godot Version

4.3

I was following Brackey’s tutorial and I found this post but I had a bit of trouble going trying to make the solution work.

The logic made sense but for some reason it was inconsistent with how things were working. But I managed to fix it by simply adding a timer at the start (on _process not on _ready).
await get_tree().create_timer(0.5).timeout

Heres the whole script for the enemy

extends Node2D
const SPEED = 30
var direction = 1
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_left_path: RayCast2D = $RayCastLeftPath
@onready var ray_cast_right_path: RayCast2D = $RayCastRightPath


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	await get_tree().create_timer(0.5).timeout
	# the simple wall detection from Brackey's
	if ray_cast_left.is_colliding():
		animated_sprite.flip_h = false
		direction = 1
	if ray_cast_right.is_colliding():
		animated_sprite.flip_h = true
		direction = -1
	# my addition to not let it walk into empty blocks/tiles
	if not ray_cast_left_path.is_colliding():
		animated_sprite.flip_h = false
		direction = 1
	if not ray_cast_right_path.is_colliding():
		animated_sprite.flip_h = true
		direction = -1
	if not ray_cast_left_path.is_colliding() and not ray_cast_right_path.is_colliding():
		direction = 0
	position.x += direction * SPEED * delta

Seems pretty versatile, covers a bunch of edge cases too. Just wanted to share in case anyone else got stuck on the same problem

i think this post is better off in the “Resources”-Category

Awaiting a timeout seems to be a hackish solution. What was the problem in the first place?
As an aside, ray-casts only update in the physic-frames. So you could/should move the code to _physics_process for it to be frame rate independent.