Sign in to follow this  
Kk1496

Accessing components

Recommended Posts

If I had a class A with child classes B and C (possible components) how would I add one of those components when I create an instance of A in main? Am I going about this the wrong way?

Share this post


Link to post
Share on other sites

Yes I think you may be thinking about this in the wrong way. The classic style of creating entities is using inheritance i.e. a ball class would derive from shape and the ball class would add any ball specific functionality.

 

In a Component System entities are defined by their components; so you would have an Entity Class and inside that entity class you would have a list of components. These components decide how the entity behaves i.e. a FlyingComponent that allows the Entity to fly. These components can then be added and taken away from the entity at run time if needed to provide different functionality. 

 

I hope I cleared that up a little, as a starting point you might be best having an Entity class that has methods for adding and removing components and then a base Component class that you can derive all your specific components from.  

Share this post


Link to post
Share on other sites

That helped to narrow down what I needed to do. However, I'm not sure how to call those different functions.

 

I made a GameObject class that has an ArrayList<Object>. Then, I created a Character class and a printCharacter() to print the variables.

 

In Order to print the variables of a test character, i created a private Character character; in the GameObject class and included that in the first possible constructor. well, I got it to print. But, i'm not sure how this works if I have multiple components attached.

 

I also tried a boolean approach where i changed the character variable to private boolean chaaracter;

This is with no arguments and a bunch of if statements as the only constructor.  

Share this post


Link to post
Share on other sites

I'm not entirely sure what part it is you are struggling with and it has been a while since I've worked with Java. 

 

If you have a look at this site it might give you an insight into the things that you may need to do. 

 

Sorry I couldn't be more help 

Share this post


Link to post
Share on other sites

It just happens that I have a fork of an ECS (Entity-Component-System) framework that might help you.

 

Artemis ECS framework site: http://gamadu.com/artemis/index.html There you'll find examples on how to use it.

 

And my fork, dustArtemis: https://bitbucket.org/dustContributor/dustartemis You can check out the sources and see if its useful for you as it is (clone it from mercurial or just download the zip with the sources), or just to get some inspiration from it :D

 

You can get more details of my fork in my journal (link on my signature) and on the repository's readme. Although Artemis's site is the one that has info on how to use it.

Share this post


Link to post
Share on other sites


That helped to narrow down what I needed to do. However, I'm not sure how to call those different functions.

In ECS the construction of entities is not a job of the entity itself. Instead, a client external to the entity is responsible for this.

 

Example 1: There is a file, maybe in XML, that lists each component by its class name, together with their parametrization. The entity management includes a factory for entities. When the factory is invoked with the name of the file, it looks up the description from its cache, instantiates the entity, iterates the list of component names, instantiates each component, parametrizes it, and add it to the list of components of the entity.

 

Example 2: There is a prototype of the entity. When the factory is asked for an instance, the prototype is cloned deeply. However, in this scenario the prototype has to be generated first. This could have been done by deserializing an object that was formerly serialized by the game editor (just to have mentioned another way than those in example 1).

 

Both examples above are based on an architecture where the entity holds a collection of its components.

 

There may be the need to have a base class Component, so that the member of GameObject be ArrayList<Component> instead of the very general Object. With this, you could have add methods common to all components, for example for debugging. So "printing the variables of a test character" is generalized to "print the variables of the entity" and works by iterating all its components and invoking their methods. This is the OOP way of making clients unaware of concrete sub-types, you know.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this