Unsure if Best Practice, but it sure is DRY

This was a difficult challenge for me because functions require a parameter types to be declared when you pass them. Turns out, everything is a UObject, and that’s why you can pass things to IsValid() easily, which makes things a lot easier. I think the term is polymorphism if anyone wants to read more. Anyway, I created this function and passed it to a bool in BeginPlay() so that it wouldn’t be called every frame.

bool UOpenDoor::ValidCheck(UObject* Item)
{
	if (!IsValid(Item))
	{
		UE_LOG(LogTemp, Error, TEXT("%s missing component and will not work properly, make sure components are added correctly."), *GetOwner()->GetName());
		return false;
	}

	return true;
}
ComponentsSet = ValidCheck(ActorThatOpens) && ValidCheck(PressurePlate);

Then I updated my if statement to just read that bool if it was true or false.

if (ComponentsSet && PressurePlate->IsOverlappingActor(ActorThatOpens))
{
	OpenDoor(DeltaTime);
}

Is it overkill? Probably for this use case, but I want to know I have the ability to do something like this if necessary, and I learned something really neat about the AAtriggerVolume, and AActor types. I’m hoping that this doesn’t cause too high of a load because I’m just passing pointers, but I think I’m satisfied with this specific case.

1 Like

Being a strong advocate of DRY (Don’t Repeat Yourself), I strongly approve this refactor!

1 Like

Privacy & Terms