Hello, I’m doing a system where the playercharacter can detect what is in front of him and I’m doing this with a linetrace. I was wondering if It is a good habit to try do this with actor or scene component that I attach to my character in the characters constructor? The reason why I would do it this way is to give it a bit modularity, but I haven’t figured out yet what is the best way to give back the hitinfo to my character from the attached component? Do i pass the FHitResult variable as a reference from my character class to my component or is there a way to call a function from the character class from inside my custom actor/scene component? And which of the components is better choice to make this? Actor component cannot be attached to my character so that it would have it’s own transformation so it should be sent the new transform info every tick to be able to make the linetrace, but scene component could be attached for example the character camera and it would then only need to do the linetrace forward. Have I understood this correctly? And I have also wondered how performance costly it is to do a singlelinetrace every tick? I was thinking about doing this on a separate trace(physics?) layer and the trace would only be from 50 to 100 cm long, this shouldn’t be too heavy on a pc-game?
Sorry for the big amount of questions, hopefully someone has time to answer them all
Hi there, yay I am in on modularity! Doing exactly the same with the actor and scene components in my own physics library Seems to be more work beforehand, but will be far less work and pain later on when just plugging them in
Every way you describe is a solution that fits an usecase. You just need to know which usecase you have. So here my point of view to your questions:
1: ActorComponent or SceneComponent? You will be better off with ActorComponents instead of SceneComponents the most of the time. I try to avoid SceneComponents… Lets say… I’ve already experienced and reported some annoying bugs. Use SceneComponents only if you really need to work with offset positions like a custom SpringArm, which i did.
2: Blueprint communication: You want the owning BP only to initialize the component (Linetrace Distance, Channel, and everything else you want to configure). The component then provides information to the owner. And takes owner information (GetOwner->GetActorLocation, rotation,…) itself without bothering the owner.
For Colliders I pass them to the component in the constructor. And every tick I run overlap checks. Or I register the overlap events within the EventBeginPlay of the component.
For SpringArm position, you can just pass the SpringArmComponent at initialization and take location and rotation from there instead from the owner.
3: Which information gets provided by your component is your choice. It really just depends on what the owning PB needs. It will change/evolve over time anyway. You probably even want to prepare the data with additional calculation based on the LineTrace or even let the component react to things on its own without bothering the owner at all.
-
If you only need the line trace on events like user interaction, then you would be better off with a function, that executes the LineTrace and returns the value you need.
-
If you need the character to be informed about any collision, then you will be better off with events. Just think about the overlap events of the colliders/triggers/… You want to use events if you do not need to pull data every Tick, because it does not change that often. But usually it does not matter if you pull data if performance does not pile up with hundreds of actors.
-
And after all, when you need to run the LineTrace every Tick: You probably want to do some internal stuff and then store the results into variables. The Owner can fetch them if required even in multiple places. Your HUD can access them as well. Use functions if you need do pull special calculations based on the data every now and again. And you can still do some special events where it does not make sense to check for them every frame.
4: Do not worry about line trace performance. In my tech project i heavily use my plug&config&play ActorComponents and SceneComponents where at least 2 of them handle collisions with sphere collision and Linetraces and there are some components more for shooting/targeting LineTraces and so on.
([TECH DEMO] Watch things fly - Custom Physics - Game Development - Unreal Engine Forums)
It really depends on how many actors you really need… I am not even sure if my thing has good performance, since i have no reason for 200 pawns. If you go multiplayer you hit other limits far before anyway. You still can refactor and optimize things due to your modularity.
hth Marooney
Thanks Marooney, really great answer! One clarification needed though, you mentioned fetching info/data from the component in the owner. This is done by simply using the pointer to the component created in my actor that uses the component?
Yep, within your Component the GetOwner Node/Pointer returns you the Actor.
If you need more Information from the Actor you can either set up an interface on the Actor that the Component can access or as i said before just pass the required references (other components, colliders,…) to the component on initialization.
Oh, sorry just realized you mean the opposite way ^^ Yep, the ActorComponents are listed as variables in the Actor, just use them to access your component-functions/events or component-variables