Getting a error cant understand why

im getting a error and i bet its simple and easy to solve but i cant figure out why its not working. The ActorDistance is a float variable declared alright in the header file, PlayerCharacter is UPROPERTY same has SpotlightPicker, GetDistanceTo is a function made already by epic. I want to give inside GetDistanceTo arguments AActor* PlayerCharacter and OtherActor give it SpotlightPicker.

Everything is ok only the OtherActor has a redline under it and it says “Identifier “OtherActor” is undefined”.

My Actor cpp file:

ActorDistance = GetDistanceTo(this->PlayerCharacter, OtherActor->SpotlightPicker);

What the function looks like:

/** Returns the distance from this Actor to OtherActor. */
	UFUNCTION(BlueprintCallable, Category = "Utilities|Transformation")
	float GetDistanceTo(const AActor* OtherActor) const;

Trying to do these:

Hello!

Your usage is “wrong”

/** Returns the distance from this Actor to OtherActor. */
    UFUNCTION(BlueprintCallable, Category = "Utilities|Transformation")
    float GetDistanceTo(const AActor* OtherActor) const;

ActorDistance = GetDistanceTo(this->PlayerCharacter, OtherActor->SpotlightPicker);

Problem is here in your function:

ActorDistance = GetDistanceTo(this->PlayerCharacter, OtherActor->SpotlightPicker);

GetDistanceTo function need only one input parameter which is an actor float
but when you try to call this function you filled with 2 actor (2 parameter) actually…

You need modify the function declaration in this way:

     UFUNCTION(BlueprintCallable, Category = "Utilities|Transformation")
     float GetDistanceTo(const AActor* TargetActor, const AActor* OtherActor) const;

I try to not confuse you, but here is some learning stuff about c++ and function parameters…

http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/

Hope this helps! :slight_smile:

The way its written is :
/** Returns the distance from this Actor to OtherActor. /
UFUNCTION(BlueprintCallable, Category = “Utilities|Transformation”)
float GetDistanceTo(const AActor
OtherActor) const;

I dont really want to modify, i could. But dont want to since thats how epic wrote it. And i cant just leave it with 1 argument since i dont want it to be self target.

Hello.

You can simply use it like so:

ActorDistance = PlayerCharacter->GetDistanceTo(OtherActor->SpotlightPicker);

The target pin in blueprints you see is just an instance of an object on which you call a function, so in this case it’s PlayerCharacter.

Hope this helps.

@Atheist91 I can see how that work but now im getting a error on OtherActor. It says “identifier “OtherActor” is undefined” since its a const. How can it be fixed i tired using ‘const_cast’ but it didnt work.

I’m not sure how your code exactly look like so it’s kind of hard to give you precise answer. However ‘OtherActor->SpotlightPicker’ should be replaced with your variable containing an actor your want to measure distance to.

The error itself doesn’t seem to have anything to do with constness. It simply cannot find variable called OtherActor in your code.

header file:

#include “CoreMinimal.h”
#include “GameFramework/Character.h”
#include “GameFramework/Actor.h”
#include “VLDVisual.generated.h”

class ASpotLight;

UCLASS()
class TESTPROJECTVISUALS_API AVLDVisual : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
AVLDVisual();

//** Actor Variables
float Current;
float VolumetricLightSet;
float VolumetricDataSaved;
float PlayerLocationPrevious;
float DefaultSphereRadius = 340.0;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpotLight|Radius", meta = (ClampMin = "0.0"))
float Scale = 1.f;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpotLight")
AActor* SpotLightPicker;

UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Player")
ACharacter* PlayerCharacter;

float ActorsDistance;
float FloatMath_ScaleAndSphere;
bool bWithinRange;

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

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

};

cpp file :

#include "VLDVisual.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Engine/Engine.h"



// Sets default values
AVLDVisual::AVLDVisual()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	
	FloatMath_ScaleAndSphere = Scale * DefaultSphereRadius;
	
    ActorsDistance = PlayerCharacter->GetDistanceTo(OtherActor->SpotLightPicker);
	
}

// Called when the game starts or when spawned
void AVLDVisual::BeginPlay()
{
	Super::BeginPlay();



}

// Called every frame
void AVLDVisual::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);



	
}

Okay. Then assuming that you want to measure distance between PlayerCharacter and SpotLightPicker the correct usage would be:

ActorDistance = PlayerCharacter->GetDistanceTo(SpotLightPicker);

Or:

ActorDistance = SpotLightPicker->GetDistanceTo(PlayerCharacter);

Doesn’t really matter which one.

Hope this helps. :]

Thank you for the help. Looking at it now makes sense. Alway something simple i make it seem so complicated. Thank you again.