Correct way to reference a bool across scripts?

Hi there

Sorry if this has been answered anywhere else, i couldn’t find it!

I would like to know the correct way to reference a bool or any variale across scripts to check the condition, my atempt below errors (illegal reference to non-static member ‘ATopDown2Character::bActiveUnit’), so i have comented it out, it is at the begining of ATopDown2PlayerController::MoveToMouseCursor().

I have created a bool here: TopDown2Character.h



// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Character.h"
#include "TopDown2Character.generated.h"

UCLASS(Blueprintable)
class ATopDown2Character : public ACharacter
{
	GENERATED_UCLASS_BODY()


	/* Have we clicked the unit? */
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Unit)
	bool bActiveUnit;

	/** Top down camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	TSubobjectPtr<class UCameraComponent> TopDownCameraComponent;

	/** Camera boom positioning the camera above the character */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	TSubobjectPtr<class USpringArmComponent> CameraBoom;

};



I have included this header file within the: TopDown2PlayerController.cpp



// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "TopDown2.h"
#include "TopDown2PlayerController.h"
#include "TopDown2Character.h"
#include "AI/Navigation/NavigationSystem.h"

ATopDown2PlayerController::ATopDown2PlayerController(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	bShowMouseCursor = true;
	DefaultMouseCursor = EMouseCursor::Crosshairs;
}

void ATopDown2PlayerController::PlayerTick(float DeltaTime)
{
	Super::PlayerTick(DeltaTime);

	// keep updating the destination every tick while desired
	if (bMoveToMouseCursor)
	{
		MoveToMouseCursor();
	}
}

void ATopDown2PlayerController::SetupInputComponent()
{
	// set up gameplay key bindings
	Super::SetupInputComponent();

	InputComponent->BindAction("SetDestination", IE_Pressed, this, &ATopDown2PlayerController::OnSetDestinationPressed);
	InputComponent->BindAction("SetDestination", IE_Released, this, &ATopDown2PlayerController::OnSetDestinationReleased);

	// support touch devices 
	InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ATopDown2PlayerController::MoveToTouchLocation);
	InputComponent->BindTouch(EInputEvent::IE_Repeat, this, &ATopDown2PlayerController::MoveToTouchLocation);
}

void ATopDown2PlayerController::MoveToMouseCursor()
{

		
	//if (!ATopDown2Character::bActiveUnit)
	//return;
	

	
	// Trace to see what is under the mouse cursor
	FHitResult Hit;
	GetHitResultUnderCursor(ECC_Visibility, false, Hit);

	if (Hit.bBlockingHit)
	{
		// We hit something, move there
		SetNewMoveDestination(Hit.ImpactPoint);
	}
}

void ATopDown2PlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location)
{
	FVector2D ScreenSpaceLocation(Location);

	// Trace to see what is under the touch location
	FHitResult HitResult;
	GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult);
	if (HitResult.bBlockingHit)
	{
		// We hit something, move there
		SetNewMoveDestination(HitResult.ImpactPoint);
	}
}

void ATopDown2PlayerController::SetNewMoveDestination(const FVector DestLocation)
{
	APawn* const Pawn = GetPawn();
	if (Pawn)
	{
		UNavigationSystem* const NavSys = GetWorld()->GetNavigationSystem();
		float const Distance = FVector::Dist(DestLocation, Pawn->GetActorLocation());

		// We need to issue move command only if far enough in order for walk animation to play correctly
		if (NavSys && (Distance > 120.0f))
		{
			NavSys->SimpleMoveToLocation(this, DestLocation);
		}
	}
}

void ATopDown2PlayerController::OnSetDestinationPressed()
{
	// set flag to keep updating destination until released
	bMoveToMouseCursor = true;
}

void ATopDown2PlayerController::OnSetDestinationReleased()
{
	// clear flag to indicate we should stop updating the destination
	bMoveToMouseCursor = false;
}



Thanks in advance

You need to get a reference to the character, currently you’re attempting to access a non-static member with the static syntax.

This should do the trick.


ATopDown2Character* Character = Cast<ATopDown2Character>(GetPawn());

if (Character == nullptr || !Character->bActiveUnit) { return; }

As an aside, you should declare booleans using the bitfield syntax

rather than



UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Unit)
bool bActiveUnit;


You should use



UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Unit)
uint32 bActiveUnit : 1;


/ Kyle