# Entity System question

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, thank you! [smile]

Well, then I'm going to turn my Singletons back to normal local classes...
Quote:
 Original post by ShakedownHave 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?

##### Share on other sites
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 eventif('d' is pressed) send Player1 move right eventif('w' is pressed) send Player1 move up eventif('s' is pressed) send Player1 move down event

Player2Controller would look something like:

if('left arrow' is pressed)  send Player2 move left eventif('right arrow' is pressed) send Player2 move right eventif('up arrow' is pressed)    send Player2 move up eventif('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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.