Variable Not In Scope

I would like a button pressed to set a variable to some integer and have it be accessible inside the level blueprint. However when I try this I always get the error that the variable is not in scope. Any help?

Note I set the variable inside the widget event graph using EventPressed (button)

Then in the level blueprint I get a WidgetBP reference, drag off and get Variable then include that in my sequence of nodes.

This is all about ownership of the variable. If it’s related to the state of the game then it should go in a game state BP and then you can get it anywhere (with the GetGameState BP node). If it belongs to an actor in your world then you need to get that actor in the level bp (with the FindAllActorsOfClass BP node or hold a ref in the level BP if it’s always in the world). If it’s level specific then it should live inside the level bp but you shouldn’t wire it up so the things know about the level, the knows about everything and nothing should know about the level.

Variables represent state of an object so you need to know what the variable means for the game itself and the object it belongs to and make sure that relationship makes sense.

If you provide some more details I would be happy to give some more insight on this.

Can you show us your Blueprints?

You could try making the said variable public and editable outside. That way you can access/change it from another blueprint. Another options would be to create a function in your widget blueprint to get the value of the variable in question. Then you can call that function from another blueprint.

I had tried making the variable editable by clicking the eye, also a function did not work here either ):

I do not find anything wrong with your setup. But try these and let me know what you get

In your level blueprint, before accessing the variable ‘W’ of your ‘Widget Reference’, do a validity check. ie Use an ‘Is Valid’ node and pass ‘Widget Reference’ as its parameter. Connect the rest of your network to the ‘Is Valid’ output. Attach a Print string node to the ‘Not Valid’ output. Now run it and tell us what you get.

I am trying to make sure your Widget Reference is actually pointing to an object that actually exist.

It returns the print statement, so it is not valid.

Is it because I set the value of W in a separate level? When I go to the next level maybe that value does not exist, or remains unset after the level transfer.

I tried using a function w/ output integer:

still same results.

If I try using a CastTo node, I get an error and I believe it’s because of a UE4 update, changing the way casting functions:

Its not because W is out of scope. The problem is the variable ‘Widget Reference’ does not point to an instance. Same goes for Cast; you’ll need a valid reference before you can cast it.
Can you show me how you get a reference to your widget and assign it to the variable ‘Widget Reference’?

These threads might be useful

Well that is not going to work. You created a variable of type ‘Widget BP’. All it means is this variable can contain an instance of WidgetBP. BUT it is still empty, you need to put something (an instance of WidgetBP) inside it for it to work.

What you should do is Create an instance of WidgetBP by spawning and then set the spawned widget instance to ‘Widget Reference’. I am assuming that you spwn the widget from your Level BP (possibly in BeginPlay). If that is not the case and you spawn the widget from some other BP (Pawn for instance) then you should find a way to first get a reference to that BP and then get reference to your Widget from it.

I spawn the widget in a separate level using EventBeginPlay. Would the reference be the return value of the CreateWidgetBPWidget node?

Yes, the output of CreateWidgetBPWidget is the actual instance. You should use it to reference your Widget. You could put it inside GameState as it is accessible everywhere.

Could you show me a blueprint of how to solve this? I can’t get it done.

I could not do that without knowing more about your setup.

But I could give a sample setup here.

  1. In your Pawn/PlayerCharcter BP (Lets call it MyPawn), create a variable of type WidgetBP. Make it public. Lets call it WidgetRef
  2. When you spawn your Widget, get a reference to your PlayerCharcter thruogh GetPlayerCharcter. Cast the result to ‘MyPawn’ (or whatever class your PlayerCharcter is). Now set the output of ‘CreateWidgetBPWidget’ as the value for MyPawn.WidgetRef
  3. Now whenever you want to access the Widget, you should get a reference to your PlayerCharacter using GetPlayerCharcter, Cast it to MyPawn and then access the WidgetRef. To make sure it works all the time, you should do a ‘IsValid’ check on WidgetRef before accessing its memebers.

Im also getting some troubles with this error/warning and it makes mty meshes move to random places in the world/level. How can I solve this?

Im having an actor reference in my player BP that represents any carried object.