问题
How can i call a c++ variable inside a blueprint. I have created a widget blueprint and i have to display a variable value in a text block inside that blueprint. The variable is obtained from c++. Is it possible.? Please help.....
回答1:
Use the UPROPERTY macro to specify properties for the next variable you declare. In this case you may want to use the BlueprintReadOnly property.
So if your variable declaration looks like this:
int widgetValue
You have to add this:
UPROPERTY(BlueprintReadOnly)
int widgetValue
This will make the variable readable in blueprints but it won't allow you to modify it through those means.
回答2:
Here is an example of how I manage my Widgets in Unreal Using C++
You want to create a custom widget to base your widgets on. To do this create a C++ class that inherits from UUserWidget:
// Copyright 2014-2016 Blackbriar Softworks, Inc. All Rights Reserved.
#pragma once
#include "Blueprint/UserWidget.h"
#include "XUserWidget.generated.h"
/**
*
*/
UCLASS()
class UNSUNGHEROES_API UXUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XConfiguration")
FString WidgetName;
};
Inside the editor you want to now reparent your widget that you want to have access to custom properties:
Open Widget -> File -> Reparent Widget -> Your C++ Widget Class Name
now you can access the WidgetName inside the blueprint widget or use data binding to display the values./
This is where you expose the property to blueprints:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XConfiguration") FString WidgetName;
来源:https://stackoverflow.com/questions/35551229/how-to-call-a-function-from-c-to-a-widget-blueprint-in-unreal-engine