• Advertisement
Sign in to follow this  

Accessing the transform component in my GameObject

This topic is 696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Another architecture problem here.

I am wondering if I store all components, such as renderer, mover and transform components in a vector in game objects.

When I query the game object for the position and rotation, I need to iterate over the vector to locate

the transform component, but it is not that efficient to do that frame after frame.

But it is not logical to move the transform component to a standalone pointer.

What is the general approach to tackle this problem?

I want to have fast access to the transform component without iterating the container.

 

Update:

And one more question.

Should I store the object's mesh, mass, speed, destination, animation controllers in the game object?

Or I store them in a separate component as well? But they don't contain functionalities, looks like they should be properties of the class, right?

Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement

In my previous approach I did actually provide a pointer in the GameObject class and had a template specialization just for the Transform, since it was the component that was accessed the most. However, these were allocated on the heap and that's the only scenario that would work in. It's hard to get it any faster than that.

 

If you aren't currently doing that, you could switch to allocating it manually and storing the pointers in that vector instead. That does incur a hit on data locality though. Another option is to omit it as being a separate component and store the position and rotation directly in a GameObject, though that's not too common. One last option is to use a hashmap (e.g. unordered_map) instead and store the transforms by their GameObject index. 

 

In case you actually even know the index of the Transform in the vector in some way, you don't even need a hashmap, but can request the index instead and use that to access the Transform in the vector.

 

As for your other question: no, I would not store it in a GameObject at all. Does every GameObject have a mesh? Is mass relevant to every GameObject? Speed? Perhaps it doesn't incur any direct processing overhead, but is sure to kill some memory and data locality along with it. That's just my opinion however. If it makes it more complex than it needs to be by splitting it up in the components, then do so, if it works it works.

 

I'm somewhat assuming here that you are aiming towards ECS, seeing as you have vectors of components. In that case, you actually want the components to be data so that the systems can limit themselves to retrieving the data they require.

 

If you do have the logic in your components, I think your problem is most likely that you are looking to access the data from different behaviours. E.g. the physics component wants to access the speed to update the position, but the input component wants to access the speed to change it. Is this the problem you are trying to solve, regarding your second question?

Share this post


Link to post
Share on other sites

"I want to have fast access to the transform component without iterating the container."

 

My answer:
Store a pointer to the transform component. 

 

"if I store all components, such as renderer, mover and transform components in a vector in game objects."
 

My answer:

I suggest NOT to have a component vector per gameObject.
Instead, use a global vector for each type of component. And let the gameObject have pointers to the components it owns (or use a O(1) lookup).
This has several advantages:

1. you can layout all component of the same type in continuous memory
2. you can change the execution order of components (e.g. update all componentX before componentY)
3. you can easily query for components of a certain type

4. you don't iterate/update gameObjects, but vectors of components
5. since not every component type will be updateable (some might be pure data), you can skip updating those vectors


 

"Should I store the object's mesh, mass, speed, destination, animation controllers in the game object?"

My answer:
Your gameObject should carry as little public information as possible. Name/Id/Enabled should be enough.
Everything else should go to components. If you want to do a proper ECS, treat your gameObjects as if they were pure integers.

 

cheers

Edited by chlerub

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement