Announcement

Collapse
No announcement yet.

Best Practice: Actor vs. Actor Component for temporary logic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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!

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

    Comment


      #3
      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.

      You have a logic that needs to run on an actor for a temporary duration.
      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.

      Comment


        #4
        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.

        Comment


          #5
          Learn to use the UObject class.
          It's much cheaper to use than both, can also tick if really needed.
          | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

          Comment


            #6
            Hi BrUnO XaVIeR , 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?
            Unreal Possibilities
            Grid Creation Systems | Tower Defense Starter Kit | Wave Spawning System [50% OFF]

            Comment


              #7
              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.
              | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

              Comment


                #8
                Thanks for the info BrUnO XaVIeR.

                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/unrealengin...or_components/
                Unreal Possibilities
                Grid Creation Systems | Tower Defense Starter Kit | Wave Spawning System [50% OFF]

                Comment


                  #9
                  Originally posted by ClockworkOcean View Post
                  hanstar17 Can you be a bit more specific? Are you talking about placing actors from the level BP vs actor components within a BP?..
                  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.

                  Originally posted by Everynone View Post
                  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.
                  That's good to know, thanks It makes sense that you can't add component to an actor component.

                  Originally posted by Everynone View Post
                  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.

                  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:
                  https://answers.unrealengine.com/que...2544/view.html

                  Originally posted by GarnerP57 View Post
                  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.
                  Yeah. 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.

                  Originally posted by BrUnO XaVIeR View Post
                  Learn to use the UObject class.
                  It's much cheaper to use than both, can also tick if really needed.
                  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?



                  Comment


                    #10
                    Originally posted by Stormrage256 View Post
                    Thanks for the info BrUnO XaVIeR.
                    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/unrealengin...or_components/
                    Nice, thanks for the benchmark info. Good numbers to remember.

                    Comment


                      #11
                      Originally posted by hanstar17 View Post
                      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?
                      No, there are tools to make an UObject tick:

                      https://answers.unrealengine.com/que...with-mini.html
                      | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                      Comment


                        #12
                        Originally posted by BrUnO XaVIeR View Post

                        No, there are tools to make an UObject tick:

                        https://answers.unrealengine.com/que...with-mini.html
                        That is really good information... thank you so much BrUnO!

                        Comment


                          #13
                          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.

                          Comment

                          Working...
                          X