possible to create a widget reference based on string?

hey,

I have multiple menus that I need to access the outer canvas widget for at different times in my game. Then I have a chunk of code that performs a zoom effect on the blueprint. But I don’t want to have to copy and paste a chunk of code a hundred times to achieve this for each menu.


I just want to be able to switch which widget reference I am using on the fly.

At any time I have a string which is the name of the widget class. But can I feed this string into the ‘create script widget’ maybe to switch to the required blueprint. And just have one chunk of code that does the zoom and the reference is the only thing that switches.

does anyone know what heck i am talking about

thanks, Sam

maybe i just need to use functions or something

You can likely use a map. It’s one of the basic four variable types (single, array, map, set) in the dropdown next to the variable type.
Map from string to widget.

If you want more specific advice, please zoom in and provide a bunch of screenshots.

Thankyou, yes i think this is what i am after. However im not too sure how to implement it. You see here i have my current selected widget gets its text checked and then the corresponding reference to blueprint class is created.

so the ‘write script’ is connected to the create widget node which creates a reference to the script menu blue print so i can access the variables there.

I tried to follow what you said about mapping all the widget names as strings - but i need to be able to have the strings correspond to the names of other widget blueprints.

but not sure how this works here, i cant specify the name:

tho im thinking this is the right track
thanks,
Sam

I think you response your own question…

Do you want a reference based on string?
use a switch then… basicly you want to do a factory method desing pattern…

i can not find a blueprint exaple… i’m sorry

Yes that is the right track.
It seems you have exactly what you want… the name is “Concept Art / Reference” no?

Also I don’t recommend using a switch statement. That will still result in a lot of duplicated code.

1 Like

the string is the name of the blueprint I need to access variables for. But I don’t know how to set the value to the matching names blueprint? - it just allows me to select ‘widget Blueprint class’ as a type, but not the specific name of the blueprint. does this make sense?:confused:

i have basically 10 menus that i need to be able to pop up when i require. So the string is the name of the currently selected menu i need to be added to viewport. So i want to use the dictionary map thing to connect to the correct blueprint to be able to display.

Crete a Map…

TMap<FString, UUserWidget*> MyWidgetMap;

//register
MyWidgetMap.Add( "MyWidgetName", MyWidgetReference );

//Show
MyWidgetMap[ "MyWidgetName"].AddToViewPort();

//destroy
MyWidgetMap[ "MyWidgetName"].RemoveFromParent();

//You can use a variable name
FString variableName = "CurrentName";

//So you can do this for example
MyWidgetMap[ variableName ].AddToViewPort();

thankyoug Ivan3z, sorry im not smart enough to code this, i am just using blueprints…

it is exactly the same…
Do you have Maps in bluepints?.. Yes
Do you have Strings in bluepints?.. Yes
Do you have Widget in bluepints?.. Yes

Ok… you have all you need…

The function has the same name…

The difference is… blueprints is like a spaggeti plate.
and in C++ you do the same in 4 lines :rofl: :rofl:

you only translate it… it is easy… you can do it!!

That is exactly i just to wrote…

It is the second time you response your own question.

Nope, you really are smart… you only need belive in you!!

@Ivan3z It’s not easy when the person doesn’t know C++. You can’t intuit C++ code as someone who’s never coded before (at least not to the point you can convert it)- you think people can since you can, but they can’t.

Yeah you’ve got what you need, you’re just missing the connecting pieces I think.
You’ll want a map of string → user widget class.
image

You can then fill out the values you want:
image
ie ScriptBreakdown would be mapped to ScriptBreakdown (using the images of what you did to convey which class)
image

image

After that you’ll just use the find node. If the find node finds something that matches (so in the example I’ve made that would be “Some Menu NAme”, “Another MEnu”, or “Pause menu”), the boolean will be true and the class will be whatever you’ve mapped it to.



I don’t want to confuse you too much, but you shouldn’t be creating a new user widget every time.
To avoid this, create a second map. This should be a map of string → user widget object. You don’t want to add any defaults to this.
image

Then we simply check if we’ve already created the widget for a given name. If we have, we add that reference to the viewport. If we haven’t, we create a reference, add it to the viewport, and store a reference to that in our map.

1 Like

Thanks for doing it… I almost did it too…

I think he knows exactly what he wanted to do (he answered his question twice)… But he didn’t have much confidence in himself. He just needed someone to confirm his ideas.

I really think he’s smart. I believe that if he puts his mind to it he can translate those 4 lines of C++ without problems.

I have seen many people on this forum with less knowledge than him.

Anyway, you’re cool too, you did the hard work!! :heart:

thanks for this rokenrock, Ill check this out!

you seem to know an awful lot about me based on one post. but its nice to know you believe in me and think I’m smart. thanks papa

1 Like

I’m sure of that, you are above the majority on this forum. Trust yourself.

rokenrock - I had a small follow up question if it was simple.

Now that I have access to the correct blueprint, I need to access the top level canvas panel in each. So would I need to create a second MAP node variable that stores the unique panel names? and if so what would be the ‘value’ type in the MAP?

The panels have the same names as the key names in the previous MAP node, just with ’ OuterPanel’ added at the end. So was thinking to use these modified strings to somehow call up the corresponding panel variable. All of those panels have ‘isVarable’ fyi.

Any tips would be handy;)

thanks,
Sam

Third time you answer your own question.

Maps has a key and a value. There cannot be two identical keys. therefore they are unique.

I’m going to show you a trick. Maps and enums work great together.

So, create a enum. (Enumerate your UI) So in your Map the key will be your of enumeration type, and the value will be your UI reference.

I like this way, it is better than using strings as key.

This has several advantages over strings.

-It only uses 8 bits of memory

-You don’t have to write strings all over your code.

-It is easier to debug, especially in C++ because string literals are harder to find. (You can make a mistake writing the string and it is a disaster).

So, enums are better

Ivans3z, its not that I don’t understand how to construct a sentence that solves my problem its about implementing it in unreal engine. Why don’t you just answer my question instead of patronizing me and giving me irrelevant information about enums.

the reason I am using strings is because I have a menu which contains many buttons that can be traversed. When I select a button I am using the button text to call the corresponding menu to be zoom transitioned onto the screen. So I am not only displaying the new menu but I am also targeting the canvas in that menu to perform a scale anim at the same time.