Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 2691 times)
0 Members and 1 Guest are viewing this topic.
Offline RylandAlmanza

Junior Duke


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 Duke


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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (87 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!