英文:
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 "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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论