Component based behaviorless entity systems and scripting

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

midix
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Sat Sep 10, 2011 1:02 pm

Component based behaviorless entity systems and scripting

Post 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.
Last edited by midix on Sun Sep 11, 2011 12:24 pm, edited 1 time in total.
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Component based behaviorless entity systems and scriptin

Post 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.
midix
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Sat Sep 10, 2011 1:02 pm

Re: Component based behaviorless entity systems and scriptin

Post 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...
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Component based behaviorless entity systems and scriptin

Post 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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Component based behaviorless entity systems and scriptin

Post 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?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
midix
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Sat Sep 10, 2011 1:02 pm

Re: Component based behaviorless entity systems and scriptin

Post 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:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Component based behaviorless entity systems and scriptin

Post 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.
crancran
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Mon Oct 31, 2011 3:17 pm

Re: Component based behaviorless entity systems and scriptin

Post 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 :).
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Component based behaviorless entity systems and scriptin

Post 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:
User avatar
Light-Dark
Dreamcast Developer
Dreamcast Developer
Posts: 307
Joined: Sun Mar 13, 2011 7:57 pm
Current Project: 2D RPG & NES Platformer
Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
Programming Language of Choice: C/++
Location: Canada

Re: Component based behaviorless entity systems and scriptin

Post 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:
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
Image
Aleios
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Mon Feb 21, 2011 2:55 am
Current Project: Aleios Engine
Favorite Gaming Platforms: PC, Dreamcast
Programming Language of Choice: C++
Location: Melbourne, Australia

Re: Component based behaviorless entity systems and scriptin

Post 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.
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Component based behaviorless entity systems and scriptin

Post 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:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Component based behaviorless entity systems and scriptin

Post by Rebornxeno »

At least I finished something that people remember, wait I don't know who you are?
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Component based behaviorless entity systems and scriptin

Post 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.
There's no place like ~/
User avatar
szdarkhack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Fri May 08, 2009 2:31 am

Re: Component based behaviorless entity systems and scriptin

Post by szdarkhack »

Rebornxeno wrote:At least I finished something that people remember, wait I don't know who you are?
Wow... Just wow...
Post Reply