Using actor/scene component and the cost of linetrace
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 :)
asked Jul 23 '16 at 01:06 PM in C++ Programming
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.
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. (https://forums.unrealengine.com/showthread.php?113323-TECH-DEMO-Watch-things-fly-Custom-Physics&highlight=watch+things+fly) 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
Follow this question
Once you sign in you will be able to subscribe for any updates here