I’m doing a simple paddle game with Blueprints at first and C++ after.
I have the current workflow in my blueprint game mode:
And this is my code base on:
void APaddleGameGameMode::SpawnBall()
{
UWorld *world = GetWorld();
if (world)
{
Ref_GameBall = world->SpawnActorDeferred<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), NULL, NULL, ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding);
if (Ref_GameBall) world->GetTimerManager().SetTimer(DelayBallMovement, this, &APaddleGameGameMode::SetVelocity, 0.2f);
}
}
void APaddleGameGameMode::SetVelocity()
{
if(Ref_GameBall) Ref_GameBall->ProjectileMovement->Velocity = FVector(Direction * Speed, 0.f, 0.f).RotateAngleAxis(FMath::RandRange(-45.f, 45.f), FVector(0.f, 1.f, 0.f));
Ref_GameBall->ProjectileMovement->Activate();
}
The problem here is, every time the player make a point, the ball is destroyed and a new ball is spawned:
void APaddleGameGameMode::UpdateScore(bool bIsAI)
{
UWorld *world = GetWorld();
if (bIsAI)
{
SPScore++;
Direction = 1.f;
}
else
{
FPScore++;
Direction = -1.f;
}
if (world) world->GetTimerManager().SetTimer(DelaySpawningBall, this, &APaddleGameGameMode::SpawnBall, 1.f);
}
It works fine at the first, the initial, spawnin: ball is spawned and it takes movement; but it does not work at the second spawn, i don’t understand why, the ball is spawned but it does not move.
Can anyone to explain me it and help me to solve it?
Thanks.