Get a variable by name, using other variables values

Hi everyone!
Coming to UE5 blueprints from other langages, I’m struggling with this.

If I was in another langage, and I had a ‘player_1’, ‘player_2’, ‘player_3’ root list of variables (whatever the type), and had another variable somewhere containing ‘1’, ‘2’ or ‘3’ (let’s call it ‘MyNum’), I could easily catch the corresponding root variable with something like “root[‘player_’+MyNum]”, and have fun from there.
Works the same with various enums/strings to identify specific elements : just concats some bits to create a variable name, and fetch it, done and done.
I can use values of this to catch names of that.

Maybe I’m a dirty programer, or a very dumb one, but it seems there’s no such neat handling, in blueprints ?
(especially with Structures or big data stuff like that)
And I always end up having to compare every single thing one by one, whatever the types I try to mix and make correspondances between.
And it would be soooo much easier if I could just quickly build and send variable names to match stuff around, once destination (and the target’s type) is contextualized properly.
(again : especially with structures or structures’ datas, but not only)

Any leads ? Am I dumb or too dirty for UE5 ?

I might be misunderstanding, but to me it sounds like you are either looking for a data table (thats an asset type in blueprint) or something like a Map where you can have a key of an enum or gameplay tag with a value of a struct?

I’m not sure I understand. You might have to give a more specific example of what you are trying to do.

If you just want to try to concatinate in BP, you can use the append node for strings. If you use that with string map keys maybe that is what you are looking for?

Maps & keys, I’ll have to look into that.

As for an example : I have a datatable of 50 potential characters, for a game, with all their stats & caracteristics.
And I have structs on my main BP, one for each of the 12 characters that make my “active team”, containing their names, current health points, and inventory slots and what they have in it.

And the point I reach is : if I want to transfer 1 inventory item from team_member_8 to team_member_5, from a drag n drop operation located in 1 inventory item BP, and dropping onto team_member_5 widget portrait, it becomes a big mess.
Because I have to reach and cast almost everything, it seems ?

team_member_5 (let’s short it to TM_5) widget portrait can’t have a ref to TM_5 struct, to identify which “real struct” I have to test for inventory, when dropping an item on TM_5 portrait.
So I have to make a whole series of “is this equal ?” checks between “portrait’s display name” and my structs names, casting them all one by one, until I climb back up from TM_5 portrait to TM_5 struct.
(and then check for free inventory slots, to finally be able to transfer the item)

In another langage, I would just have to tell “get me “main BP struct” which variable name is equal to “current drop target portrait’s display name string””, and that would be it.
I wouldn’t have to cast and check the whole 20 structs of my active team members to compare them with portrait’s display name and find the right one.

I got various other cases like that, it’s not about “that one problem”, it’s about finding a better way to program stuff in UE, to avoid all those casting and checking for correspondances.
What I’d like is to understand how can I do that trick of “get this chunk of string to find something else, elsewhere, of another type” that works in other langages ?

Same goes for images, for example.
I used to be able to inject assets source paths like “./my_img_directory/”+character_name+“_”+current_situation+“.png” , in some langages.
Where character_name is just a string or struct name or whatever bits of string I get from somewhere, and “current_situation” might like a number I get from story context, and resulting in providing the angry face avatar of called character, or the smiling face avatar.
But it doesn’t seem possible, in UE5 BP system ?

I’d just like to be able to build names/paths/refs, using other bits, instead of having to cast and check stuff one by one.

As I said : I’m probably super dumb and there’s an easy way to do that, but I can’t seem to find it for now.
And it’s more about a global way of reaching/building stuff in UE5, that I don’t get.

If you have the structs in the game mode, for example, you could say → get game mode → get “your struct name” → set members in struct

You would need to either cast to your specific game mode, or use an interface, or you could use Event Dispatches to communicate in a decoupled way.

My preference is to use Events because this seems to offer greatest flexibility and extension. So to make it such that Game Mode doesn’t need to know about some widget or w/e, I use an abstract class (this could be an actor component you attach to the game mode) to handle the events.

Then, widget could say GetGameMode-> Get Component by Class (the event holder) → call event. The event could pass arguments for what data you want to transfer, and what the targets are (where is it coming from, where is it going.)

The game mode can be bound to those events, then based on the where and to targets, you can just direct the data and use Set Members to update the structs.

Hmm… Event Dispatches might be a neat trick to improve things indeed, if I can just “teleport” around via event triggering.

Thanks!
It won’t solve all issues I got, I feel.
Like if I got several stuff of the same class, I’ll still have to do big checks to get a landing zone.
But will definitely help restructuring some of the stuff.
Like all secondary options that are just “one-shot features” that can be as many different class, and so forth landing zones for “get component”.

I miss my delicious “use this variable to call that variable”, lol, it was so simple and practical, but I need to move on from that relationship, if UE5 BP doesn’t have it. xD

You can use a Map type variable, which is an array of something that can be indexed by something else, in your case perhaps a string.

There’s probably a better way to do whatever it is, though.

In another langage, I would just have to tell “get me “main BP struct” which variable name is equal to “current drop target portrait’s display name string””, and that would be it.
I wouldn’t have to cast and check the whole 20 structs of my active team members to compare them with portrait’s display name and find the right one.

I… don’t really understand what you’re getting at. These things don’t sound equivalent.

How about having an Array of Players variable, and an integer variable to index that array ?

When you are drawing portraits, you should be iterating a list of actors or structs that contain data for each portrait. Assuming a portrait has an avatar and a name, those could be properties within your PlayerState class. In your container widget you’d iterate playerstates via GameState->PlayerArray, and for each player, create a portrait widget.
Now when you are dropping a widget onto a portrait, all you need is to retrieve the index of that portrait, then you can reach the corresponding PlayerState directly via GameState->PlayerArray[index].

If you are using structs it is similar. In your “main BP” you shouldn’t have 12 variables of the same struct named “TM_1” “TM_2” … “TM_12”, that is highly inefficient. Instead, declare an array of structs, and populate it with 12 elements. Then you can easily iterate or index by an integer variable.

For this precise example, I think the index-portrait-array combo might be a strong lead, yep, I’ll try that.

And thanks for the “TM_1, 2, 3 is highly inefficient” : that’s what I try to get, here : what are the wrong/better ways to go.

Array, maps, and index tricks.
That offers a few leads to explore.
Thanks everyone for the tips ^^