Hi,
the title pretty much explains it. Is this behaviour expected or unintended? My problem is that on BeginPlay my PlayerController relies on something in the GameState that happens on its BeginPlay but that didn’t happen yet.
Best regards
Hi,
the title pretty much explains it. Is this behaviour expected or unintended? My problem is that on BeginPlay my PlayerController relies on something in the GameState that happens on its BeginPlay but that didn’t happen yet.
Best regards
Have you seen this?
Yes definitely. It doesnt list playercontroler or gamestate. also:
the order of beginplay is swapped in editor (where it works) and in a packaged game (where it is in a different order). also the gamestate is created in the gamemode as far as i know.
If you can’t rely on it, you just have to do it another way… ![]()
Besides your ridiculous answer I found out that a PostInitializeComponents also does the trick. It is called before the BeginPlay functions and is a good place to run some logic.
I hope your help @ClockworkOcean is more meaningful in other topics.
Thank you for your caring and heartwarming answer.
I think you’ll find that the easiest solution is to adapt to what is actually happening, and not try your hardest to force a solution where there just isn’t one.
This applies to life also, you might have discovered.
I expect either help or a qualified answer than a flapping “do it differently”. If you cannot give constructive criticism then you don’t have to answer. My initial question is not an out of this world question.
If I REQUIRE a value before I move on, then I create a loop that will loop until I get what I want. Then it can move on. Often with casting for GameState, I’ll create a 0.2 second delay on the fail and loop back into the cast. That way it won’t freeze if it never succeeds. It will just loop until the gamestate is created and then it will be true and create a reference for me.
I thought of another way too where once the game state is created, it will then trigger everything to start their code. Like have a BeginPlayAfterGS event that is triggered when the game state is made.
I also find the characters are a bit random on when they are created too. Watching the 4 windows pop up and seeing all different times for them all to spawn in.
I was also thinking about the Game Mode having like a “When everything is spawned, run all the beginplays”. So like If GSref is valid, if valid number of characters = play list length, then call the DelayedBeginPlay event in all the references.
But the loop works for me so I just do it that way. No real control over the order in which they execute but it works.
Good idea! I did it now like this in the gamestate class:
.h file
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnBeginPlayCompleted);
UPROPERTY(BlueprintAssignable)
FOnBeginPlayCompleted OnBeginPlayCompleted;
bool bBeginPlayCompleted = false;
.cpp file
void AMyGameState::BeginPlay()
{
Super::BeginPlay();
bBeginPlayCompleted = true;
OnBeginPlayCompleted.Broadcast();
}
And then where I need to wait for the gamestate being finished:
.cpp
if (AMyGameState* GS = Cast(GetWorld()->GetGameState()))
{
if (GS->bBeginPlayCompleted)
{
TriggerFunction();
}
else
{
GS->OnBeginPlayCompleted.AddDynamic(this, &AMyListenerClass::TriggerFunction);
}
}
Maybe also the HasBegunPlay function works but I also wanted the delegate.
cheers!