Best Practice: Actor vs. Actor Component for temporary logic

Hi, it’s first time posting in the forum.

You have a logic that needs to run on an actor for a temporary duration. What is better, spawning an actor with the logic and attaching it to the target actor, or adding an actor component to the target actor?

I thought an actor is more expensive because it has a baggage of information including transform, etc. that Actor Component doesn’t have. Actor Component may have some other information but my guess is it has less.

What is the best practice here?

Thanks in advance!

@hanstar17 Can you be a bit more specific? Are you talking about placing actors from the level BP vs actor components within a BP?..

Actor components work really well for something like this. Unlike actors, they cannot handle timelines (since they’re components themselves) or own any other components, though.

A crude example and a bit more info here:

https://answers.unrealengine.com/que…9785/view.html

Not sure about performance (probably much faster to spawn) but they sure are quite flexible. Decluttering actors by adding / removing functionality on the go is useful. They are somewhat neater than child actors, too. The biggest downside (for me) is not having access to a timeline, but you can sample an external curve instead and / or adjust tick interval.

I’ve been using them specifically for this. Did not run into any major snags apart from exposed structs not updating reliably - not sure if this is fixed in 4.22 but there’s is / was a pending bug for that.

1 Like

Actors have about 8200 lines of code and ActorComponents roughly 2900 so it is safe to assume that Components not only use less memory but also are much lighter than Actors.

Keep in mind how you reference the Components if you have a lot of them on a single Actor since functions that look-up components will be less efficient the more Components you have.

Learn to use the UObject class.
It’s much cheaper to use than both, can also tick if really needed.

Hi [USER=“434”]BrUnO XaVIeR[/USER] , how do you handle destroying of UObjects in blueprints? I couldn’t find anything like the Destroy Actor node for it. Do you just remove all external references to the object when you don’t need it?

Disable whatever you have to disable on the UObject then set the UPROPERTY pointer to null.
When garbage collector execute it will destroy the UObject that has no pointers holding it anymore.

Thanks for the info [USER=“434”]BrUnO XaVIeR[/USER].

@hanstar17 If you’re looking for performance comparisons, this reddit post has benchmark results on spawn/destroy times for both (when compared with ticks disabled and no components on the actor): https://www.reddit.com/r/unrealengine/comments/74hgi5/performance_bp_actors_vs_actor_components/

Thanks for reply. I have a logic that I want to run on an actor. Let’s say a status effect that buffs an actor’s attack speed for 5 seconds. You can spawn an actor with buff logic and attach it to the target actor and destroy itself after 5 seconds, or add a buff component to the target actor that removes itself after 5 seconds.

That’s good to know, thanks :slight_smile: It makes sense that you can’t add component to an actor component.

My biggest problem with Actor Component is I can’t add a component to an *Actor *in the Blueprint. For some reason, Add Component method is only available in Actor blueprint. I don’t understand why this restriction is imposed when you can do it in C++ without any issues. I asked a question in answerHub but didn’t get any useful answer yet:

Yeah. :slight_smile: Thanks. Yes, I just checked FindComponentByClass code and it iterates the entire component set until it finds one of the class type. It won’t be cheap if you have many, so I usually cache components that aren’t temporary.

Actually, I am looking at this option as well. Thanks for the suggestion! But to tick, somebody needs to call the tick function, and that is going to be a managerial actor or actor component, I guess?

Nice, thanks for the benchmark info. Good numbers to remember. :slight_smile:

No, there are tools to make an UObject tick:

That is really good information… thank you so much BrUnO!

Just thought Id jump in and mention Timers when used with a Function Name allow the inclusion of an Object as well, this works great for temporary tasks but it does depend on what level of access you need to an Actor, sometimes Components are better in that you dont need to create and destroy them at all simply activate/deactivate. If its a task which constantly switches I would personally lean towards components as there might be issues with Objects being Garbage Collected.