# How to get mass of all attached actors

I creating object from many actors for example, A,B,C,… means actors, and → means attached to. So I have:
A->B->C->D… ->Z and more attached actors, like a chain…
It is possible to get mass of all that actors or number of chained actors?

This is video showing my “actors chain”:

And the problem is how to calculate mass for a whole object?

Because when I call CalculateMass() on UPrimitiveComponent it allwayrs return 37 no matter how many blocks attached.

The mass of an actor depends on the masses of it’s `UPrimitiveComponent`s. So to calculate the Mass of an actor which could feature a set of components like meshes or whatever you have to get all `UPrimitiveComponent`s:

``````float ActorsMass = 0;
TArray<UPrimitiveComponent*> comps;
GetComponents(comps);
for (auto Iter = comps.CreateConstIterator(); Iter; ++Iter)
{
UPrimitiveComponent* comp = Cast<UPrimitiveComponent>(*Iter)
if (comp)
{
ActorsMass += comp->GetMass();
}
}
``````

Now if you go through your actor chain just compute the mass of all actors. You can also get the mass of a given bone by using `UPrimitiveComponent::CalculateMass` instead.

Cheers,
Moss

Each block is Acator deriver class (AVoxel) and it contais a UStaticMeshComponent as root component.
So each containt UPrimitiveComponent.

Is each a UPrimitiveComponent or are all blocks one big UPrimitiveComponent?

Yes but I dont know how many actors are connected, for example:
I attach actor B to A, so A have 1 child and B one paren, then I attach C do B, so B have 1 child and 1 parent, and C have 1 parent, in that case in all Actors CalculateMass() will return 37. But I dont know how to find number of connected actors ;]

So if you call CalculateMass() each time for each block an accumulate it you should get the mass you want I think. The value of 37 is the mass in KG of each component so if you have X you should get X*37.

I will test it tomorrow and give you answer if managed to make it

Ah ok, to get the connected actor to a given actor. If you attache Actors using their components and a socket using AttachTo the new parent will hold a list of children `AttachChildren`, you then only have to go from the root actor and iterate all its `AttachChildren` and compute their masses. You can get the root using `GetAttachmentRoot` to get the root component of the attachment or `GetAttachmentRootActor` which gives the root. This method is working when using `AttachRootComponentToActor` which attaches components of different `AActor`s.

If that is not working the AActor class has a list of `Children` which are the attached actors, if you are `SetOwner` of course.

Cool ^^ I wish you luck! I use ‘AttachRootComponentToActor’ to attach actors to sockets, that way I can just iterate the `AttachChildren` list recursively.

Propably I found solution but not implement it yet:
Can I use float pointer? For example float* TotalMass and share between all actors?

This is how look hierarchy of my actors.
When I attach actors to one branch it can be difficult to attach actors in another branch. So because of that I think about pointers. What you think about it?

http://i.imgur.com/pD3Mui7.png

I would rather hold a pointer to the root actor which holds the total-mass value, this way you can add more root-related data without modifying much code.

Yes but it is possible to detach root from the rest, so the rest will lost all data. I need to share pointer between all actors. The solution for more data is datastructure shared between actors.

For that should I use normal c++ pointer or UE4 shared pointer?

I think you can catch when a object is attached, detached, this way you have to maintain the hierarchy yourself. You can use a shared pointer or just a plain C++ one, but it totally depends on your implementation.

I would recommend holding a reference to the root and handling attaching/detaching.