Player reference becomes invalid

When initiating my HUD, my player reference suddenly becomes invalid for no apparent reason.

I try to create my combined hp/energy display widget:

then in the construct event of the hp/energy display:

and i get:

293546-2019-12-08-02h27-53.png

i don’t get how this happens… seeing how the construct event is literally the next step in the code/blueprint.

Hi,
New Print string appears from the top. So your player was invalid (false debug) and became valid (true).
false was displayed first and then true.

if that were the case, the init function wouldn’t be failing, as i get “Accesed None trying to read property Player” in the init function of the combined display…

if that were the case, the init function wouldn’t be failing, as i get “Accesed None trying to read property Player” in the init function of the combined display…

You never set the variable ‘player’ prior to using it. Hence the ‘accessed none’ error. Check out video #27 in the link below. Also check out #25 for more information on creating references to various objects/actors. In this case referencing the player shouldn’t be very hard.

Also the HUD could be initialized before the player character is created. How are you creating your HUD? Which blueprint creates the main widget/HUD?

What does the construct of the widget menu bar look like?

Also, try adding a delay (for testing purposes) after the main HUD construct and before calling the init function. Want to see if perhaps the passed in reference wasn’t valid before using it to pass to the display widgets.

To answer both:

this is the BeginPlay event of my Player class:

Construct of MainHUD:

293740-2019-12-11-18h00-24.png

This is the init function of MainHUD:

Construct of CombinedDisplay (HealthAndEnergyDisplay) is the same as that of MainHUD, with this being the init function:

All Player and MainHUD variables are set as exposed on spawn and instance editable, so passing them in the CreateWidget node should set them with no need for having to do it manually during the Construct event, from what i understand?

Unless i’m mistaken on that, i see no reason why the player would be valid in the MainHUD (if printed) and not in the CombinedDisplay

it initializes fine and has no reference to the player within it (not yet anyway, as the menus arent populated with player data yet and not directly (only via its MainHUD reference)) so i fail to see how it would be relevant

but for argument’s sake:

That hasnt fixed the reference issue

Yes I was asking to be sure it wasn’t that widget throwing the error. A simple print string won’t tell you what blueprint fired off that event given you have multiple and since we are dealing with initialization issues I thought I should ask to double check if that widget wasn’t the issue because I could see how things would error out if certain functions executed before others completed. Anyway, my next thought is add something to the print string so you know exactly which one is printing true and which is false. Like “main HUD widget” appended to the true/false output
Then you can tell exactly if it’s the construct giving the false or the function. The above post is correct though the string prints new lines at the top of the screen.

293764-2019-12-11-23h16-49.png

what is your point here? i know which print did what, that’s why i posted the question to begin with. there is nothing asynchronous going on here from what i understand of unreal engine… so it’s baffling.

I don’t know if you mean to come across this way or not but when you say things like “I fail to see how this is relevant” and “what’s your point” it feels as if you are irritated with me for volunteering to try and help you work through this problem. As the individual not experiencing the issue and not in front of your computer I will tell you that a lot of times people run unto issues that are quite simple to fix had a fresh set of eyes looked over the entire thing. Unfortunately it is impossible for me to do that on this end of the screen. So I try and wall you through how I troubleshoot things. It may seem like you’ve dotted your “I’s” and crossed the “T’s” until I ask you to print a string or put a delay in etc. So the fact that none of that is working is actually useful info for me to know as I process this issue with you. You’d be surprised how many times I’ve come up with every possible solution I can think of, spent days on a problem only to have the OP post something that says “turns out I forgot xyz”. So while all of this may seem annoying to you when it doesn’t work I appreciate you confirming these things for me. Now that we got that out of the way, the only reason I know why something would be “invalid” if you haven’t made any silly errors would be the player character is actually destroyed at some point in between the main HUD and the display initializing. Which would be odd hence why I am trying to find a less odd reason for this issue. But for completeness sake, any possibility your character enters a state in your game where he would be killed or something happen to him that could potentially invalidate the variable reference?

i, too, am a programmer by trade :slight_smile: looking back on it, it does come across as a bit snappy.

There shouldn’t be, this is entirely new project with the presented code being about half the code there is in the whole project (the other half being a basic item implementation, but only classes setup, no actual items got initialized yet nor are there are any instanced in the testing map).

Haha all good my friend. Just wanted to keep this cordial :slight_smile: Another thought, have you tried replacing the variable with a direct reference from a cast node and see if that fixes the issue? Could be some issue with how the variable is being passed through the multiple sub widgets.

you mean a cast as Player node? if so, no i haven’t (will do tommorow at the earliest)

Yes, basically just attempt a direct cast to player character and feed that in to the function instead of the passed by reference variable.

That hasn’t helped either. i even tried moving the display into the Player class and the reference in the Display class is still broken…

I’ve been able to create a display for my stats that isn’t structured quite like what i’ve initiality imagined, though i have absolutely ZERO clue as to why the approach i’ve taken now works vs the one that initially did not :|… (i create the displays directly in hud instead of having a CombinedDisplay class and put them into a dictionary for purposes of systematic updating (i have a ModifyStat event on my Player that is supposed to be the main event that is fired to make sure everything is kept up-to-date whenever health, energy and such change) and that apparently works…