I'm trying to write an Actor in Unreal Engine C++, that gets texture data from a render target.
So, I have a class with my two custom methods:
#include "RenderActor.h"
#include "Engine.h"
#include <stdio.h>
// Sets default values
ARenderActor::ARenderActor()
{
// 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;
UE_LOG(LogTemp, Warning, TEXT("Init RenderActor\n"));
}
// Called when the game starts or when spawned
void ARenderActor::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("BeginPlay RenderActor\n"));
UTextureRenderTarget2D* target = GetTextureByName("MyCanvas");
}
// Called every frame
void ARenderActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
UTextureRenderTarget2D* GetTextureByName(wchar_t* name)
{
return LoadObject<UTextureRenderTarget2D>(NULL, name, NULL, LOAD_None, NULL);
}
int ARenderActor::GetTextureData(UTextureRenderTarget2D* TextureRenderTarget ,void* out_ptr,int length)
{
int sx=TextureRenderTarget->SizeX,sy=TextureRenderTarget->SizeY;
TArray<FColor> SurfData;
FRenderTarget *renderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
check((sx*sy*4)<=length);
renderTarget->ReadPixels(SurfData);
memcpy(out_ptr,reinterpret_cast<void*>(SurfData.GetData()),sx*sy*4);
return sx*sy*4;
}
But I'm getting a compile error:
[1/3] Compile RenderActor.cpp
[1m/home/starrabb1t/Documents/Unreal Projects/MemRender/Source/MemRender/RenderActor.cpp:30:9: [0m[0;1;31merror: [0m[1mno matching function for call to 'LoadObject'[0m
return LoadObject<UTextureRenderTarget2D>(NULL, name, NULL, LOAD_None, NULL);
[0;1;32m ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[0m[1m/home/starrabb1t/UnrealEngine/Engine/Source/Runtime/CoreUObject/Public/UObject/UObjectGlobals.h:1345:11: [0m[0;1;30mnote: [0mcandidate function template not viable: no known conversion from 'wchar_t *' to 'const TCHAR *' (aka 'const char16_t *') for 2nd argument[0m
inline T* LoadObject( UObject* Outer, const TCHAR* Name, const TCHAR* Filename=nullptr, uint32 LoadFlags=LOAD_None, UPackageMap* Sandbox=nullptr )
[0;1;32m ^
[0m1 error generated.
I don't understand, what's wrong. There is a function LoadObject() here:
"Runtime/CoreUObject/Public/UObject/UObjectGlobals.h"
I included this header in RenderActor.h class file, but with no result.
There are two compiler errors.
I think you're getting this error because you haven't included the definition of the template parameter for your LoadObject
call (though I'm not certain because you didn't include RenderActor.h
in your question.
Try adding #include "TextureRenderTarget2D.h"
to fix this as the definition of the template parameter must be available when you call LoadObject
.
wchar_t *
to const TCHAR *
The end of that compiler error says 'const TCHAR *' (aka 'const char16_t *')
, which is not the same type as wchar_t*
.
When you're writing UE4 code, the standard thing to do is make your functions take FString
s or FName
s. You rarely see raw character pointers passed around. If you do need to pass a character pointer around, then you should use TCHAR*
to be compatible with the rest of the string handling functions.
To make your code work like the majority of UE4's code base, change:
UTextureRenderTarget2D* GetTextureByName(wchar_t* name)
to
UTextureRenderTarget2D* GetTextureByName(const TCHAR* name)
And then in BeginPlay
change:
UTextureRenderTarget2D* target = GetTextureByName("MyCanvas");
to
UTextureRenderTarget2D* target = GetTextureByName(TEXT("MyCanvas"));