Download

Can't Cast from Widget Blueprint to Player Controller

Heyoo!

I got some Problem I can’t really wrap my head around…

I made a HUD and it all was working just fine. Then I packed the project to test on two Clients (It’s a Multiplayer) just to realize there is no HUD showing. I then checked and found out I created a Editor Utility Widget by accident (dumb me).

Now I transferred it all to a Widget Blueprint like all my other Widgets and now most of my Casts don’t work anymore! Most of all means, just one thing still works and that is selecting a brush based of an Integer like here:

After I end playing in Editor I get this error:

I cast to the “Gameplay_Ref”-Variable like so:

And that’s how I try to cast the rest of the functions/bindings:

Unfortunately I really can’t figure out what’s going on and really hope you can tell me where I went wrong!

What it means is your player controller is not of type ‘gameplayPC’, put a print string on the fail route and see…

Thanks for the answer!

When I print the Player Controller is gives me GameplayPC so I guess it is of the same type.

Ok, so that’s correct. Then what it means is you’re trying to cast an empty variable. It’s always one or the other :slight_smile:

Yeah most likely that event construct is getting called prior to the player controller getting loaded. Try creating a function that checks if the gameplay pc ref is valid and if its not, redoing the cast.

Yeah I suppose… But how that comes? I mean, I’m casting to it and it also changes the small Icon on the last Screenshot whenever I overlap a specific Actor. Why this call works?

And also, I print the Get Player Controller right before I cast to it so it’s not empty when I cast. How could the Reference Variable possibly be empty?I even double-checked. Even AFTER setting it it has the right type. I’m really confused.

It’s very simple. When you get that particular error, you click the link ( at the right hand end ) and it takes you to the node causing the problem.

It WILL be because the reference isn’t pointing to anything, you just have to trace it back and find out why.

I know it’s very tempting to think it’s not true, but…, it is I’m afraid :-/

I did that. The error happens on the Node of the first Screenshot I provided. I printed the Reference before and after the variable is used - which is every Tick when I’m correct. I recorded this via my Elgato and played it back, the variable was never empty or anything else. In both there is the right type…

I guess I may delete that HUD and build it again without copying it from the other.

But help me out here: It should be absolutely no problem to cast to a Player Controller from a Widget, right?

Yes, you can do that cast.

If it’s on tick, it only has to be invalid for 1 frame to get the error, is the variable changing?

PS: best to quote otherwise I don’t get a nofication.

Also ( it’s hard to tell from your pics ), maybe you’re accessing the variable before it’s set, right in the beginning?

Those bindings do run every tick. Thing is, if a reference is invalid inside one of those, the engine does not report an error. It used to be that it would output it to the log (windows - dev tools - output log), but it doesn’t appear to do that now either for me (in a 4.25 project).
Any way, I created a binding for a Text, it’s text value. Then I just made an actor ref variable and got its location and input that into the text return.
Sure enough, no errors or warnings. The text displays the variables default value, which is 0,0,0. The reference is invalid.
Then, on the widgets construct I set the actor to be the player controller. Sure enough, the text now displays a proper value. The reference is valid.

But, creating a brush binding and accessing that actor does give an error, even when the actor is Set. It’s like that binding fires once before the Event Construct fires. Which is possible considering its functionality.
What you need to do is, right after the Get Power Up, do an isvalid() check on the reference and if false - return a default brush value.

Or preferably, look into a more event driven structure, less tick bound. But I get that it is convenient when learning.

Thank you very much for the explanation! Absolutely understood that now.
The Error now doesn’t appear anymore, due to the isvalid-check!

My biggest Problem anyways still persists - the Function don’t work and doesn’t update my HUD when stuff happens - only 2 of my 4 Functions actually work, all living from the same Reference.

And I’m sure there are way better ways to accomplish that but thank you for your understanding!

Is it always the same brush that’s showing? Is the Default pin firing from the switch?
I would put a print string on the int value before the switch, to verify that it is changing appropriately. Same with whatever else is not working.

Something to consider, though it may not be the case, is that maybe the visual brush doesn’t get a chance to update. If the number changes constantly, it could be that it doesn’t get a chance to draw. Not really sure since I’ve never actually bound anything to the brush.

Sorry for the late replay, I did some debugging the last two days.

Now I’m a little further. Now my “Get Power Up” Display works without any Errors thanks to ste1nar! The isvalid Check helped!

The Situation I’m now in is the following:
“Get Power Up” and “Get Stroke Count” work fine.

Now my only left problem is the Progress Bar for “Hit Strength”.

I call the PlacerController the same way I call in the other functions:

In the PrintString-Node the correct values are printed. It starts at 0 and stops at 1 as it’s filling up.

But the Bar does nothing.

The Bar doesnt fill up neither does the color change work:

How is the Bar not updating when my values get passed correctly?

Hmm hm well now I see that it’s a multiplayer project. Depending on how you handle the creation of the widgets, it might be possible that each client actually has multiple widgets on the screen. In that case the one being updated correctly is underneath the other which is not updated.
So make sure there is only one of these widgets for each client.
Get All Widgets of class - get length - print.
It should display something like

Client1: 1
Client2: 1

where as if there’s multiple widgets, they’ll print another number.
But I am very rusty on the multiplayer aspect.

But if that’s not what is causing it, then I don’t know.
I fail to see a singleplayer reason as to why those two functions would not modify the progress bar correctly, assuming the float is between 0 and 1 and the reference is valid.

Something you could do, for debugging, is to put a random float in range 0 … 1 just to see if there’s a visual response to the value. If there is, I bet it’s something to do with the above mentioned multiplayer aspect.