Is there a better way of your sprite fliping to the side your mouse is on

Godot Version

4.3

Question

so I want my 2d platformer character to face the same side as my mouse so ive set up 2 area2ds and if it goes in the left one then it flips left right one flips right is there a better way of doing it?

There is no need for areas and collision checking.

You can get the mouse position with get_global_mouse_position (CanvasItem — Godot Engine (stable) documentation in English) or get_viewport().get_mouse_position() (get_viewport().get_mouse_position()) and compare the position on the x-axis with your characters.

when I put it in a variable then try print it it throws a error saying its returning a null value

extends Node

var mouse = get_viewport().get_mouse_position()

Called when the node enters the scene tree for the first time.

func _ready() → void:
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
print(mouse)

First, you get the error because you are trying to get the viewport before the node was added to the scene-tree. Therefore it is not in the viewport yet and get_viewport returns null.
If you want to get the mouse position in the beginning you can move it to the _ready function. But then you only get the mouse position once in the beginning.

And this is not what you want. What you want is to get the current mouse position every frame (i.e., in _process) and flip the sprite based on it.

I want to declar the variable in the _ready but it throws an error if I try print it in process

extends AnimatableBody2D
@onready var raycast = $RayCast2D
const MOVE_SPEED = 130
@onready var sprite = $AnimatedSprite2D

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _ready() → void:
var mouse = get_viewport().get_mouse_position()

func _process(delta: float) → void:
var location: Vector2 = raycast.get_collision_point()
if raycast.is_colliding():
sprite.play(“run”)
position.x = move_toward(position.x, location.x, delta * MOVE_SPEED)

if raycast.is_colliding() == false:
	$AnimatedSprite2D.play("idle") 

if location.x - position.x > 0:
	sprite.flip_h = false
elif location.x - position.x < 0:
	sprite.flip_h = true
	
print(mouse)

Variables are “scoped” meaning they are accessable to deeper, further indented parts of the code, but not to outer un-indented code. The _ready and _process function do not share variables.

The variable must be declared outside, assigned within _ready (or using @onready) then it can be used each frame. Another issue in the sample is that get_mouse_position would be copied once and never updated, it must be called each time you intend to use the mouse position.

To detect which side of the screen it’s on you would also need to get the viewport’s size.

@onready var viewport := get_viewport()

func _process(delta: float) -> void:
    var mouse: Vector2 = viewport.get_mouse_position()
    var size: Vector2 = viewport.size

    var is_on_right_side: bool = mouse.x > size.x / 2.0

thanks alot