I have a ship which is floating in the water and I want to spawn particles (splashes) when the water gets too high or when the ship goes deeper in the water.
So, I created C++ component (sphere) which is checking on tick how deep it is (MyDepth variable). Everything works fine but I want to have 30-40 spheres placed around the ship but very close to the ship hull so that I could iterate though them and spawn particles in the specific shere location.
Few questions:
-
Is there a special function in Unreal which helps calculating the relative to the ship (parent) location? What is the best way of doing it (I don’t want to do it manually)
-
How can I create multiple instances (objects) of my class, initilize them properly and make them visible in the editor? (the only instance I created - TestComponent - is always nullptr and I don’t know why). Also, can I do it without creating lots of components in the editor?
-
What is the best way in general to solve the issue?
.h
#pragma once
#include "CoreMinimal.h"
#include "TrueSkyWaterProbeComponent.h"
#include "Components/ActorComponent.h"
#include "Engine.h"
#include "TestComponent.generated.h"
UCLASS(Blueprintable, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class ALMAZ_API UTestComponent : public UTrueSkyWaterProbeComponent
{
GENERATED_UCLASS_BODY()
~UTestComponent();
float MyDepth;
UTestComponent* TestComponent;
float GetMyDepth();
void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction);
};
.cpp
UTestComponent::UTestComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bStartWithTickEnabled = true;
bAutoActivate = true;
SetComponentTickEnabled(true);
if (TestComponent != nullptr)
{
TestComponent->RegisterComponent();
}
}
UTestComponent::~UTestComponent()
{
}
float UTestComponent::GetMyDepth()
{
MyDepth = UTrueSkyWaterProbeComponent::GetDepth();
return MyDepth;
}
void UTestComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
GetMyDepth();
GEngine->AddOnScreenDebugMessage(-1, 0.f, FColor::Blue, FString::Printf(TEXT("The depth is %f"), MyDepth));
}