Decouple the code

There are a few spots that seem like the code could be a little cleaner, for example: we call Controller->GameHasEnded() from the GameMode and call the GameMode from a character, etc.

Addtionally, this code caused us an error when DetachFromControllerPendingDestroy() was called before CSGameMode->PawnKilled(this) and had to be debugged.

// Character Class
	if (IsDead())
	{
        // calling DetachFromControllerPendingDestroy() here causes a bug in GameMode
		if (ACryoStrikeGameModeBase* CSGameMode = GetWorld()->GetAuthGameMode<ACryoStrikeGameModeBase>(); CSGameMode != nullptr)
		{
			CSGameMode->PawnKilled(this);
		}
		DetachFromControllerPendingDestroy();
		GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	}

I’m on the fringe of my understanding. I believe this is referred to as code that needs decoupling. If someone would help me with the terminology?

Is there even an issue with doing things this way, say in a much larger code base? Maybe this is completely normal, IDK. If there is an issue, What are some solutions? I’m familiar with interfaces, I’ve aware of the message and subscribe system, and event dispachers. Is there a certain Design Pattern that could have helped? I’m just trying to brain storm a little. The course has been great, Sam has been great, and I understand something like this is outside the scope of this course.

Is there an issue, and is there a solution? In the context of a larger code base.

1 Like

Well you could shift the responsibility of detaching to the game mode as its using controllers to determine the game state so would make sense for it to “release” them, so would have

for (AShooterAIController* Controller : TActorRange<AShooterAIController>(GetWorld()))
{
    if (!Controller->IsDead())
    {
        // Game isn't over so detach the one that was just killed
        PawnKilled->DetachFromControllerPendingDestroy();
        return;
    }
}
1 Like

Dan, as always, for the win! Very specific answer to such a vague question. Thanks!

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

Privacy & Terms