Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Accessing components


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 KailiKame   Members   -  Reputation: 316

Like
0Likes
Like

Posted 25 June 2014 - 10:13 PM

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?

Sponsor:

#2 JordanBonser   Members   -  Reputation: 617

Like
2Likes
Like

Posted 26 June 2014 - 02:26 AM

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.  



#3 KailiKame   Members   -  Reputation: 316

Like
0Likes
Like

Posted 26 June 2014 - 02:01 PM

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.  



#4 JordanBonser   Members   -  Reputation: 617

Like
0Likes
Like

Posted 27 June 2014 - 02:30 AM

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 



#5 TheChubu   Crossbones+   -  Reputation: 6809

Like
0Likes
Like

Posted 27 June 2014 - 03:52 AM

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.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#6 haegarr   Crossbones+   -  Reputation: 6067

Like
0Likes
Like

Posted 27 June 2014 - 04:40 AM


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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS