Hi there guys, i’m baffled… hope you can help me out.
First of all, i’m creating a component that’s called ChannelSwitchableLightComponent, basically is a light component with a Channel index that can be called to trigger every component with the same channel. The object is as follow
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UChannelSwitchableLightComponent : public USceneComponent, public ISwitchableByChannelInterface
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UChannelSwitchableLightComponent();
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
virtual uint8 GetChannel() override;
virtual void On() override;
virtual void Off() override;
//For constructing on editor
virtual void OnRegister() override;
virtual void OnUnregister() override;
//The channel on which this light listens
UPROPERTY(EditAnywhere, BlueprintReadWrite)
uint8 Channel;
//The light component to use
UPROPERTY(EditAnywhere, BlueprintReadWrite)
ULightComponent *LightComponent;
};
The ULightComponent appears in unreal editor, and it lets me choose from PointLight and others type of ULightComponent which is awesome.
The editor though automatically attaches the LightComponent to my actor and i would like it to be attached to my component instead so i did this.
void UChannelSwitchableLightComponent::OnRegister()
{
Super::OnRegister();
bool found = false;
for (int i = 0; i < AttachChildren.Num(); i++)
{
UActorComponent *children = AttachChildren[i];
if (children->IsA<ULightComponent>())
{
if (children != LightComponent)
children->DestroyComponent();
else
found = true;
}
}
if(LightComponent != nullptr && found == false)
LightComponent->AttachTo(this);
}
Maybe not the most beautiful implementation (if there is another way i’ll gladly change this), but this let me attach the light component to my own component.
The problem raises with blueprints… (on plain components it works).
- If i try to add my component to a blueprint, and try to set the LightComponent to any value, it crashes with the message: “Template Mismatch during attachment. Attaching template component to instanced component.”
Here we can see it crashes at the moment it enters this piece of code on Register
if(LightComponent != nullptr && found == false)
LightComponent->AttachTo(this);
- If i do this on an instance instead, it does not crash on this point, but it crashes in the moment i do any change that would trigger PostEditChangeProperty, because the Actor->GetRootComponent() fails the Assertion
this is on the file
ComponentInstanceDataCache.cpp
on the FComponentInstanceDataCache::ApplyToActor method
the line that fails is
check(Actor->GetRootComponent());
I really don’t know what to do… am i doing something wrong at trying to attach the component, should i use a different aproach?
is there a way to make the user choose a type of light component to use and then instantiate it and attach it with no problems?