Get variable by name (string)

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 !

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.

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

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.

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

nobody know ?

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.

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 ?

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
or in javascript using MyObject[objectVarName].
If its not possible to do this with blueprint maybe in C++ ?

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. :slight_smile:

yes its done

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

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

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.

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.

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.

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

1 Like