How to tell code if a variable is increasing or decreasing

Godot Version

4.3

Question

`how do I write a code that makes it so if a variable is increasing then do x and if it decreasing do y I cant make a delayed recording of it to my knowledge so unless its built in I dont know what to do I need it to tell my npc what direction to face im using lerp for direction this is my code:
extends AnimatableBody2D
@onready var raycast = $RayCast2D
var MOVE_SPEED = 1
var inray = false

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

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

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

`

You would have to store the variables state before the change and compare the difference, if I understand correctly you want to check position.x’s change after the lerp, but I believe you can check the difference in location instead.

if location.x - position.x > 0:
    pass # location is relative positive, face right
elif location.x - position.x < 0:
    pass # location is relative negative, face left