Set A Variable In A Widget Blueprint From Another Widget Blueprint

I have two widget blueprints that are shown at the beginning of the game. Each is tied to their own level.

Level A loads Widget Blueprint A

Level B loads Widget Blueprint B

Widget Blueprint B has variables X and Y

I need to be able to set variables X and Y from Widget Blueprint A

https://s6.postimg.org/pgvjse4kx/Capture.png

(Widget B is “MainMenu”)

Have you tried casting to the widget?

Ya I tried doing what I captioned below but not sure what the object reference would be. It can’t be “Player Controller” because the widgets don’t inherit from that so not sure what should go there.

https://s6.postimg.org/6ptmi8a0h/Capture.png

if main menu is a widget, then you should be able to create a variable with it like this

Well I tried the above but it’s saying that it’s already that widget apparently?

https://s6.postimg.org/bpvo3x50x/Capture.png

Hi,

casting is only neccessary and even only possible when trying to convert from an base object to a child object. This is not the case here.

It is also not enough to have a variable of Type MainMenu, you also need to initialize it with the actual object reference.

If there is only one instance of MainMenu you can use the GetAllWdgetsOfClass node and access the first entry. Similar to this, but select MainMenu from drop down instead of UserWidget, then use the result of the GET node as target input for SetMapSetting and SetDifficultySetting

134344-getallwidgets.png

I tried the below. I believe that is what you were talking about but might have mixed something up. It compiles but when I select “play” (which looks at the map variable) the game crashes with the below 4 errors since the map variable was empty and didn’t get set by the blueprint process below.

Do I have to provide a index location here and if so how can I get the right location? I just started using UE 4 and haven’t used these functions before so still learning. :slight_smile:

Error Blueprint Runtime Error: Accessed None trying to read property CallFunc_Array_Get_Item from function: ‘ExecuteUbergraph_WidgetA’ from node: Set MapSetting in graph: EventGraph in object: WidgetA with description: Accessed None trying to read property CallFunc_Array_Get_Item

Error Blueprint Runtime Error: Attempted to assign to None from function: ‘ExecuteUbergraph_WidgetA’ from node: Set MapSetting in graph: EventGraph in object: WidgetA with description: Attempted to assign to None

Shows the same error for the other variable.

https://s6.postimg.org/tqsaodx2p/Capture.png

It fails because your MainMenu does not yet exist when calling GetAllWidgetsOfClass. You must ensure the order of execution somehow. It depends on your architecture which way is best.

  • Maybe create the MainMenu Widget from Blueprint A
  • Maybe do it the other way around: Inside of MainMenu get the reference to Blueprint A
  • Many other options …

I just re-read the question and found you where talking about two different levels. If that is the case I may have lead you wrong, the above does only work on the same level. You would then need to store the variables inside the GameInstance as described here https://answers.unrealengine.com/questions/548875/pass-variable-between-main-menu-and-levels.html

Using Game Instance casting based on the thread below is how I got multiple blueprints to use the same variables. Thanks for the help!

https://answers.unrealengine.com/questions/548875/pass-variable-between-main-menu-and-levels.html