• 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
Hauck

Better way to make entities and map comunicate in a Hierarchy Component System

6 posts in this topic

@Edit: I can't edit the title, I mean Hierarchy Entity System, sorry.

 

I've been working on a simple game with C++/SDL using hierarchy entities and I've been running into  some problems to make the entities deal with the map in a intelligible way. I'm using the common way of states, currently having a Start Menu state and a Main Game state.

 

Short story: I want my entities to know about each other current states (though they should be ideally independent), what would be the most efficient way to do that, and what would be the most readable/organized way to do that?
 
 
Long Story:
I had a class called Map, which holds a bi-dimensional array, each position representing a "square" of the map. Also, I had my entities Enemy and my entity Player. My first problem came when I needed to make entities communicate to the map, like "I want to walk to square [x][y], can I do it?" 
The first solution I thought I was declaring the map Static, this way everyone could access it in their classes. But I personally dislike using static objects, and I heard it's not a good idea to do it on games, so I tried thinking in something else.
 
Then, after thinking one more time, I thought of putting the Player and the Enemies INSIDE the map class. It sounded a little weird at first, but considering I had this only one map for the entire game, it seemed plausible. This way, to make entities communicate with the map, I had something like this:
Map::Update()
{
	Player->Update();
	(for each Enemy) Enemy->Update();
		
	int MoveParam = IsMoveValid(Player->MovingDirection, Player->X, PlayerY);
	Player->Move(MoveParam);
	(for each Enemy) MoveParam = IsMoveValid(Enemy->MovingDirection, Enemy->X, Enemy->Y);
	Enemy->Move(MoveParam);
}
Basically, it works like that: The player and enemies updates will set the directions they WANT to move.
Then IsMoveValid will get that direction they want to go, their currently X and Y position, will check if the X and Y position they want to move is ok, and will return a parameter for them (like "Ok, you can move", "You can't move this way", "You can go down but you have to move a pixel to the right first", something like that). 
Based on this parameters, the Move function of each entity class will decide if they are moving or not. This implementation sounded really okay at first, but them suddenly I realized that I moved EVERYTHING to the map class, the Main Game state was basically updating and rendering the map object.
 
Then, considering the map class held only a bi-dimensional array of positions, I moved everything back to the state class and eliminated the map class. This is the version I currently have in here. It's working fine, but I thought on looking for more options to deal with this.
 
I saw some other feasible ways to implement it, like making your entities hold a pointer to the map and tho each others, or make the class that hold them implement a message system so they can communicate. I'm also reading a lot about Entity-component-system and it may (or may not?) offer some solutions for this question, but considering that there are so many games developed under Hierarchy Entities, I guess a lot of ways should have been thought already. 
 
What's you guys thoughts on this? What is the way you usually implement your entities communication?
 
Edited by Hauck
0

Share this post


Link to post
Share on other sites

I think the entities having a reference to the map, and vice versa is the way to go. when the entity needs to get a path, it can query the map, which can use a* or other pathfinding to return the path to the entity. If your entity needs to query for nearest enemy or what not, it can query the map, which knows all entities already. In an ECS system, I would have a pathfinding system to handle the logic, which would have a reference to the map, and use the entities position component to get starting information for the graph search.

1

Share this post


Link to post
Share on other sites

You could pass the Map object by reference:

 player->update(theMap);
 (for each Enemy) enemy->update(theMap);

There's really nothing wrong with passing objects through function calls to the objects that depend on them.

1

Share this post


Link to post
Share on other sites

You could pass the Map object by reference:

 player->update(theMap);
 (for each Enemy) enemy->update(theMap);

There's really nothing wrong with passing objects through function calls to the objects that depend on them.

 

I thought about doing something like that, but I don't know, it just don't feels right to be passing everything through parameters. Perhaps I'm just too paranoid. I also had some problems with circular dependency when I first tried doing that, but this is probably some mess I did with the code.

 

 

I think the entities having a reference to the map, and vice versa is the way to go. when the entity needs to get a path, it can query the map, which can use a* or other pathfinding to return the path to the entity. If your entity needs to query for nearest enemy or what not, it can query the map, which knows all entities already. In an ECS system, I would have a pathfinding system to handle the logic, which would have a reference to the map, and use the entities position component to get starting information for the graph search.

 

I was thinking of something like that, like the entities storing a pointer for the current map and vice versa, though I feel like I'm going to have some headache to implement that at first. I'll give some more thought and research on the matter before what I'm really going to do.

 

Thank you guys!

0

Share this post


Link to post
Share on other sites

 

You could pass the Map object by reference:

 player->update(theMap);
 (for each Enemy) enemy->update(theMap);

There's really nothing wrong with passing objects through function calls to the objects that depend on them.

 

I thought about doing something like that, but I don't know, it just don't feels right to be passing everything through parameters. Perhaps I'm just too paranoid. I also had some problems with circular dependency when I first tried doing that, but this is probably some mess I did with the code.

 

 

I think the entities having a reference to the map, and vice versa is the way to go. when the entity needs to get a path, it can query the map, which can use a* or other pathfinding to return the path to the entity. If your entity needs to query for nearest enemy or what not, it can query the map, which knows all entities already. In an ECS system, I would have a pathfinding system to handle the logic, which would have a reference to the map, and use the entities position component to get starting information for the graph search.

 

I was thinking of something like that, like the entities storing a pointer for the current map and vice versa, though I feel like I'm going to have some headache to implement that at first. I'll give some more thought and research on the matter before what I'm really going to do.

 

Thank you guys!

 

You may find this a good read

1

Share this post


Link to post
Share on other sites

You could pass the Map object by reference:

 player->update(theMap);
 (for each Enemy) enemy->update(theMap);
There's really nothing wrong with passing objects through function calls to the objects that depend on them.

 
I thought about doing something like that, but I don't know, it just don't feels right to be passing everything through parameters. Perhaps I'm just too paranoid. I also had some problems with circular dependency when I first tried doing that, but this is probably some mess I did with the code.

Well, you're not passing everything, are you? Just the map. If updating your entity requires the map, pass it in. If at some point you find you're passing 10 arguments into your method, perhaps that method is doing too much work, and should be split up into more functions that do specific work and each takes 1-2 arguments. Also, this actually solves circular dependencies, as in this case, the Entity doesn't need to be created with a Map reference.
 

I think the entities having a reference to the map, and vice versa is the way to go. when the entity needs to get a path, it can query the map, which can use a* or other pathfinding to return the path to the entity. If your entity needs to query for nearest enemy or what not, it can query the map, which knows all entities already. In an ECS system, I would have a pathfinding system to handle the logic, which would have a reference to the map, and use the entities position component to get starting information for the graph search.

 
I was thinking of something like that, like the entities storing a pointer for the current map and vice versa, though I feel like I'm going to have some headache to implement that at first. I'll give some more thought and research on the matter before what I'm really going to do.
 
Thank you guys!

If you don't store the Map into entities, but pass it into an update() method or move() method, then you have no headaches on rewriting anything, and the entity updates based on the map you give it. If the entity changes the map it's in, it doesn't need to know that at all, you just pass in a different map.

My advice would be to do what's easiest (which is argument passing), and refactor later if it proves (through usage) to be ineffective or hard to use.
2

Share this post


Link to post
Share on other sites

 

The first solution I thought I was declaring the map Static, this way everyone could access it in their classes. But I personally dislike using static objects, and I heard it's not a good idea to do it on games,

 

 

why not using statics in games? can you elaborate?

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