Entity/Component System - Getting Entities to interact with eachother

I’m using the Entity Component system described here. It works perfectly, but I have one question. How do I get components to be able to interact with other entities?

For example: I have a battle component that I add to an entity. I want the battle component to know if the parent entity is touching another entity, and then call the hurt() method on the entity that it is touching.

This sounds easy enough, but I’m just wondering how I get components to know where other entities are. One thing I thought of is to have an array of entities, and pass it into the components constructor. However, that would just create copies of the entities for the component, and then when the component called the hurt() method on one of them, it would call it on the copy entity, not the real entity, correct? (I’m new to java, and I’m still not sure whether it passes variables by reference or by value.)

How do I get a component to interact with the actual entities, and not copies of them?

Java passes everything by value, without exception. However, Objects are referred to with references, and those references are passed around by value. It’s exactly like passing pointers around in C++, except you can’t explicitly dereference them or do pointer arithmetic on them. Java will never copy an object behind your back, so if you pass an object like an Array into a method, it will always refer to the same array.

As for how you get entities to cooperate, that’s generally handled by the subsytem concerned with those entities either doing a query of some sort that retrieves the entities involved, or being handed a list of entities by some other subsystem that already contains all the participants. Components shouldn’t be dealing with entities, they should only be passed the relevant items involved, which is usually other components of the same type.

For example, the Spatial component of an entity may have a “collides” method that should receive another Spatial, not the entity that contains the Spatial. It’s up to the Collision subsystem to detect collisions between the Spatials of nearby entities, and call the “damage” methods of the entities that do collide (or hand off the colliding entities to another subsystem that handles it).

Also consider that standard OO composition mechanisms may be more appropriate than entity systems for your particular program. In fact I’d go so far as to say Entity Systems are highly overrated things that no one should be using unless they have specific knowledge of the exact benefits they gain from using them over a traditional OO approach.

Wow. That was an amazing post. You answered all my questions in one post, and then answered the questions I would have had if you didn’t answer them before I could ask them! (Wait, what?) Thanks a lot! I’m used to getting several replies and still not getting things, but that post was really clear. It could be my ADD pills, but I’m pretty sure it was your post. :slight_smile: