Jump to content
  • Advertisement
Sign in to follow this  
JoshuaBaker

Unity C# Component Entities and References

This topic is 2353 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

I have a issue that I am having trouble finding a solution for.
Currently I have an Entity/Component architecture which is quite similar to Unity. At a basic level an entity looks like this.


public class Entity {

private Dictionary<Type,Component>() Components;

public void AddComponent<T>() where T : Component;
public void RemoveComponent<T>() where T : Component;
public T GetComponent<T>() where T : Component;

}

There is alot more to it, but this is enough to understand.

My issue involves references to Component and Entities. Suppose I have a ScriptComponent (which right now is just a c# class) and it references a seperate entity...


Entity entityA = EntityManager.CreateEntity("EntityA");
Entity entityB = EntityManager.CreateEntity("EntityB");
BScript script = entityB.AddComponent<BScript>();



public class BScript : ScriptComponent {

Entity entityA;

public override Start() {
EntityA = EntityManager.GetEntity("EntityA");
}

public override Update() {

}
}


I noticed that EntityA in ScriptB will be kept alive for the lifetime of the script. I felt like this might become a problem, but I can't decide for certain if it will be a big issue. In Unity they do some magic to set the references to null, but i don't think i can do it with native c#. I'm just looking for more eyes on this if someone has tried a similar system before and if it became an issue.Thanks!

Share this post


Link to post
Share on other sites
Advertisement
this isn't answering your question but,
why are you using dictionary<type, component> ? what if you need to have multiple components of a single type? such as multiple draw components, or multiple attached entity components?
I use dictionary<byte,Component>
public byte addComponent(Component component)
{
if (Components.Values.Count == 256) throw new ArgumentException("No more components may be added to this entity");
while (Components.ContainsKey(m_iComponent))
{
m_iComponent++;
if (m_iComponent >= 255) m_iComponent = 0;
}
Components.Add(m_iComponent, component);
return m_iComponent;
}

the byte being returned being used to easily get the Component. Though if the component is added in the entities constructor I'll usually hold a reference to it in the Entity for convenience.

on a more related note, why would it be a problem? if the script is using the entity then shouldn't you want the entity alive while its running? otherwise you'd get null reference exceptions.

Share this post


Link to post
Share on other sites
If the OP wanted to have multiple components of a single type in this architecture they could just create a container type for that given type and store that.

In regards to the OP, if you'd like the ability to control the lifetime of your objects even if another component is holding on to it, you could have your entity manager return handles to objects instead of direct references.

Share this post


Link to post
Share on other sites
Why not just pass in dependencies to components?

Entity e;

Transform trans(...);
RigidBody rbA( trans, ... );
RigidBody rbB( trans, ... );
Joint joint( rbA, rbB, ... );

e.AddComponent(trans);
e.AddComponent(rbA);
e.AddComponent(rbB);
e.AddComponent(joint);

That's what I do in my entity/component system (in addition to allowing multiple components of the same type), and it works quite nicely.

I talk about my entity architecture a bit in the following journal entries (it's in C++. A C# implementation would probably be a bit simpler):

http://www.gamedev.net/blog/1390/entry-2254772-tank-physics-and-other-progress/
http://www.gamedev.net/blog/1390/entry-2254064-c-port-of-artemis-entity-component-system-framework-in-progress/

Share this post


Link to post
Share on other sites

this isn't answering your question but,
why are you using dictionary<type, component> ? what if you need to have multiple components of a single type? such as multiple draw components, or multiple attached entity components?
I use dictionary<byte,Component>
public byte addComponent(Component component)
{
if (Components.Values.Count == 256) throw new ArgumentException("No more components may be added to this entity");
while (Components.ContainsKey(m_iComponent))
{
m_iComponent++;
if (m_iComponent >= 255) m_iComponent = 0;
}
Components.Add(m_iComponent, component);
return m_iComponent;
}

the byte being returned being used to easily get the Component. Though if the component is added in the entities constructor I'll usually hold a reference to it in the Entity for convenience.

on a more related note, why would it be a problem? if the script is using the entity then shouldn't you want the entity alive while its running? otherwise you'd get null reference exceptions.


Your approach is interesting, perhaps ill look into it when i need two of a component, and your idea about the references is what i was thinking, if its an entity its a minimal amount of memory hanging around, but it felt like it might cause me issues later on. Just being aware of it, and expecting my scripts to handle the situation appropriately is the best solution in this case, i just really like solutions that force proper behavior.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!