• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
duktapeman90

Classes as HashMap keys

5 posts in this topic

I'm working on a 2D game engine in Java that uses an entity-component based system, but I seem to have hit a snag designing my base entity class. I have it set up so that when you add a component, it adds it to a private HashMap in the entity class with it's class as it's key:

private final HashMap<Class<? extends Component>,Component> components;
public void addComponent(Component component){
     components.put(component.getClass(), component);
}

The problem arises when I call my getComponent() method:

public <T extends Component> T getComponent(Class<T> type){
     return (T) components.get(type);
}

This works most of the time, but not when I want to retrieve a Component that is also a subclass of a more general class. For example, I have two types of sprite classes: StaticSprite and AnimatedSprite, both extending from an abstract Sprite class. As far as game scripting is concerned, you'd never have to deal with the specific types of sprites, just the general Sprite class, so you'd create sprites like this: 

Sprite sprite = ResouceLoader.loadSprite("data/sprites/testanimatedsprite.xml"); //Returns an AnimatedSprite

The problem is, once I add this sprite to a component and try to retrieve it using getComponent() like this,

Sprite sameSprite = entity.getComponent(Sprite.class);
sameSprite.doSomething();

I end up getting a NullPointerException as it searches for a Sprite, but it's actually stored as an AnimatedSprite in the HashMap.

 

My question is, is there any way to do this so I can add/retrieve components from this HashMap by a it's superclass without having to explicitly declare the key in addComponent() or use the more specific class in getComponent()?

Edited by duktapeman90
0

Share this post


Link to post
Share on other sites

As it is, I doubt it.

 

You're using Component runtime classes as "categories" more or less. So you end up adding a component in the, say, StaticSprite category, but trying to retrieve it from the Sprite category.

 

You could create some concept of "slot". Maybe an index. Each Component type has an index, so when you add that Component to an Entity, it goes into that index. Sprite would define its own index, and subclasses of it would end up in that same slot.

 

Thing is that retrieval would depend on implicit knowledge. Ie, you would end up trying to retrieve a Sprite from an entity and casting it to StaticSprite or AnimatedSprite somewhere along the road, and there is no guarantee the Entity has one or the other. It isn't very safe.

 

Now, outside of the specific problem at hand, why do you have a StaticSprite and an AnimatedSprite? AFAIK the ECS approach is to separate behavior by separating the components, sprite is one behavior, animation is other, so you'd have Sprite component and Animation component.

 

Off the top of my head (thus possibly not adapted to your actual needs), Sprite would hold the current Sprite frame that has to be drawn, Animation would hold the data of the rest of the animation frames, AnimationSystem would grab your Animation and Sprite components, and update the Sprite with the next frame the Animation component holds.

2

Share this post


Link to post
Share on other sites
AFAIK the ECS approach is to separate behavior by separating the components, sprite is one behavior, animation is other, so you'd have Sprite component and Animation component.

I agree but if you want to have one object with 2 animated sprite, you will have to link each animation component with the sprite component.

 

--

 

Why do you use a hashmap ? One flat array with a linear search is enough, you will never have a lot of component in an object.

Edited by Alundra
0

Share this post


Link to post
Share on other sites


Now, outside of the specific problem at hand, why do you have a StaticSprite and an AnimatedSprite? AFAIK the ECS approach is to separate behavior by separating the components, sprite is one behavior, animation is other, so you'd have Sprite component and Animation component.

 

+1 for that!

 

The Object.getClass() method in Java doesn't behave as you want it to.  It will always return the deepest child class, even from a parent class. So that's not going to do what you want.  You can get around this by using the static class reference: MyObject.class, but that doesn't help you at run time.

 

Since you need to have objects that are composed of many different types you'll need to store them a different way.  Maybe just a hash of the different component types?

0

Share this post


Link to post
Share on other sites
The usual ways of solving this are one of:

a) Use "slots" as a prior poster said. Each Component subclass overrides a GetComponentClass that returns the appropriate class type that you want to register with.

b) Always register only the direct descendent class of Component. When you get a class descriptor, iterate up the base chain until your base is Component.

c) Register the component under all the Component descendent types. That is, if you register StaticSprite which derives from Sprite, you'd walk up the bases registering the component under each base until you hit Component. So the component would be registered both as StaticSprite and Sprite.

If you want to be using a real ECS architecture, though, then you have no need of this map in the first place. If you want to know if an entity has a certain component or retrieve that component type, you just query the entity's ID against the component's system, e.g. StaticSpriteComponent sprite = graphics.GetStaticSpriteComponentSystem().LookupComponent(my_entity_id);
1

Share this post


Link to post
Share on other sites

As others have implied, you really have two architectural options (ignoring various hacks to work around the current semantics):

  • Move to an outboard component system, where you query sprites from the external SpriteSystem, which understands the difference between static and dynamic sprites, or
  • Get rid of the multi-level inheritance, either by collapsing StaticSprite and AnimatedSprite into a single, optionally animated class, or by separating them entirely and make each inherit directly from Component.

Note that the two options are not mutually exclusive, and might well both be considered desirable. The path you are currently going down is likely to result in many of the same issues that an ECS is designed to avoid in the first place (i.e. deep inheritance, poor cache locality, etc).

0

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  
Followers 0