一个插件如何在C++中获取一个与项目无关的 HUD 并显示一个小部件?

huangapple go评论72阅读模式
英文:

How does a plugin get a project-agnostic hud to display a widget to in C++?

问题

我正在制作一个插件,我想向用户显示一个小部件。我看到的所有创建C++小部件并将其添加到视口的示例都需要修改游戏模式、玩家角色或其他我无法访问的内容,因为我只是一个插件。

如何在不知道特定C++ HUD类的情况下获取HUD并将小部件添加到视口?

我尝试了几个不同的教程和YouTube视频,但这是一个插件,所以我无法更改玩家控制器,而它们都这样做。

英文:

I'm making a plugin that I want to display a widget to the user. All the examples I see of creating a C++ widget, and adding it to the viewport, require modifying the game mode, or player character, or something that I wouldn't have access to, being a plugin.

How do I go about getting the HUD and adding a widget to the viewport without knowing the specific hud class in c++?

I've tried several different tutorials and youtube videos, but this is a plugin, so I can't change the player controller, which all of them do.

答案1

得分: 1

解决:

#include "Engine.h"
#include "Blueprint/UserWidget.h"

const int PlayerIndex = 0;
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex))
{
    if (UUserWidget* CreatedWidget = CreateWidget<UUserWidget>(Player, MyWidgetClass))
    {
        CreatedWidget->AddToViewport();
    }
}
英文:

Solved:

#include &quot;Engine.h&quot;
#include &quot;Blueprint/UserWidget.h&quot;

const int PlayerIndex = 0;
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex))
{
	if (UUserWidget* CreatedWidget = CreateWidget&lt;UUserWidget&gt;(Player, MyWidgetClass))
	{
			CreatedWidget-&gt;AddToViewport();
	}
}

huangapple
  • 本文由 发表于 2023年4月11日 03:12:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定