Unreal engine 4.27 C4458

I’m trying to implement to my stealthmeter functions to get scene lights color R, G and B:

CPP file:

#include "LightDetector.h"


// Sets default values
ALightDetector::ALightDetector()
{
 	// 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;

}

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

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

}

void ALightDetector::ProcessRenderTexture(UTextureRenderTarget2D *detectorTexture) {
	// Read the pixels from our RenderTexture and store the data into our color array
	// Note: ReadPixels is allegedly a very slow operation
	fRenderTarget = detectorTexture->GameThread_GetRenderTargetResource();
	fRenderTarget->ReadPixels(pixelStorage);

	// We iterate through every pixel we retrieved and find the brightest pixel
	for (int pixelNum = 0; pixelNum < pixelStorage.Num(); pixelNum++) {
		pixelChannelR = pixelStorage[pixelNum].R;
		pixelChannelG = pixelStorage[pixelNum].G;
		pixelChannelB = pixelStorage[pixelNum].B;

		// Use a formula to determine brightness based on pixel color values. Source for Formula used:
		// www.stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
		currentPixelBrightness = ((0.299 * pixelChannelR) + (0.587 * pixelChannelG) + (0.114 * pixelChannelB));
		float MapR = pixelChannelR;
		float MapG = pixelChannelG;
		float MapB = pixelChannelB;


		// If the current pixel we just processed is brighter than the previously brightest pixel, replace it with the new pixel
		if (currentPixelBrightness >= brightnessOutput) {
			brightnessOutput = currentPixelBrightness;
		}
		if (MapR >= brightnessOutputR) {
			brightnessOutputR = MapR;
		}
		if (MapG >= brightnessOutputG) {
			brightnessOutputG = MapG;
		}
		if (MapB >= brightnessOutputB) {
			brightnessOutputB = MapB;
		}
	}
}

 
float ALightDetector::CalculateBrightness() {
	// Ensure that the user has actually supplied us with RenderTextures
	if (detectorTextureTop == nullptr || detectorTextureBottom == nullptr) {
		return 0.0f;
	}
	// Reset our values for the next brightness test
	currentPixelBrightness = 0;
	brightnessOutput = 0;

	// Process our top and bottom RenderTextures
	ProcessRenderTexture(detectorTextureTop);
	ProcessRenderTexture(detectorTextureBottom);


	// At the end we return the brightest pixel we found in the RenderTextures
	return brightnessOutput;
}

float ALightDetector::CalculateBrightnessRed() {
	// Ensure that the user has actually supplied us with RenderTextures
	if (detectorTextureTop == nullptr || detectorTextureBottom == nullptr) {
		return 0.0f;
	}
	// Reset our values for the next brightness test
	MapR = 0;
	brightnessOutputR = 0;

	// Process our top and bottom RenderTextures
	ProcessRenderTexture(detectorTextureTop);
	ProcessRenderTexture(detectorTextureBottom);


	// At the end we return the brightest pixel we found in the RenderTextures
	return brightnessOutputR;
}

float ALightDetector::CalculateBrightnessGreen() {
	// Ensure that the user has actually supplied us with RenderTextures
	if (detectorTextureTop == nullptr || detectorTextureBottom == nullptr) {
		return 0.0f;
	}
	// Reset our values for the next brightness test
	MapG = 0; 
	brightnessOutputG = 0;

	// Process our top and bottom RenderTextures
	ProcessRenderTexture(detectorTextureTop);
	ProcessRenderTexture(detectorTextureBottom);


	// At the end we return the brightest pixel we found in the RenderTextures
	return brightnessOutputG;
}

float ALightDetector::CalculateBrightnessBlue() {
	// Ensure that the user has actually supplied us with RenderTextures
	if (detectorTextureTop == nullptr || detectorTextureBottom == nullptr) {
		return 0.0f;
	}
	// Reset our values for the next brightness test
	MapB = 0;
	brightnessOutputB = 0;

	// Process our top and bottom RenderTextures
	ProcessRenderTexture(detectorTextureTop);
	ProcessRenderTexture(detectorTextureBottom);


	// At the end we return the brightest pixel we found in the RenderTextures
	return brightnessOutputB;
}

H file:

#pragma once

#include <memory>

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/TextureRenderTarget2D.h"
#include "UnrealClient.h"
#include "LightDetector.generated.h"

UCLASS()
class STEALTHMETEREXAMPLE_API ALightDetector : public AActor
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, Category = "LightDetection")
	float CalculateBrightness();
	UFUNCTION(BlueprintCallable, Category = "LightDetection")
	float CalculateBrightnessRed();
	UFUNCTION(BlueprintCallable, Category = "LightDetection")
	float CalculateBrightnessGreen();
	UFUNCTION(BlueprintCallable, Category = "LightDetection")
	float CalculateBrightnessBlue();

	void ProcessRenderTexture(UTextureRenderTarget2D *texture);

	TArray<FColor> pixelStorage;
	float pixelChannelR{ 0 };
	float pixelChannelG{ 0 };
	float pixelChannelB{ 0 };
	//outputs :
	float brightnessOutput{ 0 };
	float brightnessOutputR{ 0 };
	float brightnessOutputG{ 0 };
	float brightnessOutputB{ 0 };
	//temp values:
	float currentPixelBrightness{ 0 };
	float MapR{ 0 };
	float MapG{ 0 };
	float MapB{ 0 };
	FRenderTarget *fRenderTarget;

	// The Render Textures we will be passing into the CalculateBrightness() method
	UPROPERTY(EditAnywhere)
	UTextureRenderTarget2D *detectorTextureTop;
	UPROPERTY(EditAnywhere)
	UTextureRenderTarget2D *detectorTextureBottom;

public:	
	// Sets default values for this actor's properties
	ALightDetector();

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

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

But MapR, MapB, MapG float variables are under warning C4458. Also those functions do not appear in blueprint editor (CalculateBrightnessR,G,B). Any ways to fix it?

Make them public, or at least protected iirc for blueprint function & variables

[C4458] is beacause ProcessRenderTexture has a local variables MapR,MapG,MapB shadowing the class variables with same name

Hello @526088

Were you able to resolve the C4458 issue with the information Firefly provided? If not, we can look further into the issue.

Not really. I’m a complete noob to C++ and I don’t know what he’s talking about

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