Get relative or screen space location of Image in a Widget Blueprint.

Alright this ended up being my solution:

I created a FVector2D variable in the PlayerController which holds the CrosshairRelativeLocation. I put it here because I couldn’t figure out how to access the Game Level variables through the widget but the widget does have a reference to the PlayerController.

In the widget I then went to the graph and added what can be seen in the image:

Then in my PlayerController I have a function for doing the line trace:



bool AFirstPersonController::LineTraceSingleByChannelFromCrosshair(FHitResult& OutHit, float DistanceToCheck, ECollisionChannel TraceChannel) const
{
    int32 ViewportSizeX, ViewportSizeY;
    GetViewportSize(ViewportSizeX, ViewportSizeY);

    FVector WorldLocation, WorldDirection;
    DeprojectScreenPositionToWorld(ViewportSizeX * CrosshairRelativeLocation.X, ViewportSizeY * (1.f - CrosshairRelativeLocation.Y), WorldLocation, WorldDirection);

    FCollisionQueryParams Params = FCollisionQueryParams(FName(TEXT("")), false, GetOwner());
    if (GetPawn())
    {
        Params.AddIgnoredActor(GetPawn());
    }

    DrawDebugLine(GetWorld(), WorldLocation, WorldLocation + WorldDirection * DistanceToCheck, FColor::Red, true);

    return GetWorld()->LineTraceSingleByChannel(
        OutHit,
        WorldLocation,
        WorldLocation + WorldDirection * DistanceToCheck,
        TraceChannel,
        Params
    );
}


This works just fine, the problem with this however is that it’s bound to Tick. It really doesn’t need to update the location of the crosshair every tick but I couldn’t find another way to get the geometry, the ones I found just returned 0,0. Any tips on how to do this in like a BeginPlay or something like that?

Edit:
So I fixed the tick thing with using On Initialized event and using Get Viewport Widget Geometry -> Get Local Size which worked just fine.