# Get variable by name (string)

**URL:** <https://forums.unrealengine.com/t/get-variable-by-name-string/398065>\
**Category:** Blueprint\
**Tags:** unreal-engine, UE4, CPP, Blueprint, question\
**Created:** [July 29, 2017, 7:11pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065 "2017-07-29T19:11:01Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [July 29, 2017, 7:11pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/1 "2017-07-29T19:11:01Z")

</div>

Hello,  
I use UMG to make players UI in a game then I would like to initialize dynamicaly the data of each player block.  
To do that I need to get Widget variables by name.  
I named them to know to which belongs to the good player but I can’t found a way in BP or C++ to get my widget variable by name.  
From a string I can cast to TurnBasesMatchInterface but I got an error on compilation and didn’t found what the use of it in the documentation.  
Is someone can help me pls ?

Edit:  
So without this method I think the best way is to create all dynamicly and expose on spawn like Nachtmahr explain.  
However, I try to find a way to do it in C++ and found FindPropertyByName function from GetClass on UObject.  
Then cast it to the good type, there I try for FString (so need to make a function by type), but when I try to return the value I got ue4 crash and a memory read address error in log, i’m surely doing something bad.  
If someone know why there are cpp and h files :

```
ToolsFunctionLibrary.h :

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "ToolsFunctionLibrary.generated.h"

/**
 * 
 */
UCLASS()
class LH_API UToolsFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:

	UFUNCTION(BlueprintPure, meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", DisplayName = "Get String By Name", CompactNodeTitle = "GetStringByName", Keywords = "get var name blueprint"), Category = Game)
		static FString GetStringByName(UObject* CurrentObject, FName Name);
};

ToolsFunctionLibrary.cpp :

// Fill out your copyright notice in the Description page of Project Settings.

#include "lh.h"
#include "ToolsFunctionLibrary.h"

FString UToolsFunctionLibrary::GetStringByName(UObject* CurrentObject, FName Name)
{
	UClass* CurrentClass = CurrentObject->GetClass();
	UProperty* CurrentProp = CurrentClass->FindPropertyByName(Name);
	FString* CurrentValue = CurrentProp->ContainerPtrToValuePtr<FString>(&CurrentObject);
	return *CurrentValue;
}

```

Thx all for your spent time on helping me !

---

<div class="post-metadata">

**Author:** ![BenVlodgi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benvlodgi/32/1194567_2.png) [@BenVlodgi](https://forums.unrealengine.com/u/BenVlodgi)\
**Post date:** [July 29, 2017, 8:03pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/2 "2017-07-29T20:03:59Z")

</div>

Bind the value of each `Text` to a public background string variable.

When you dynamically create each block you can set the value of the string on the widget reference.

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [July 29, 2017, 8:37pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/3 "2017-07-29T20:37:24Z")

</div>

In fact block are already created and all elements needed binded as variable but I can’t access them by name

 ![192854-screenshot.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/e/e/1ee2ffa2a820ed0a42601b0ff5542af638f017a9.png)

---

<div class="post-metadata">

**Author:** ![BenVlodgi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benvlodgi/32/1194567_2.png) [@BenVlodgi](https://forums.unrealengine.com/u/BenVlodgi)\
**Post date:** [July 29, 2017, 8:48pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/4 "2017-07-29T20:48:05Z")

</div>

You can’t turn a color into a UObject.

If you need a reference to your TurnBased MatchInterface, you need to pass a reference to it into the function, or set it as a property on your widget.

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [July 29, 2017, 8:59pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/5 "2017-07-29T20:59:48Z")

</div>

I used TurnBasedMathInterface because it was the only choice of cast by a string but I would have in there the variable Playerx which is a Border Widget

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [August 12, 2017, 12:39am UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/6 "2017-08-12T00:39:38Z")

</div>

nobody know ?

---

<div class="post-metadata">

**Author:** ![Nachtmahr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nachtmahr](https://forums.unrealengine.com/u/Nachtmahr)\
**Post date:** [August 12, 2017, 1:40am UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/7 "2017-08-12T01:40:18Z")

</div>

You aproach it in a very odd way. Nothing dynamic about it there. Make a new Player Info Widget and hand the Player over (Expose on Spawn) you want to get the Information from. Add it to your Main Widget, done. No need for Variable names and you can add Players Dynamicly and not Hardcoded to 4 Players 😜

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/3/763ce8bba03b7a6a8be3f57205dfdd584756f34e.png)

And take a look at this Training Video since you seem to have a bit trouble with Communication.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/0/4/c04b72f0c2350fe16cce0a9795c662a495694f67.jpeg "Blueprint Communications | Live Training | Unreal Engine") ](https://www.youtube.com/watch?v=EM_HYqQdToE)

---

<div class="post-metadata">

**Author:** ![Fjellgnu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fjellgnu](https://forums.unrealengine.com/u/Fjellgnu)\
**Post date:** [August 12, 2017, 1:53am UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/8 "2017-08-12T01:53:52Z")

</div>

If I understand you correctly, you need access to variables in different widgets?

The create widget node gives you the reference to each widget you create from its return node. You can store that reference in a variable and use it to get access to variables in the widgets directly.

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [August 12, 2017, 2:47pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/9 "2017-08-12T14:47:03Z")

</div>

I’ve already can access to variables in the widget and outside but can only select them by ref not by name (string). Yes I could create all player widget elements dynamicly and expose each on appropriate pawn to resolve the problem but it was an example to explain what I need and I made 4 players cause it’s the max allowed. This pb is not only in widget, its in level or anywhere I would get a variable reference by his name else I must store variables in array map with string index of their name to access to them. This is so bad for memory, to setup and for execution time so there is a way to simply get a variable by his name without associate it with a pawn or array or anything ?

---

<div class="post-metadata">

**Author:** ![Fjellgnu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fjellgnu](https://forums.unrealengine.com/u/Fjellgnu)\
**Post date:** [August 12, 2017, 2:53pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/10 "2017-08-12T14:53:45Z")

</div>

No, I don’t think so. That is after all the point of using the references. They would be pointless if you could just access the variable directly.

What you are asking for would be global variables. The “cardinal sin” of programming because that gets messy real quick! Besides I don’t think you are allowed to make those in blueprint.

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [August 12, 2017, 5:50pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/11 "2017-08-12T17:50:05Z")

</div>

Global or not I would get a variable by is name like in php for example :  
[http://php.net/manual/en/language.variables.variable.php](http://php.net/manual/en/language.variables.variable.php)  
or in javascript using MyObject[objectVarName].  
If its not possible to do this with blueprint maybe in C++ ?

---

<div class="post-metadata">

**Author:** ![BenVlodgi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benvlodgi/32/1194567_2.png) [@BenVlodgi](https://forums.unrealengine.com/u/BenVlodgi)\
**Post date:** [August 12, 2017, 6:49pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/12 "2017-08-12T18:49:05Z")

</div>

It might have let you drag the string, but obviously it is what is causing the error.

You can’t cast a string.

You need to look up what casting does. It is not a magical node that turns whatever you want, into other objects.

Do what I said in my earlier posts.

---

<div class="post-metadata">

**Author:** ![Fjellgnu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fjellgnu](https://forums.unrealengine.com/u/Fjellgnu)\
**Post date:** [August 12, 2017, 6:51pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/13 "2017-08-12T18:51:13Z")

</div>

Well, I guess you could make a blueprint library class in c++ and create variables there that are exposed to blueprint. The blueprint library class is acessible to all other blueprints.

EDIT: Okay, so I regret saying this already. You should probably follow Ben Vlodgi’s suggestion. It will involve using the references, but it is a better way to do this. Besides, it won’t make peoples’ eyes bleed. 🙂

---

<div class="post-metadata">

**Author:** ![BenVlodgi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benvlodgi/32/1194567_2.png) [@BenVlodgi](https://forums.unrealengine.com/u/BenVlodgi)\
**Post date:** [August 16, 2017, 9:58pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/14 "2017-08-16T21:58:39Z")

</div>

You should add this as an edit to your question.

Upvote the answers that helped, and mark the one that solved your problem as the answer.

---

<div class="post-metadata">

**Author:** ![bennette](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennette](https://forums.unrealengine.com/u/bennette)\
**Post date:** [August 16, 2017, 10:05pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/15 "2017-08-16T22:05:08Z")

</div>

yes its done

---

<div class="post-metadata">

**Author:** ![officernickwilde](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/officernickwilde/32/547477_2.png) [@officernickwilde](https://forums.unrealengine.com/u/officernickwilde)\
**Post date:** [December 6, 2022, 3:09pm UTC](https://forums.unrealengine.com/t/get-variable-by-name-string/398065/16 "2022-12-06T15:09:24Z")

</div>

Havent tried implementing this myself (and Im not saying that this is a good/recommendable solution) but:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/a/4/ca40a7e3bbb0ffccd57ec72e79f4d1883af4bb06.jpeg "UE4 get variable by name with Blueprints and c++") ](https://www.youtube.com/watch?v=VvRqJTocTfw)
