I setup an player char variable on “Event BeginPlay” and then it gets used in “Event Tick” where I check if player is within range, if it is then start playing attack animation.
But I got this error:
LogScript: Warning: Accessed None trying to read property Player Char
AlienChar_C /Game/Levels/UEDPIE_0_Mars.Mars:PersistentLevel.AlienChar_C_1
Function /Game/DarthBarian/AlienChar.AlienChar_C:is player alive:000C
PIE: Error: Blueprint Runtime Error: "Accessed None trying to read property Player Char". Node: Return Node Graph: is player alive Function: Is Player Alive Blueprint: AlienChar
This can only happen if the event tick runs before begin play so wondering if that is true.
You will find if you put a Print String node immediately after your Event Begin Play and Event Tick nodes that the Begin Play will fire first, but only for a fraction of a second. Doing a cast is an expensive operation, so promoting it to a variable is definitely the right thing to do, however the event tick is perhaps firing a little too soon for the operations to complete.
I’d suggest looking into an alternative to using the Event Tick. Perhaps using a timer, or an event dispatcher instead. Event Tick is great for prototyping ideas, but ideally you’ll want to find a way to accomplish the same thing that’s less resource intensive.
Also, I’d probably use a Validated Get for your Player Char variable, or at least use the Is Valid node. This way you can catch when things go wrong before they throw errors.
To give some further context to TastySnak’s answer.
What you’re experiencing is called a race condition, which is what happens when two pieces of code running on two different threads but code A needs information from code B and it’s not ready yet. And blueprints are multithreaded.
To make a track-and-field analogy, imagine two runners in a relay race about to pass the baton. However, the runner holding the baton hasn’t caught up to the next runner before it’s time to pass that baton.
As a temporary fix (I agree with Tasty that keeping things off tick is the best way to go. But the course doesn’t really advocate for that) you can do an isvalid check for the playerchar variable before doing the rest of your code.
Thanks this sounds like an advanced topic but using is valid macro on event tick to check if marine is valid seems to be working.
Is there any course that goes through this kind of scenario?
Thanks @TastySnak for the suggestion, I tried out the timer and realized there’s another place where I use the marine variable and it’s in the animation blueprint.
I tried to do the same thing that you have suggested, use timer + events and have done below.
Is this trick ok for animation blueprints as well?
After adding the timer thing (in the last 2 screenshots above), it was still failing for same error.
I think it’s because of the extra enhancements I added for fun to the game.
I saw there were actually 3 attack animations (swipe left, swipe right, overhead attack) so I try to make the alien do all 3 consecutively but if player moves away in between I need to make the alien stop and follow the player again.
For that I introduced this player around variable so if player is not around I will stop the montage:
Sorry for the late reply. The Set Timer function returns a timer handle struct that you can promote to a variable and then call “ClearTimerByHandle” function later and pass that variable in to stop the timer.