Questions about the workflow of using components

Hello everyone!

I’m trying to make a simple thing: a flickering light component that can be reused with any Light component. I have several candelabra blueprints with flames as particle systems and I want to give the candelabras a movable point light with a subtle flickering. I don’t want to copy paste code, so that’s why I wanted to create a component. But I’m having some issues:

  • I could make the component to inherit from PointLightComponent, but I’d like to be able to attach the component to other already existing lights, not to be a light by itself. Besides, if I wanted a flickering directional light, I would have to create another component and copy and paste the same code, right?
  • I’d like to do it the Unity way: just to be able to assign any light component to my flickering component. But it seems that Unreal doesn’t let you reference components from other components. From what I’ve gathered reading the answer hub and the forums, that seems to be the case. If I create a public LightComponent reference, the editor only lets me assign assets in the project, not other components in the same blueprint.
  • I could create the reference to the light in the blueprint itself, and then, from my flickering component, access the owner and then the reference. But what’s the point on having reusable components if you have to add code to the each blueprint anyway?
  • And this is what I have right now: Since I’m not able to set a direct reference to the light component, I added to my flickering component a public string variable where I can enter the name of the light, then I access the owner, get all classes of type LightComponent in the blueprint, and then find the one with the specified name.

This method, as I said, is the one I’m using now and it works. But I don’t like it, because I find it ugly and having to compare strings and find components is not as performant as having a direct reference to the light.

So, for my situation what would you think is the best approach to deal with this with Unreal? Maybe there’s another way I’m not able to come up with.

Thank you very much for your attention!

I’ve recently been experimenting with a more decorator-like approach to components. In your flicker example, one might define a FlickerChildren component that causes all lights that are attached as children to flicker. So, if you wanted a point light and a spotlight to flicker synchronously, you might do something like this:

131160-flicker-01.png

This works rather well for this simple scenario, but it gets a bit more complicated if you want to layer several behaviors on top of each other, since then the components would have walk the tree of all children to find the ones they want to influence.

A simpler approach to combine different behaviors would be attaching components that provide the desired functionality to the components they ought to influence. If we wanted to, e.g., change the colors of our flickering lights independently (say, one from red to green, the other one from blue to yellow, and with different frequencies) we could attach a ChangeColorComponent to each of them like so

131171-flicker-02.png

This makes it easier to customize a component with multiple behaviors, but it becomes, of course, more difficult to apply the same behavior to multiple components.

I’m not sure how well this second approach works in practice, but to it might be worth investigating. I’ve written a quick demo project (in C++, sorry, but I’m way faster programming in C++ than in Blueprints) and uploaded it on [GitHub][3]. Well, it causes the lights to blink, not flicker, but that’s programmer art for you :wink:

Regarding the question of assigning components to other components, if I understand correctly that is mostly a user interface issue, in the sense that the current selection widget does not show sibling components, right? I don’t think it would be too hard to write a type FComponentReference, or so, that stores a component reference and provides a detail panel customization that shows the components of the actor. But it’s probably not possible to implement this in Blueprints, and it requires that two modules are added to a project that uses this (one for the type itself and one editor module for the customization). Would something like this be useful for people working in Blueprints or is setting up a C++ project with several additional modules too much hassle for this functionality?

Thanks for your answer.

Those are some interesting tips that I’m sure I will find useful some time. In my case I ended up using a FComponentReference, but as you say, it is not available on blueprints right now, so I had to do it with C++. But I think it would be quite useful to have that exposed to blueprints as well.