Jump to content

  • Log In with Google      Sign In   
  • Create Account


Entity System question


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
29 replies to this topic

#21 Mike2343   Members   -  Reputation: 468

Like
0Likes
Like

Posted 20 April 2008 - 10:06 AM

I know its a bit late and this likely won't be read by the OP... But still... Helpful I hope info/ideas/thoughts.

First thing:
Quote:
obj = ObjTemplateMgr::getInstance()->createObject( "HumanTemplate", ObjIdType("Human"));


Please let the singleton die the death it deserves! Read up on the forums of why its bad. Its so rarely a valid pattern you should forget it exists, you will KNOW when its actually valid. Also, a high level event system (not huge on the word manager) would solve this for you. As a side note, I call mine a Messaging System, but that's merely a name.

Example:

//EventSystem is a namespace, SendEvent is a "global" function.
EventSystem::SendEvent("CreateObject", "HumanTemplate", "Human");



In my case my "EventSystem" is actually a class that is templated internally so you can have all kinds of messages inside a single class. So your not as limited as say inheriting from an Event class and being stuck with just a few options (send functions).

Also, not sure which article linked in your first post you liked the most but your Entity class pretty much flies in the face of the T=Machine blog posts. Which is actually the system I like the most as it is also the most generic, but yet can be nicely optimized at the same time.

Then again going overly complex in a single person project is just time your spending not making a game...

Now to answer some of your other questions:

Quote:
Is a Trigger also an Entity?


I actually think no. I think something like TriggerEvent1234 should be an entity that is made up of a Trigger component, Position component and Script component. This allows a lot more complex game events to take place. Like below...

Quote:
When 2 Entities are less than 10 units away from each other, something should happen (like a message or anything). Where in the code do I check whether the two Position components are 10 units away from each other? In the PositionSubSystem?


One solution I came up with as I was reading the question (not done it but think I will try it soon...) is to have the object your concerned about being close to another use the Trigger component and set its cube to be 10 units in size. I can be flat for just hits on its same level, etc. This trigger then could call its own script component and run a custom script upon being triggered.

Quote:
Do I have for each single property of an Entity a component? Like Position, Target, Name, Team, ...


Yes, kinda the point. Entity should really be completely empty though you can come up with decent arguments for position but mines still separate and not a pain to access. Target and Team can be kept in AI honestly. I have a CommonInfoComponent that holds things like name and other generic info.

Quote:
But for a component like Position I don't see the point of a subsystem, since there's only data in the component.


Its data, but whats wrong with that? You can have things that set/check/get (I hate putting GetX, SetX as methods instead I like readable code like int X() or void Y(int).. etc. Cleaner when you type it out. But this can verify things and allow a lot of other components to get access to it. It can also be packaged into a message (event, though think even message sounds better) and fired off.

Quote:
Is Movement not a part of Physics?


Though commonly tied closely together, one doesn't exactly NEED the other and situations can be thought of when movement might not need physics.

Ok, 4 phone calls cause this 10 minute reply to turn into 2 hours. Kinda lost focus. Anyways this just my take on things. My system runs similar to what I described above. Maybe another bit of insight might help who knows.

Sponsor:

#22 aeroz   Members   -  Reputation: 171

Like
0Likes
Like

Posted 23 April 2008 - 06:31 AM

I was just checking my old links and noticed your post. Thanks for the reply.
I have already brought quite some structure to my code. I don't really know if it's good because I have never seen a professional way of doing this. Well, I hope that I can learn from my project.

I see that I have a lot of possibilities for the design; I just have to choose one. For me it seems that with a component system you can easily add new things to the game once you have the system. The game I'm developing is for a school project, so I have to stay alone with it. Do you really think a single person project is not worth it? I don't know if it's easy to find other people that are willing to work with you till the end, especially in a voluntary project.

So now, if somebody is still reading this post, it would be nice if one could answer some of my questions regarding what I have achieved till now.
Quote:
Please let the singleton die the death it deserves! Read up on the forums of why its bad.
I don't want to ask basic questions about Singletons, maybe you can give me a link: what can you do instead of them? I have made my EventManager, my RenderSystem, my InputSystem global Singletons, is that a bad thing? I don't know if passing around pointers to the systems would be better.
Quote:
//EventSystem is a namespace, SendEvent is a "global" function.
EventSystem::SendEvent("CreateObject", "HumanTemplate", "Human");
Has this not the same drawbacks as a global Singleton?
------------------------------------------------------------
Now my design:
Comments are welcome!

The game is separated into SubSystems: RenderSubSystem, InputSubSystem, PhysicsSubSystem, Camera, etc...
The SubSystems are updated every frame in the main loop:
void GameApp::UpdateGame( double deltaTimeSecs )
{
InputSubSystem::GetInstance()->Update();
PhysicsSubSystem::GetInstance()->Update( deltaTimeSecs);
RenderSubSystem::GetInstance()->RenderScene();
/* ... */
}


Then there are game objects that are Entities and consist of pure Components. To make a new Entity I use this code:
#include "Entity.h"
#include "components/CompVisualTexture.h"
#include "components/CompPhysics.h"
#include "components/CompPosition.h"
#include "components/CompPlayerController.h"
/* ... */
#include "World.h"

void Init( World* pWorld )
{
Entity* pEntity = new Entity("Entity0");

CompPosition* compPos = new CompPosition;
compPos->Position()->x = -10.0;
compPos->Position()->y = 10.0;
pEntity->SetComponent( compPos );

CompVisualTexture* compPolyTex = new CompVisualTexture;
pEntity->SetComponent( compPolyTex );

CompPhysics* compPhysics = new CompPhysics;
compPhysics->SetInvMass( 0.0 );
pEntity->SetComponent( compPhysics );

/* more components ... */

pWorld->AddEntity( pEntity );
}


In case of Events, you just call the EventManager. Example in the AddEntity() method:
void GameWorld::AddEntity( Entity* pEntity )
{
/* ... */
EventManager::GetInstance()->InvokeEvent( Event( NewEntity, pEntity ) );
/* Event( EventType type, void* data ) */
/* Each event type has a different type of data. Here it is a pointer to an Entity */
}


Or to listen for an Event you do this:
/* RenderSubSystem initialisation */
void RenderSubSystem::Init ( ... )
{
/* ... */
EventManager::GetInstance()->RegisterListener( NewEntity, RegisterVisualComp );
/* In the case of a NewEntity event, the EventManager calls RegisterVisualComp.
(RegisterVisualComp(Event* pEvent) is a callback function that checks whether the
new Entity has a renderable component and saves a pointer to it if yes.) */

}



I think that these are the most important thing to say. How can I implement the EventManager when it isn't good to have it a global Singleton?
Another thing I came across and don't know how to solve elegantly is whether input events are also normal Events that objects can listen to or whether the InputComponents can check the InputSubsystem for key states. (key states vs key events?)

Thank you for your help.
aeroz

[Edited by - aeroz on April 23, 2008 1:31:09 PM]

#23 newera   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 April 2008 - 09:01 AM

A possible solution:


class IListener
{
public:
IListener(EventMng* evnt) : mEventMng(evnt)
{};

void Register()
{ mEventMng->Register( this ); };

void UnRegister()
{ mEventMng->UnRegister( this ); };

virtual void HandleEvent( Event &evnt )=0;

private:
EventMng* mEventMng;
};

// each subsystem inherits from this,
// and register itself to the EventMng.
class ISystem : public IListener
{
public:
ISystem(EventMng* evntMng) : IListener(evntMng)
{};
virtual void Init(..)
{ Register(); }
virtual void Shutdown()
{ UnRegister(); };
virtual void Update(float deltaTime)=0;

};


...
EventMng eventMng;
ISystem* render = new RenderSystem( &eventMng );
render->Init(...);
ISystem* etc...


#24 stanirya   Members   -  Reputation: 771

Like
0Likes
Like

Posted 23 April 2008 - 10:58 AM

Quote:
Original post by aeroz
what can you do instead of them?

Passing around pointers, mostly.

Quote:

I have made my EventManager, my RenderSystem, my InputSystem global Singletons, is that a bad thing?

The prevalent opinion, and one I also subscribe to, is: yes, it is a bad thing.

Quote:

I don't know if passing around pointers to the systems would be better.

It would. Explicit dependencies are better than implicit ones, and you might avoid more dependencies by thinking of just where you need to pass those pointers. Also, making non-trivial stuff like Renderer a singleton has the potential for really chaotic execution paths since the Renderer.render() method could easily be called from anywhere in your program. Debugging things like that when you do not have certainty about what happens in what order is not a lot of fun.

Quote:

How can I implement the EventManager when it isn't good to have it a global Singleton?

My own, perhaps naive due to lack of experience, approach would be to create the EventManager object in a top-level object such as a Game object, and pass it down to any object that needs to send events. I say naive because I have not worked in a team on a large project, but I believe this is the standard approach taken by everyone who does not use singletons.

#25 Shakedown   Members   -  Reputation: 230

Like
0Likes
Like

Posted 23 April 2008 - 08:05 PM

Quote:

Another thing I came across and don't know how to solve elegantly is whether input events are also normal Events that objects can listen to or whether the InputComponents can check the InputSubsystem for key states. (key states vs key events?)


Have you already implemented input as events? I've seen inputs handled by a single class, and the class interprets the input and creates other Events accordingly. In that implementation, there are no input Events. It makes sense, too. This single class, usually called a controller, is provided to a game view. In that way, you can have 2 different controllers: one for the player (PlayerController), and one for the AI (AIController). Only the PlayerController would be a KeyListener and a MouseListener, so it can receive player input and create Events accordingly. The AIController would only create Events based on your artificial intelligence system (however that works).

Doing it this way, rather than creating a InputSubysytem, removes the necessity of input in your game completely. I can't quite remember what you're doing with your game, but you wouldn't need to worry about input if you wanted to simulate a game with only "bots."

Also, this may not apply too much to your game, but if you had an InputSubsystem, how would you handle other players over a network? If every entity in your game was controlled by a controller (PlayerController, AIController), you would give the networked players their own PlayerController, and then those controllers would read input local to the player, but send the events over the network to the server.

Quote:

I don't want to ask basic questions about Singletons, maybe you can give me a link: what can you do instead of them? I have made my EventManager, my RenderSystem, my InputSystem global Singletons, is that a bad thing? I don't know if passing around pointers to the systems would be better.


Quick and Dirty Answer:

Structure your game differently. If you structure your game to include a game-wide (application-wide) layer, then anything that needs to be accessed from anywhere in the game can be put in that layer. One broad way to structure your game may look like this:


Photobucket
Taken from Game Coding Complete, Second Edition by Mike McShaffry


More on this if you're interested.

#26 aeroz   Members   -  Reputation: 171

Like
0Likes
Like

Posted 24 April 2008 - 06:09 AM

Hi, thank you! [smile]

Well, then I'm going to turn my Singletons back to normal local classes...
Quote:
Original post by Shakedown

Have you already implemented input as events? I've seen inputs handled by a single class, and the class interprets the input and creates other Events accordingly. In that implementation, there are no input Events. It makes sense, too. This single class, usually called a controller, is provided to a game view.

In my game I have always thought of an event as something that has already happened and not as something that should still happen. So when something happens, other parts of the system can do something in response. I didn't know an event could also be something that has yet to be done. How can you distinguish both types?

Currently I have only events of the first type. When the player presses lets say the up arrow key an event of type InputEvent is send with parameter AccelerateKey. This event is sent by the InputSubsystem. And then the PlayerController class catches the event and calls the AddForce() method in the Physics component of the Player Entity.

I think you would do this differently, like that:

There is no InputSubSystem. The PlayerController checks for the state of a key an when the up arrow is pressed it sends an PlayerAccelerate event. Then the Physics component catches it and applies the force to the player.

Is that correct? But when another component sees the PlayerAccelerate event that doesn't mean the player has accelerated. Would the component that accelerates the player then need to send another event like PlayerAccelerated so that other components know the player has really accelerated?

In this version the PlayerController is like an InputSubSystem, isn't it? What is if I want to have multiple things controlled by the Keyboard? Wouldn't it be better to have an InputSubSystem that takes the input and the PlayerController that checks it? For example when two players play on the same keyboard I have two PlayerControllers but only one InputSubSystem.

Or have I misunderstood you?

#27 Shakedown   Members   -  Reputation: 230

Like
0Likes
Like

Posted 24 April 2008 - 07:36 AM

Quote:

In my game I have always thought of an event as something that has already happened and not as something that should still happen.


I imagine events as something that is happening right now. The player is accelerating right now so I should play the "player_accelerating" sound effect right now. The player has fired their weapons right now so I should play "player_fired" sound effect right now.

Quote:

So when something happens, other parts of the system can do something in response.


I think of it as all parts of the system reacting simultaneously. The player has pressed the accelerate key, so the physics system would apply a force and the sound system would play a sound effect. Note that if the player is holding down the accelerate key, 1 accelerate event is fired every iteration of your game loop, the systems don't understand the idea of a key being "held down;" each iteration of the game loop the key is either down or not down. That means that every iteration your physics system will apply the force and every iteration your sound system will play the sound. Perhaps you're imagining it as a start event (PlayerAccelerate) and a stop event (PlayerAccelerated), and any time in between, the physics system is applying a constant force and the sound system is looping the sound effect. That may be an interesting way of doing things, but it might turn out to make things more complicated.

If you imagine every iteration of your game loop as a snapshot of your game, independent from the previous iteration and the next iteration, then all that your systems should react to is what is happening right now in your game. If the player is no longer holding down the accelerate key, then there are no more PlayerAccelerate events being fired.

For example, in one iteration of your game loop the accelerate key is down (independent of the fact that it may have just been pressed, or it may have been held down for the entire game). So, your PlayerController does its job and reacts accordingly; it sends a PlayerAccelerate event. The physics system catches the event, applies the force to the player. The sound system catches the event and plays the sound. Let's say that's all that happened in that particular game loop iteration.

Next iteration. There are no keys down, so there are no events fired from the PlayerController; the player will not move and there will be no sound effect played. The enemies are still going about their business, flying around the screen and shooting at the player because they are being controlled by an AIController which isn't going to depend on the player. End of iteration.

Quote:

There is no InputSubSystem. The PlayerController checks for the state of a key an when the up arrow is pressed it sends an PlayerAccelerate event. Then the Physics component catches it and applies the force to the player.


Right. A PlayerAccelerate (which could even be PlayerMove) event would be dispatched to everything that cares. The physics component cares, so it receives the event and applies the appropriate force to the player.

Quote:

But when another component sees the PlayerAccelerate event that doesn't mean the player has accelerated. Would the component that accelerates the player then need to send another event like PlayerAccelerated so that other components know the player has really accelerated?


Explain what you would need a PlayerAccelerated event for. If I imagine what you're getting at, this might be the part where it gets tricky using components and events.

Quote:

In this version the PlayerController is like an InputSubSystem, isn't it?


Yep, except that it won't receive events from the real systems, and every entity in your game has its own controller.

Quote:

What is if I want to have multiple things controlled by the Keyboard? Wouldn't it be better to have an InputSubSystem that takes the input and the PlayerController that checks it? For example when two players play on the same keyboard I have two PlayerControllers but only one InputSubSystem.


You would have 2 different PlayerControllers - Player1Controller and Player2Controller. They would both be KeyListeners, except they would only react to the appropriate keys. For example, Player1Controller would look something like:


if('a' is pressed) send Player1 move left event
if('d' is pressed) send Player1 move right event
if('w' is pressed) send Player1 move up event
if('s' is pressed) send Player1 move down event



Player2Controller would look something like:


if('left arrow' is pressed) send Player2 move left event
if('right arrow' is pressed) send Player2 move right event
if('up arrow' is pressed) send Player2 move up event
if('down arrow' is pressed) send Player2 move down event



Then, in your game, you would give player 1's game entity the Player1Controller and player 2's game entity the Player2Controller, and then all of your enemies would receive different versions of an AIController.

Also, another note on Controllers. The Controllers control the particular entity that they're attached to (duh). This means that you will have a lot of different types of Controllers - Player1Controller, Player2Controller, AIGruntController, AIEliteController, AIHunterController, AIMarineSoldierController, AIMarineSniperController, AIWrathController, AIPelicanController, etc. Also, just to make clear on the PlayerControllers, you probably wouldn't have to differentiate between a Player1Controller and a Player2Controller if your players were not on the same input device (keyboard). If your game was on the Xbox 360, you would have 2 PlayerControllers, except one is listening to controller port 1 and controlling player 1's game entity, and the other is listening to controller port 2 and controlling player 2's game entity.

Hope that clears some confusion. Also, I might have mentioned this already, but there are many ways of achieving the same result. The way I discuss is only one of many, each with its own pros and cons.

#28 aeroz   Members   -  Reputation: 171

Like
0Likes
Like

Posted 24 April 2008 - 08:50 AM

I think I got it! [grin] Thx

But I still have an InputSubSystem that checks the key states every frame (SDL_GetKeyState).
The Controllers then ask the subsystem for the key state.
/* in your example: */
if('a' is pressed) send Player1 move left event
/* this would be in my game somthing like: */
if ( pInputSubSystem->KeyState( Player1UpKey ) )
pEventManager->InvokeEvent( Event(PlayerMoved, /* event data */ ) );
Quote:
I think of it as all parts of the system reacting simultaneously. The player has pressed the accelerate key, so the physics system would apply a force and the sound system would play a sound effect. Note that if the player is holding down the accelerate key, 1 accelerate event is fired every iteration of your game loop, the systems don't understand the idea of a key being "held down;" each iteration of the game loop the key is either down or not down. That means that every iteration your physics system will apply the force and every iteration your sound system will play the sound.
The sound system has still to know whether the key has just started being pressed or is already a second time down (to only play the sound at the beginning, otherwise there would be a lot of simultaneously playing sounds).

One last thing I wonder is what you mean with "game view" on your post before.
Thank you for your patience! [wink]
aeroz

#29 Ashkan   Members   -  Reputation: 450

Like
0Likes
Like

Posted 27 April 2008 - 11:29 AM

This is the second time that this thread is resurrected, so please accept my apologies for that.

Having read many topics on the subject, I have a relatively good understanding of a component-based entity system, but there is this one question that's been bugging ever since I started reading on the subject, mainly due to lack of comprehensive resources on the subject or me coming from a different background. Anyhow, my question is this: How does this component-based entity system interfaces with a scene graph?!

The way I have envisioned and designed my system is to have Entity objects as part of my scene graph, with entities (such as Camera or Light) inheriting from the parent Entity class, pretty much the old school way. With every iteration of the game loop the scene graph (which is merely a transformation hierarchy in my design) gets updated. Now lets say I switch to a component-based entity design instead. For some reason I have a hard time understanding how to incorporate this new design into my current system. Considering that entities do not have an Update() function in this new event-based system, how should I propagate transformations up the hierarchy?

So for instance, let's say we have a Camera entity that can also be attached to all other entities in a scene. This kind of behavior requires maintaining some form of hierarchical representation of the scene so such "relative relations" can be maintained and updated with every tick of the game loop.

1) First of all, how is this Camera (or Light, which has pretty much the same properties) going to be modeled in a component-based system. What are the components of such an entity to be exact? Where should the view and projection transformation of a Camera (or Light) be stored? A CameraComponent? Or can this be broken into smaller components?

2) Secondly, how should I incorporate such a component-based Camera in a scene graph? In my current design entities have Update() functions, but the prevailing opinion is that entities in a component-based system should not have data and methods. Some even go as far as representing an entity with a GUID alone. So, I'm pretty much in dark regarding how I should handle this situation. TBH, I'm so lost that I can't get my head around forming and asking a question about it... Just to give it a shot, should I send events of any form in this case?! Are such components not part of a scene graph at all? ...

As you see I'm pretty lost here, so any help (preferably accompanied by some examples) is hugely greatly appreciated.

Thanks in advance

#30 Kylotan   Moderators   -  Reputation: 3329

Like
0Likes
Like

Posted 27 April 2008 - 11:23 PM

Most of the components in question have nothing to do with the scene graph at all. If you have components that represent renderable objects or objects with a position in the game world, they may exist in that graph.




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