Page 1 of 2

Component based behaviorless entity systems and scripting

Posted: Sun Sep 11, 2011 4:19 am
by midix
Hi all game devs!

I am planning to dive into game deving and try some interesting approaches. I can afford experimenting because I do not plan to release a full-blown game anyway, I just want to keep my brain busy between some boring s&%t I program for living.

So I found some interesting articles and forum posts which basically convinced me that component based entity system is something awesome if I implement my entities as simple property bags without behavior at all. Then I can avoid inter-component dependencies, hierarchies and what not else. As the entities are just a bunch of component properties which get added to them as needed, I can easily store the state of my entities in files, SQL, send over network etc.

In such a hypothetical system the entire game behavior and basic systems are implemented in some kind of singleton subsystems. Those subsystems use some mechanism to track when there is any work to do on some particular component. MovementSubsystem, PathfindingSubsystem, AISubsystem and so on. Subsystems care only about entities which have the corresponding components. RenderingSubsystem ignores all entities, which have no mesh, nor sprite, nor light components. MovementSubsystem cares only about the Position component. I guess, you got the idea.

Everything seems pretty clear to me (although I am sure I'll have gazillion questions while implementing this in C++), except scripting.

Let's say, I want my entire game logic to be scripted (with Lua or C#/Mono, I guess). The game engine itself should not know anything about particular game, the engine just can supply some optimized algorithms, like pathfinding, some predefined generic AI algorithms etc.

Now how do I implement the scripting part?

Let's say, I have some ScriptingSubsystem which is a host for all game logic subsystems. To access entire engine from scripts, the ScriptingSubsystem should be as a gateway to all other subsystems. But there should be only one way: game engine itself should never know anything about any particular script which runs inside the ScriptingSubsystem.

Now in a scripting language I can implement game specific subsystems. For example,I have some imaginary CriminalitySubsystem and game entities have a Criminality component which is just a single integer or float property.

When my game initializes, I call ScriptingSubsystem and say: hey, call all the scripted subsystems and pass them all the in-game entities so each subsystem can register the needed components.
CriminalitySubsystem (which is a Lua or C# script) loops through all passed entities and looks: yes, this one has Criminality, I will add it to my processing list.
Or maybe I could use another approach: I can pass an entity without Criminality component, and the script will add this component for me and register the entity.

Now comes the tricky part. The CriminalitySubsystem should somehow detect some events when the entity does something criminal. For example, each time the user (or NPC) triggers Hit action against some other entity, the CriminalitySubsystem increases Criminality value. And when Criminality value reaches some threshold, the CriminalitySubsystem calls PoliceSubsystem (which also is a script) and tells it: "Register this nasty entity as a criminal and start chasing it all over the town!"

So it seems I'll need also some GameActionsSubsystem which can trigger various game-specific high-level actions, like "hit someone", "escape prison" and so on. Obviously, CriminalitySubsystem depends on this GameActionsSubsystem because CriminalitySubsystem needs to know if in the current game loop iteration there was a "hit someone" action. The GameActionsSubsystem should know nothing about CriminalitySubsystem (to avoid recursive dependencies).
Now there is a problem: how should CriminalitySubsystem find out when the Hit event occured? I have two solutions on my mind:
1) some event-listener or pub/sub system with a MessageManager where one subsystem can publish various messages and other subsystems can subscribe to those events.
2) dirtyness flag for each component. At the beginning of the current game loop iteration I set all entities and all components as dirty=false. If some component changes during the current iteration, it gets dirty=true, so the next subsystem in the processing chain can inspect each component and see if it is dirty and needs something done about it.

How those subsystems know, in what order to process the entities? If I use some pub-sub messaging, I guess, I could implement that in the messaging system itself. Let's say, CriminalitySubsystem needs the GameActionsSubsystem to be present and registered before, so I can do the following pseudocode:

Code: Select all

MessageManager::Register(CriminalitySubsystem::GetSingleton(), 
GameActionsSubsystem::HIT, 
MessageManager::POSITION_AFTER,
 CriminalitySubsystem::TYPE_ID);
I guess, it is pretty clear: I ask MessageManager to register the CriminalitySubsystem to listen to HIT messages and insert me after CriminalitySubsystem. If there is no CriminalitySubsystem registered yet, I'll throw some exception "Yo, dumbass programmer, you have a broken dependency chain!"

But if I use dirtyness, there should be a similar chain anyway, so I process Criminality only after the GameActionSubsystem has done its work.

So, is there anyone here, who have experimented which such a component (property bag) based entity system with singleton behavior subsystems? Did you try implement also the scripting part and how does it all tie together, and how did you solve the chaining of dependent subsystems?

Thanks for any ideas.

Re: Component based behaviorless entity systems and scriptin

Posted: Sun Sep 11, 2011 9:05 am
by k1net1k
im not sure if this is a question, a statement, a development log, or an autobiography.
but it sure as hell is interesting to read.

Re: Component based behaviorless entity systems and scriptin

Posted: Sun Sep 11, 2011 12:04 pm
by midix
:lol: good that it was entertaining at least. Actually it is a question, but the question comes at the very end (if you had a patience to read that far :mrgreen: ). I could put the question at the very beginning, but then you would have no idea what I am talking about when I say "Component based behaviorless entity" :).

Anyway, if someone is interested to read more educational material about this stuff from native English writers (my English sometimes is weird, I know), here you go:

http://t-machine.org/index.php/2007/09/ ... nt-part-1/

http://www.gamedev.net/topic/463508-out ... try4089913
(the post made by Lord_Evil received pretty good responses, and I liked the idea - almost the same as in T-machines blog - entities are NOT behavior).

The problem is, I cannot find people who have really tried this approach and know about possible problems which may arise when trying to implement scripting into such a system. I hoped, someone here might have gone through such ideas and has something to say...

Re: Component based behaviorless entity systems and scriptin

Posted: Sun Sep 11, 2011 12:23 pm
by k1net1k
Most people who have english as their second language probably speak it better than english people

no but seriously, i was learning while reading your post, but i dont know the answer sorry

Re: Component based behaviorless entity systems and scriptin

Posted: Sun Sep 11, 2011 11:00 pm
by Ginto8
I think I figured out the question: How can you have a weak engine with strong scripting? The answer is: you usually don't want that. Scripting is usually used to simply extend certain behaviors that the engine itself doesn't have by default. For example, your engine should handle NPCs. You should be able to add them, remove them, move them, start a conversation, etc. However, if you want special behavior outside the norm, you often would like to be able to change and test it easily without recompiling: hence, scripts. You should have a strong engine, with scripts extending it, instead of a weak engine, with all the power held in the scripts. If all the power is in the scripts, why have the engine there as anything more than an api?

Re: Component based behaviorless entity systems and scriptin

Posted: Mon Sep 12, 2011 12:22 pm
by midix
That's a good point, Ginto8, I guess there is always a compromise:
- more general purpose game engine is weaker in what it gives to the game logic but is less limited to some certain type of game (FPS, RPG ...);
- more specific engine is stronger (and more efficient) and makes life easier for scripters, but is also more or less limited in things it can offer for kinds of games, which it was not initially designed for.

I have no idea, how far my project will go, maybe there will be some interest among my collegues and they will want to join. What I am trying to create is something more or less abstract. I agree, it will be weak concerning functionality for any certain kind of game :( but I am doing this on purpose. I want some quick-and-dirty but still potent and modern platform for anything I might want to create later. That is why I wanted to put more load on scripting and also that is why I spent much time looking for a general architecture.

But I guess, I should stop talking and get to coding :lol: I have chosen some prebuilt libraries for 3D rendering, networking, physics, scripting, I have taken the code from "Game coding complete" as a base for the project structure, so now I have to stick it all together and see how it goes. :roll:

Re: Component based behaviorless entity systems and scriptin

Posted: Mon Sep 12, 2011 6:49 pm
by Rebornxeno
How about trying to develop a specific game first, and then extracting the "engine" part of it that you feel might be useful in other projects, instead of developing an engine.

Re: Component based behaviorless entity systems and scriptin

Posted: Mon Oct 31, 2011 3:22 pm
by crancran
I stumbled across your post midixand was curious how things are progressing?

I am still at the stage where I am trying to define these subsystems, their respective components, and how they communicate effectively with just basic concepts such as rendering, movement, and animation, etc. So far things are going a bit slow mainly due to the lack of a lot of in-depth examples. Most are very trivial examples I have encountered which leave a lot of the design on the reader to decipher :).

Re: Component based behaviorless entity systems and scriptin

Posted: Mon Oct 31, 2011 3:31 pm
by Falco Girgis
Rebornxeno wrote:How about trying to develop a specific game first, and then extracting the "engine" part of it that you feel might be useful in other projects, instead of developing an engine.
WHO THE FUCK DO YOU THINK YOU ARE!?!?!? :twisted:

Re: Component based behaviorless entity systems and scriptin

Posted: Mon Oct 31, 2011 11:24 pm
by Light-Dark
GyroVorbis wrote:
Rebornxeno wrote:How about trying to develop a specific game first, and then extracting the "engine" part of it that you feel might be useful in other projects, instead of developing an engine.
WHO THE FUCK DO YOU THINK YOU ARE!?!?!? :twisted:
I think its quite clear now. He's the guy who 'programmed' E.T For Atari :shock2:

Re: Component based behaviorless entity systems and scriptin

Posted: Tue Nov 01, 2011 1:17 am
by Aleios
Rebornxeno wrote:How about trying to develop a specific game first, and then extracting the "engine" part of it that you feel might be useful in other projects, instead of developing an engine.
Sense no make.

Re: Component based behaviorless entity systems and scriptin

Posted: Tue Nov 01, 2011 11:27 am
by Falco Girgis
Light-Dark wrote:
GyroVorbis wrote:
Rebornxeno wrote:How about trying to develop a specific game first, and then extracting the "engine" part of it that you feel might be useful in other projects, instead of developing an engine.
WHO THE FUCK DO YOU THINK YOU ARE!?!?!? :twisted:
I think its quite clear now. He's the guy who 'programmed' E.T For Atari :shock2:
:lol:

Re: Component based behaviorless entity systems and scriptin

Posted: Tue Nov 01, 2011 2:35 pm
by Rebornxeno
At least I finished something that people remember, wait I don't know who you are?

Re: Component based behaviorless entity systems and scriptin

Posted: Tue Nov 01, 2011 6:10 pm
by LeonBlade
Rebornxeno wrote:At least I finished something that people remember, wait I don't know who you are?
Finished being a dumb ass? No, you're still going at that one.
Also, you don't really have much credibility when most of your posts so far on this forum have been asking for help.

Re: Component based behaviorless entity systems and scriptin

Posted: Tue Nov 01, 2011 8:36 pm
by szdarkhack
Rebornxeno wrote:At least I finished something that people remember, wait I don't know who you are?
Wow... Just wow...