Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Entity/Component System - Getting Entities to interact with eachother  (Read 5399 times)
0 Members and 1 Guest are viewing this topic.
Offline RylandAlmanza

Junior Devvie

Medals: 3

« Posted 2012-02-09 21:09:51 »

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?
Offline sproingie

JGO Kernel

Medals: 202

« Reply #1 - Posted 2012-02-09 21:50:27 »

(I'm new to java, and I'm still not sure whether it passes variables by reference or by value.)

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.
Offline RylandAlmanza

Junior Devvie

Medals: 3

« Reply #2 - Posted 2012-02-09 22:01:10 »

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. Smiley
Pages: [1]
  ignore  |  Print  

Riven (846 views)
2019-09-04 15:33:17

hadezbladez (5790 views)
2018-11-16 13:46:03

hadezbladez (2602 views)
2018-11-16 13:41:33

hadezbladez (6206 views)
2018-11-16 13:35:35

hadezbladez (1498 views)
2018-11-16 13:32:03

EgonOlsen (4733 views)
2018-06-10 19:43:48

EgonOlsen (5791 views)
2018-06-10 19:43:44

EgonOlsen (3275 views)
2018-06-10 19:43:20

DesertCoockie (4174 views)
2018-05-13 18:23:11

nelsongames (5500 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!