RealDocs

UUserWidget::SetPositionInViewport

function UMG Blueprint Since 4.7
#include "Blueprint/UserWidget.h"
Access: public Specifiers: UFUNCTIONBlueprintCallableBlueprintCosmetic

Description

Sets the position of this widget within the viewport. Must be called after AddToViewport or AddToPlayerScreen; has no effect on widgets not yet in the viewport.

Caveats & Gotchas

  • bRemoveDPIScale=true is almost always correct when you obtained Position from UMG coordinates or from ProjectWorldLocationToWidgetPosition. Set it to false only if you have already manually accounted for DPI.
  • This function has no effect until the widget has been added to the viewport via AddToViewport or AddToPlayerScreen. Calling it before adding will silently do nothing.
  • The position sets the widget's top-left corner by default. To anchor the widget to its center, call SetAlignmentInViewport(FVector2D(0.5f, 0.5f)) before or after calling this.

Signature

UMG_API void SetPositionInViewport(FVector2D Position, bool bRemoveDPIScale = true)

Parameters

Name Type Description Default
Position FVector2D The 2D position to place the widget in the viewport.
bRemoveDPIScale bool When true (default), the engine divides Position by the DPI scale so that the visual result matches the requested widget-space coordinate after DPI scaling is applied. true

Return Type

void

Example

Follow a world actor with a widget marker C++
FVector2D ScreenPos;
bool bOnScreen = UWidgetLayoutLibrary::ProjectWorldLocationToWidgetPosition(
    PlayerController, TargetActor->GetActorLocation(), ScreenPos, false);
if (bOnScreen)
{
    MarkerWidget->SetPositionInViewport(ScreenPos); // bRemoveDPIScale=true by default
}

Version History

Introduced in: 4.7

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.