Change the crosshair dynamically in the first person template

I’m trying to change the crosshair dynamically in the first person template. The idea is to change the crosshair when targeting an item or hide the crosshair when going into iron sights. I’m using the following as a test:


ABasicFPSHUD::ABasicFPSHUD()
{
	ABasicFPSCharacter* Character = Cast(UGameplayStatics::GetPlayerCharacter(this, 0));
	FString crosshairPath;
	TCHAR* something;
	if (Character != nullptr) {
		UE_LOG(LogTemp, Warning, TEXT("Charcter not null"))
		something = TEXT("/Game/FirstPerson/Textures/hand");
		UE_LOG(LogTemp, Warning, TEXT("the text is %s"), something)
	}
	else {
		something = TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair");
	}
	// Set the crosshair texture
	static ConstructorHelpers::FObjectFinder CrosshairTexObj(something);
	CrosshairTex = CrosshairTexObj.Object;
}

The problem is that the if statement is working and I would think that it would set the CrosshairTextObj to “/game/firstperson/textures/hand” but it falls back on the else statement for some reason and I have no idea why.

Is my approach to dynamically changing the crosshair wrong? What’s the best way to do this in C++?

Hey there, you are doing that initialization on the constructor and probably when he’s executing that, the player hasn’t been assigned with a character yet. You need to have 2 different variables for the crosshair and for the hand and on the DrawHUD function you do the cast and decide which texture to use.

Hi, do I guess I should be doing the check in DrawHUD function? Also, what I intend to do is have a boolean in my BasicFPSCharacter class that changes depending if the character’s line trace is hitting an object to pick up (which is the hand texture instead of a cross hair). Do I need to have a tick function in the HUD class to always check the value of the boolean? I’m just wondering since I’m not sure if it would just automatically update the cross hair, as I want it to change back into a cross hair if the line trace is no longer hitting an pickup object

DrawHUD is your tick function. So do the cast there and check the boolean value and draw the quad with the appropriate texture.

Thanks for the tip, it worked!