How to cast C++ variable to another Blueprint class

Hello!

How to cast C++ variable to another Blueprint class?

I create C++ Actor with some calculation and put it to a scene. Let’s name it CodedActor.

CodedActor.h
CodedActor.cpp

CodedActor contain some TArray as a result of some function.

I create blueprint widget with some menu. There is another class. Let’s name it BlueprintMenuWidget.

How can I use variables (my TArray) from the CodedActor in the BlueprintMenuWidget?

I have some problems with the implementation due to different classes of objects.
I believe, it’s typical thing, however I can’t find an answer.

Thanks in advance!

Either your BlueprintMenuWidget has to have a reference to your CodedActor, or the other way around. If you have this reference in Blueprint (or pointer in C++) you can access all of the (public) members of the object.

So lets say you have a reference to your CodedActor in you BlueprintMenuWidget. You could do this by having the Actor create the widget. This ways the Actor can set the reference insinde of the Widget.

When the widget wants to access members of the CodedActor (your TArray in this case) it can just do so by using the reference that it has to the actor. If you declare the TArray like this in your actor:

UPROPERTY(BlueprintReadWrite)
  TArray<type> ArrayName;

You can just drag a node off of the reference to the actor in blueprint, type ArrayName, and voila you have your array.

what error is that?

Yes. I did this. Unfortunately the Actor’s variable in blueprint returns error.

I can’t understant why it doesn’t work.

LanguageActor.h

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Language)
TArray<FString> LanguageVariableNameArray;
	
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Language)
TArray<FString> LanguageNameArray;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Language)
FString TestLanguage = "text test";

LanguageActor.cpp

void ALanguageActor::BeginPlay()
{
	Super::BeginPlay();
	
	// Read from file
	if (PlatformFile.DirectoryExists(*ReadDirectory))
	{
		// Get absolute file path
		FString AbsoluteFilePath = ReadDirectory + "/" + FileName;
		FFileHelper::LoadFileToString(TextToRead, *AbsoluteFilePath);
	}
	else GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("Directory unexist"));
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TextToRead);
	
	
	if (TextToRead.ParseIntoArray(LanguageFileArray, TEXT("\n"), true))
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, LanguageFileArray[1]);
		if (LanguageFileArray[0].ParseIntoArray(LanguageVariableNameArray, TEXT(";"), true))
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, LanguageVariableNameArray[3]);
		if (LanguageFileArray[1].ParseIntoArray(LanguageNameArray, TEXT(";"), true))
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, LanguageNameArray[3]);
	}
}

Can you please show the code where you set the LanguageVar? It is going to be null in the beginning, so if you dereference it before you set it you might run into errors. Also: please post the error message you get, or screenshot it so we can see what’s the problem.

Dear pulp_user,

I did’t set the LanguageVar. May be there is problem.
Where and how should I set it?

Error Blueprint Runtime Error: Accessed None trying to read property LanguageVar from function: ‘ExecuteUbergraph_MainMenu’ from node: Print String in graph: EventGraph in object: MainMenu with description: Accessed None trying to read property LanguageVar

Lets say you create the widget in your Language actor like this:

BlueprintMenuWidget* widget = CreateWidget<BlueprintMenuWidget>(GetWorld(), BluePrintMenuWidgetClass);

You then have a reference to your widget. If you have the LanguageVar variable of your widget declared in C++ like this:

UPROPERTY(BlueprintReadWritable)
LanguageActor* LanugageVar;

you can simply set it like this:

widget->LanguageVar = this.

The variable will still appear in blueprint so you can use it.

I still dont’ got it…
May be you can explane it a little more easy.
I have used C++ 20 yeas ago. Allthing is forgotten :slight_smile:

I have added to LanguageActor.h

UCLASS()
class DREAMPAX_API ALanguageActor : public AActor
{
 GENERATED_BODY()
	
 public:	
 UPROPERTY(BlueprintReadWritable)
 ALanguageActor* LanugageVar;
}

and had had a compilation fail.

Next lines:

BlueprintMenuWidget* widget = CreateWidget<BlueprintMenuWidget>(GetWorld(), BluePrintMenuWidgetClass);

and

widget->LanguageVar = this;

initiate a syntacsys error.

Could you explane where each pice of code should be.
May be after that I’ll get how it works.

I created just “LanguageActor.h”, “LanguageActor.cpp” and one Blueprint “MainMenu”.

Thanks in advance.

Thanks. I’ll try.

You need a C++ class that derives from Widget, lets call it BlueprintMenuWidgetBase. Your BlueprintMenuWidget then needs to derive from BlueprintMenuWidgetBase, instead of Widget. You have to mark BlueprintMenuWidgetBase with UCLASS(Blueprintable) in order for that to work.

Lines 6 - 8 should go in BlueprintMenuWidgetBase, and then you should be fine.

Your explanation is clear.
However…
It’s strange… I have a clear VS. It looks lile engine problems.
I created new C++ class MyWidgetBlueprint that derives from WidgetBlueprint.
It’s finished with error and ask recompile the project.

LogOutputDevice:Error: === Handled
ensure: === LogOutputDevice:Error:
Ensure condition failed:
AttachmentRules.LocationRule ==
EAttachmentRule::KeepRelative &&
AttachmentRules.RotationRule ==
EAttachmentRule::KeepRelative &&
AttachmentRules.ScaleRule ==
EAttachmentRule::KeepRelative
[File:D:\Build++UE4+Release-4.15+Compile\Sync\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp]
and more…

I tryed to recompile it.
Can’t open include “Binding/DynamicPropertyPath.h” in the WidgetBlueprint.h