// MyParticleCallbackHandler.cpp
include “MyParticleCallbackHandler.h”
UMyParticleCallbackHandler::UMyParticleCallbackHandler()
{
NiagaraComponent = nullptr;
}
void UMyParticleCallbackHandler::SetNiagaraComponent(UNiagaraComponent* InNiagaraComponent)
{
NiagaraComponent = InNiagaraComponent;
}
void UMyParticleCallbackHandler::OnParticleDataReceived(TSharedPtr DataBuffer, FNiagaraSystemInstance* SystemInstance)
{
if (DataBuffer.IsValid())
{
const int32 NumParticles = DataBuffer->GetNumInstances();
UE_LOG(LogTemp, Warning, TEXT(“DataBuffer is valid”));
// Get the particle positions and sizes.
// const FVector* Positions = DataBuffer->GetPositions();
// const FVector* Sizes = DataBuffer->GetSizes();
// Do something with the particle positions and sizes.
// ...
}
}
UClass* UNiagaraParticleCallbackHandler::GetPrivateStaticClass()
{
return UNiagaraParticleCallbackHandler::StaticClass();
}
// // MyParticleCallbackHandler.h
#pragma once
include “NiagaraDataSet.h”
include “NiagaraSystemInstance.h”
include “NiagaraFunctionLibrary.h”
include “NiagaraComponent.h” // Include the Niagara header
include “NiagaraDataInterfaceExport.h” // Include the Niagara export data interface header
include “EngineUtils.h”
include “NiagaraEmitterHandle.h”
include “NiagaraDataInterface.h”
include “NiagaraEmitter.h”
include “NiagaraDataSet.h”
include “NiagaraDataInterfaceExport.h”
class UMyParticleCallbackHandler : public UNiagaraParticleCallbackHandler
{
public:
UMyParticleCallbackHandler();
void SetNiagaraComponent(UNiagaraComponent* InNiagaraComponent);
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Niagara")
void OnParticleDataReceived(TSharedPtr<FNiagaraDataBuffer> DataBuffer, FNiagaraSystemInstance* SystemInstance);
UClass* GetPrivateStaticClass();
private:
UNiagaraComponent* NiagaraComponent;
};
UWorld* World = GetWorld(); // Get the reference to the current world
FName ActorName = FName(TEXT(“rain_sample_bp_2”)); // Replace with the name of your Niagara actor
for (TActorIterator<AActor> It(World); It; ++It)
{
AActor* Actor = *It;
FString name = Actor->GetName();
if (name == ActorName.ToString())
{
UE_LOG(LogTemp, Warning, TEXT("Niagara Actor: %s"), *name);
// Get the Niagara component from the actor
UNiagaraComponent* NiagaraComponent = Actor->FindComponentByClass<UNiagaraComponent>();
if (NiagaraComponent)
{
UNiagaraSystem* NiagaraSystem = NiagaraComponent->GetAsset();
// Create an instance of the UMyParticleCallbackHandler class.
UMyParticleCallbackHandler* myParticleCallbackHandler = NewObject<UMyParticleCallbackHandler>(this);
//myParticleCallbackHandler->GetClass();
// Set the Niagara component for the UMyParticleCallbackHandler instance.
myParticleCallbackHandler->SetNiagaraComponent(NiagaraComponent);
// Register the UMyParticleCallbackHandler instance with the Niagara system.
//NiagaraSystem->RegisterParticleCallbackHandler(myParticleCallbackHandler);
TArray<FNiagaraVariable> OutParameters;
NiagaraSystem->GetExposedParameters().GetUserParameters(OutParameters);
for(FNiagaraVariable Parameter : OutParameters){
UE_LOG(LogTemp, Warning, TEXT("OutParameters %s"),*Parameter.GetName().ToString());
}
// Find the 'call_back' user-defined object parameter
for (const FNiagaraVariable& Parameter : OutParameters)
{
if (Parameter.GetName() == TEXT("call_back"))
{
NiagaraComponent->SetNiagaraVariableObject(Parameter.ToString(),myParticleCallbackHandler);
}
}
}
}
}
}
Can someone check the code and tell me how to access the niagara particle information.