Hi, i have some problem with my widget.
this is ‘WBP_NewItem’ widget
and check ‘is variable’
in class setting, set parent class ‘NewItemWidget’
//NewItemWidget.h
class OPENWORLDRPG_API UNewItemwidget : public UUserWidget
{
UPROPERTY(EditDefaultsOnly, Category = "WidgetVariable", meta = (BindWidget))
USizeBox* BGSizeBox;
UPROPERTY(EditDefaultsOnly, Category = "WidgetVariable", meta = (BindWidget))
UBorder* BGBorder;
UPROPERTY(EditDefaultsOnly, Category = "WidgetVariable", meta = (BindWidget))
UImage* ItemIcon;
void Refresh();
};
and this widget created by ‘NewInventoryGrid’
this is also derived from UUserWidget class.
//NewInventoryGrid.cpp
void UNewInventoryGrid::RefreshInventory()
{
//...
const auto ItemsMap = InventoryComp->GetAllItems();
for (auto ele : ItemsMap)
{
//Create NewItemWidget, but all the bind variable is null.
UNewItemwidget* ItemWidget = CreateWidget<UNewItemwidget>(GetOwningPlayer(), UNewItemwidget::StaticClass());
if (ItemWidget != nullptr)
{
ItemWidget->OnRemoved.AddUFunction(this, FName("OnItemRemove"));
//Initialize variable
ItemWidget->Tilesize = TileSize;
ItemWidget->ItemObj = ele.Key;
ItemWidget->Refresh();
//...
}
}
}
here is debug
Right after create widget.
And this is after Initialize variable.
how can i use BindWidget with CreateWidget??
Natalo77
(Natalo77)
January 28, 2022, 11:44am
2
You’ve declared that the objects are there, but you haven’t actually created them in the construction code for your widget
You should use this in
RebuildWidget
Try changing your header to this, what you have should work:
#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
UCLASS()
class OPENWORLDRPG_API UNewItemwidget : public UUserWidget
{
GENERATED_BODY()
public:
//your stuff ...
i tried changing in both header, but it didn’t work.
(NewItemWidget.h, NewInventoryGrid.h)
You need to do the following:
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets") TSubclassOf<class UUserWidget> HealthWidgetTemplate;
private:
UPROPERTY() UHealthWidget* HealthWidget;
Then in your code you can create it:
if(HealthWidgetTemplate){
HealthWidget = CreateWidget<UHealthWidget>(this, HealthWidgetTemplate, FName("MyName"));
}
//MyController.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets | Inventory")
TSubclassOf<UUserWidget> WItemwidget;
//NewInventoryGrid.cpp
void UNewInventoryGrid::RefreshInventory()
{
//...
UNewItemwidget* testwidget = ConstructWidget<UNewItemwidget>(MainCon->WItemwidget);
if (testwidget != nullptr)
{
testwidget->Tilesize = TileSize;
testwidget->ItemObj = ele.Key;
}
//...
}
I put that code in NewInventoryGrid.h but still the variable null.
did i do it right?
Try CreateWidget<> instead of ConstructWidget<>
CreateWidget<UNewItemwidget>(this, MainCon->WItemwidget, FName("whatever"));
//NewInventoryGrid.h
UPROPERTY(EditDefaultsOnly, Category = "WidgetVariable")
TSubclassOf<UUserWidget> WNewItemWidget = nullptr;
UNewItemwidget* newitemwidget;
//NewInventoryGrid.cpp
void UNewInventoryGrid::RefreshInventory()
{
//...
newitemwidget = CreateWidget<UNewItemwidget>(this, WNewItemWidget);
if (newitemwidget)
{
newitemwidget->Tilesize = TileSize;
newitemwidget->ItemObj = ele.Key;
newitemwidget->Refresh();
}
}
//NewItem.h
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "NewItemwidget.generated.h"
class OPENWORLDRPG_API UNewItemwidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly)
UNewItemObject* ItemObj;
UPROPERTY(BlueprintReadOnly)
float Tilesize;
UPROPERTY(BlueprintReadOnly)
FVector2D widgetsize;
//...
UPROPERTY(BlueprintReadOnly, Category = "WidgetVariable", meta = (BindWidget))
USizeBox* BGSizeBox;
UPROPERTY(BlueprintReadOnly, Category = "WidgetVariable", meta = (BindWidget))
UBorder* BGBorder;
UPROPERTY(BlueprintReadOnly, Category = "WidgetVariable", meta = (BindWidget))
UImage* ItemIcon;
i put NewItem widget template in NewInventoryGrid widget.
but still bindwidget variable is null.
OOOOOOOHHHHHHHHHHH WOW!!
thank you for solution.
i was putting native widget file in widget template variable.
omg. i’m so stupid.
Thank you for your reply. thank you thank you.
i hope you’ll always be happy.
TOTAL longshot here with the time since you posted this. but it looks like you are working over the same tutorial as me, for a spatial inventory. Converting from the blueprint in the tutorial to cpp as I am. Any Chance I could see all of what you wrote for the conversion? It would be super helpful for me to see a working cpp version.
PetCat
(PetCat)
April 18, 2024, 2:07pm
11