CreateDefaultSubobject confusion

I have a Lamp actor like this:

.h

UPROPERTY(EditAnywhere, BlueprintReadOnly)
ULightComponent* lightSource;

.cpp Constructor

lightSource = CreateDefaultSubobject<ULightComponent, UPointLightComponent>(TEXT("Light Source"));

When I drag a Lamp actor onto the stage, its properties panel includes a dropdown that seems to allow me to select a different light component type:

52696-dropdown.jpg

However, if I select any of it (say a Spot Light for this example), a new component appears at the bottom of the tree:

52697-newcomponent.jpg

And I can’t edit it:

52698-errormsg.jpg

At this point, Light Source does point to a SpotLightComponent–if I debug, I can see that the lightSource variable is now of that new type, meaning it is technically a UPROPERTY in c++, contrary to the error message above. However, it displays as an uneditable, extra component in the editor.

What I expected was that selecting SpotLight from the dropdown would replace the point light I had before; it wouldn’t be added as a new component, it’d replace and be called lightSource in the child components tree, and I would be able to click on it on said tree and edit as normal.

Questions:

  1. What is the point of the dropdown?
  2. How can I write the class so that I can replace the light type cleanly as described? As it stands right now, there is no point in storing a ULightComponent* when I can’t actually take advantage of that abstract base.

I got this thing too, it’s annoying, i think it’s a bug.

Try removing specifiers from UPROPERTY(), as hind text tell you, you just need UPROPERTY() so engine can see varable, not to make it EditAnywhere

Yeah you are right, i switched from EditAnywhere to VisibleAnywhere and it works like i wanted. Sometimes it happens to confuse the two and then weird stuff happens :slight_smile:

I removed all the specifiers from UPROPERTY(), but the result was that I don’t get a dropdown at all–I don’t get to choose my desired light type. What do you mean by “as hind text tell you” by the way?

I tried this but did not get a dropdown to choose my desired light type from at all. Can you post the relevant code/screenshots of how it looks like on your end?

Hey, i just looked and ULightComponent is an abstract class, it’s the base class for the lights components (DirectionalLight, PointLight, SpotLight). So you need to use for example :

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UPointLightComponent* lightSource;

and it should work fine.

Thanks for the help, but it really makes no sense–I want to be able to choose whether the component is a DirectionalLight/PointLight/SpotLight in the editor, so it makes sense to use the abstract base. Tried using UPointLightComponent* anyway but it didn’t work. Are we perhaps looking for different behaviour?

Then use TSubclassOf and with code initiate right type of component based on that varable

Then use TSubclassOf and with code initiate right type of component based on that varable, you would need to do that on runtime thru, which will make component uneditable. Also don’t place any LightComponents in base class if you have any

It needs to be editable, there is no meaning if it’s not. It just doesn’t make sense why I get a dropdown when I can’t work with it in any meaningful way.