Does Event Tick run before Event Begin Play?

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.

Event Begin Play

Event Tick

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?

Animation Blueprint:

Begin play start timer:
image

Replacement of event tick:

It shouldn’t be necessary for the animation blueprint, but in the tick you can use an “is valid” node at the start of event tick just to be sure.

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:

When I do print string without the is valid, I see it runs before the begin play:

LogBlueprintUserMessages: [AlienAnimation_C_0] Update Animation
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
LogBlueprintUserMessages: [AlienChar_C_0] Event Begin Play
LogBlueprintUserMessages: [AlienChar_C_0] marine variable set
LogBlueprintUserMessages: [AlienChar_C_0] Tick
LogBlueprintUserMessages: [AlienAnimation_C_0] Update Animation
LogBlueprintUserMessages: [AlienAnimation_C_0] Update Animation

Would there be any way to start the alien animation begin play after the alien begin play?

Oh I think one problem with timer is that it doesn’t know when to stop but the update animation does.

To fix that I put a stop node before I destroy actor, not sure if it’s the right way of doing things:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

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.

Privacy & Terms