Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to cast C++ variable to another Blueprint class


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 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!

Product Version: UE 4.15
more ▼

asked Mar 23 '17 at 01:38 PM in C++ Programming

avatar image

49 10 12 21

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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:

   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.

more ▼

answered Mar 23 '17 at 01:56 PM

avatar image

1.3k 46 29 59

avatar image SvarogZ Mar 23 '17 at 05:37 PM

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

avatar image Tobiw Mar 23 '17 at 05:51 PM

what error is that?

avatar image SvarogZ Mar 23 '17 at 05:54 PM

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


 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";


 void ALanguageActor::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]);

alt text

mainmenu.jpg (238.2 kB)
avatar image pulp_user Mar 23 '17 at 06:06 PM

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.

avatar image SvarogZ Mar 24 '17 at 05:26 PM

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

avatar image pulp_user Mar 25 '17 at 10:35 PM

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:

 LanguageActor* LanugageVar;

you can simply set it like this:

 widget->LanguageVar = this.

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

avatar image SvarogZ Mar 26 '17 at 05:07 PM

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 :)

I have added to LanguageActor.h

 class DREAMPAX_API ALanguageActor : public AActor
  ALanguageActor* LanugageVar;

and had had a compilation fail.

Next lines:

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


 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.

avatar image pulp_user Mar 26 '17 at 06:00 PM

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.

avatar image SvarogZ Mar 26 '17 at 06:17 PM

Thanks. I'll try.

avatar image SvarogZ Mar 27 '17 at 05:37 PM

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
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question