Getting an error after adding dependency injection

Edit: moving my #include “mover.h” from TriggerComponent.cpp to TriggerComponent.h fixed this.

Runtime error after adding dependency injection (github.com)

Getting a blueprint failed to compile error. It’s saying the Mover object reference doesn’t match the property mover.

// Fill out your copyright notice in the Description page of Project Settings.


#include "Mover.h"
#include "Math/UnrealMathUtility.h"

// Sets default values for this component's properties
UMover::UMover()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UMover::BeginPlay()
{
	Super::BeginPlay();
	StartLocation = GetOwner()->GetActorLocation();
}


// Called every frame
void UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (ShouldMove)
	{
		UE_LOG(LogTemp, Display, TEXT("Moving"));
		FVector currentLocation = GetOwner()->GetActorLocation();
		FVector targetLocation = StartLocation + MoveOffset;
		float speed = FVector::Distance(StartLocation, targetLocation) / MoveTime;
		FVector newLocation = FMath::VInterpConstantTo(currentLocation, targetLocation, DeltaTime, speed);
		GetOwner()->SetActorLocation(newLocation);
	}
}

void UMover::SetShouldMove(bool shouldMove)
{
	ShouldMove = shouldMove;
}


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Mover.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API UMover : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UMover();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	UFUNCTION(BlueprintCallable)
	void SetShouldMove(bool shouldMove);

private:
	UPROPERTY(EditAnywhere)
	FVector MoveOffset;

	UPROPERTY(EditAnywhere)
	float MoveTime = 4;

	bool ShouldMove = false;	

	FVector StartLocation;
};

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "TriggerComponent.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API UTriggerComponent : public UBoxComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UTriggerComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;	

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	UFUNCTION(BlueprintCallable)
	void SetMover(UMover* mover);

private:
	UPROPERTY(EditAnywhere)
	FName TriggeredByTag = "Player";

	UMover* Mover;

	AActor* GetAcceptableActor() const;

};

#include "TriggerComponent.h"
#include "Mover.h"

UTriggerComponent::UTriggerComponent()
{
    PrimaryComponentTick.bCanEverTick = true;
}

void UTriggerComponent::BeginPlay()
{
    Super::BeginPlay();
}

// Called every frame
void UTriggerComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    AActor* actor = GetAcceptableActor();
    Mover->SetShouldMove(actor != nullptr);
}

void UTriggerComponent::SetMover(UMover* mover)
{
    Mover = mover;
}

AActor* UTriggerComponent::GetAcceptableActor() const
{
    TArray<AActor*> overlappingActors;
    GetOverlappingActors(overlappingActors);
    for (AActor* actor : overlappingActors)
    {
        if (actor->ActorHasTag(TriggeredByTag))
        {
            return actor;
        }

    }
    return nullptr;
}

Did you build with Unreal closed?

Yes but like I said in my edit it looks like the issue was that I needed to move my #include for Mover.h to TriggerComponent.h (I had it in TriggerComponent.cpp).

But this raises an interesting point. Having to frequently close Unreal, rebuild, and then reopen Unreal seems like a serious disruption to my workflow. It reminds me of this joke that was going around the internet back in the 90s, specifically #3: "* 1. Occasionally your car would die on the freeway for no reason. You would have to pull to the side of the road, close all of the windows, shut off the car, restart it, and reopen the windows before you could continue. For some reason you would simply accept this.*

Do Unreal developers really accept having to close Unreal, rebuild and reopen again when making changes?

We discussed this on the Game Dev Show yesterday.

Not whenever making changes, no. However you do need to build before re-opening the project if you used Live Coding. So just build after you close Unreal after you are done for the day.

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

Privacy & Terms