Jump to content
  • Advertisement
Sign in to follow this  
hyyou

Unity Component based architecture .... for game!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any link to for Component based architecture in some complex case / real case  that objects has a lot of relation to each other?

 

I have faced some difficulty when tried create some relation Ex. Bullet-Component rotating around Rocket-Component.

 

It is hard to find good links, here is what I have :-

None of these links show how to manage relation.

 

The order of execution of system (that manage each type of Component) seems to important too (Ex. Physic must before Graphics),

but none of these links describe it.

 

I am noob, help!

 

Edit

The problem is mainly "Where should I store the relation?   Bullet or Rocket?  Something above them?"  

 

If I create some component/system that depends on two (e.g. BulletRocketSystem) , I will have to aware the order of execution.

BulletRocketSystem should execute after Bullet and Rocket's system.  

If Bullet will be deleted by Bullet_System's command, the BulletRocketSystem must be notified and remove reference of it.

 

This promotes couple between System, which is bad.

Moreover, there have to be a headquarter to enforce the order, I don't know if there is non-ugly way to do it.

 

---

 

Thank for a lot of responses, I have learned a lot. +1 all

 

@Alberth:  Thank!   I forgot to think about it carefully.   I should not force myself to use entity system to solve everything.

 

@Norman Barrows:  You are right.  Unity does it, so I followed it.   I do it for business (hopefully).

 

@phil_t:  I have edited OP.  XD

 

@Tangletail:   You caught me.  Rocket should = Shootable + HP + Physics + Graphics + ....   I hope this is what you mean.

Edited by hyyou

Share this post


Link to post
Share on other sites
Advertisement

Any particular reason why you don't drop component-based design? Or just use it at the parts where it fits?

 

I have no experience with these architectures, but at times it looks like a religion to me. People try to push every single thing into the system, for seemingly no other reason than it being the system.

In my experience, trying to unify everything in one system doesn't give nice results.

Share this post


Link to post
Share on other sites

>> I have faced some difficulty when tried create some relation Ex. Bullet-Component rotating around Rocket-Component.

 

precisely, what are you trying to do?

 

 

>>  Component based architecture

 

ECS (as opposed to somewhat more straightforward composition) has a number of uses:

 

1. team members without the access and/or knowledge to modify the code base must be able to define new entity types from pre-defined components already existing in the game engine. test question: do team members who must define entity types not have code access and / or knowledge?

 

2. to reduce build times by making entity type definitions data driven, so you don't have to recompile when you define a new entity. test question: how often do you define a new entity type?

 

3. there's a method for optimizing update once you can squeeze no more clock cycles out of render and you're still too slow. the result is more or less an ECS. test question: have you already optimized render, and you're still too slow?

 

4. just for fun or as a learning exerience. test question: are you doing this just for fun, or just as a learning experience?

 

if you pass any of these test questions, ECS may be called for.

 

note that generic game engines like Unity et al have requirement #1. I suppose that's why some folks thinks an ECS is "de rigueur".

Edited by Norman Barrows

Share this post


Link to post
Share on other sites

 

 

I have faced some difficulty when tried create some relation Ex. Bullet-Component rotating around Rocket-Component.

 

 

 

 

As a general rule, if you don't want A to depend on B, or B to depend on A, then you can create... C... which depends on both and manages the needed interaction between them.

 

I don't think your problem has anything *specifically* to do with component-based design. You would need to address it in other architectures too - it's just that a component-based design forces you to think about limiting dependencies more, so it the issue becomes more obvious and less trivial to hack your way around.

 

I don't really know why a bullet would need to rotate around a rocket - but can you just add a script to the object that knows about "bullet" and "rocket", and manages the needed synchronization between the two?

Share this post


Link to post
Share on other sites
Why do you have bullet and rocket explicitly defined as components?

You might be horribly misunderstanding the idea of ECS.

The idea is to make each component as data. Bullet and Rocket are nearly identical to each other. The two are objects with similar components.

A bullet has velocity, a sprite, collision and damage.

A rocket has nearly the same, except you have a particle trail.

The explosion is just another object that has a short life with a trigger, particles,and damage.

You need to decide on how the games logic is handled, as systems are one of the major fallouts of the system. Edited by Tangletail

Share this post


Link to post
Share on other sites

At the end of the day.... its really really really not about the code - it's about what the code does. You can get crazy, obsessive even, about trying to organize code in a way that makes you sleep well at night.

 

I have had the best luck so far with just writing code to make things work and reworking when necessary. What I found is that the more experienced I get the better I get at writing systems that work well together that require minimal rewriting. But there really is no short cut to get there. I believe ECS (or in general composition) is the result of this process for many programmers who have then shared their findings with the world.

 

So you can either spend a bunch of time trying to exactly mimic what these other programmers have done, or you can go through the process yourself and try to take their most important lessons in consideration - focusing more on the functionality than the code itself.

Share this post


Link to post
Share on other sites

At the end of the day.... its really really really not about the code - it's about what the code does. You can get crazy, obsessive even, about trying to organize code in a way that makes you sleep well at night.

 

I have had the best luck so far with just writing code to make things work and reworking when necessary. What I found is that the more experienced I get the better I get at writing systems that work well together that require minimal rewriting. But there really is no short cut to get there. I believe ECS (or in general composition) is the result of this process for many programmers who have then shared their findings with the world.

 

So you can either spend a bunch of time trying to exactly mimic what these other programmers have done, or you can go through the process yourself and try to take their most important lessons in consideration - focusing more on the functionality than the code itself.

After years of coding, I realized when I read about ECS that it was what I eventually settled on.  It works, but I work with systems of components (a entity is just an ID, not even a struct/class, it's just an ID in a vector passed on to components as a parent ID more or less).

As for Norman Barrows, tons of engines use ECS, why there were GDC talks on the subject.  Unity uses components, never looked much more into it but they work great for data driven.  You seem very confused on their use.  Its not how often you create a new data type like Engine or Missile etc.  It's how often you combine these things into game objects like ships, or cars, and make new ones with tweaks etc.  Data driven code iterates insanely fast and that is what is most important.  Writing code that allows designers, artists and other non-coding team members rapid and easy access to testing new ideas.  If a programmer needs to be involved, you likely have a horrible system or its very early in the design phase where a lot of code is still being added.  ECS is no different in it's usefulness to a designer than a visual shader editor is to artists.  Take as much work away from programmers as possible is the best idea ever, we can then focus more on fixing bugs, adding needed features, cleaning up the code, etc.  Instead of writing thousands of lines that might get cut when that part of the game is found to be boring or pointless.  If it was all data, it just gets deleted and you're more or less done.

There is a time and place for ECS, it is not a magic bullet.  I do not have an OnUpdate() etc function for my entity, it's a lot more cache friendly to have each component saved in it's respective system.  Some systems have covered multiple components (position and movement for instance).  There is no right or wrong way to implement it honestly within reason.  At the same time I also do not completely shun inheritance, I just try and figure out other ways around it.  But it still exists in my code base and I do not lose sleep over it.  Also my graphics system is in no way really tied to the ECS system.  I use ECS more or less for purely game object code.  YMMV though.

To the OP, to me a bullet is not a component, but an entity made up of components like Target, Position, Velocity, TimeToLive, etc. and give/take what you want, was off the top of my head after midnight.  Then the rocket would be (also an entity) much the same with Fuel, Engine, etc. and the Target of the rocket could be the bullet (in my case purely an ID), which it would circle if it could never directly intercept, or based on the rocket AI component.  

Share this post


Link to post
Share on other sites

 

The problem is mainly "Where should I store the relation?   Bullet or Rocket?  Something above them?"  

 

If I create some component/system that depends on two (e.g. BulletRocketSystem) , I will have to aware the order of execution.

BulletRocketSystem should execute after Bullet and Rocket's system.  

If Bullet will be deleted by Bullet_System's command, the BulletRocketSystem must be notified and remove reference of it.

 

This promotes couple between System, which is bad.

Moreover, there have to be a headquarter to enforce the order, I don't know if there is non-ugly way to do it.

 

 

The order of execution of systems is not hard to enforce. In fact, it is essential that there be a strict order that you define. There's nothing ugly about it. This could be as simple as the order in which systems are added to your world, or perhaps each system has a UpdateOrder number for it, and the world keeps the systems sorted in this order when doing updates.

 

It's also not necessarily a bad thing if one system depends on another (for instance, you might have a System that organizes entities in some spatial partitioning scheme - other systems will need to query this).

 

But like others, I question your choice of Rocket and Bullet components - but it's hard to say without knowing more about your game and what they do. Rocket I can kind of understand (it could contain like amount of fuel, engine max speed, etc...). But what is unique about a Bullet? I guess it could contain information on the types of damage it deals, etc...  Can you explain what makes your Bullet and Rocket unique, and why you need specific code to deal with them (as opposed to just being entities with transform/physics/model components)?

 

"If Bullet will be deleted by Bullet_System's command, the BulletRocketSystem must be notified and remove reference of it."

 

Managing entity lifetime can be one of the trickier parts of an ECS. But it is something that should be solved generally. That is, when an entity is destroyed, you should have code that notifies any systems that were interested in it that it was removed. This is also true if you remove a component from an entity that causes a system to no longer be interested in that entity (i.e. removing Bullet component should tell the BulletSystem that this entity has been removed). Again, this should be done at a general level, by having systems express which components they require entities to have.

 

Ideally, if a system doesn't store any information about an entity, then it may not even care if it was destroyed. That is, if your Bullet system update logic can just say "enumerate all the entities with Bullet components" each time, then it really doesn't need to know if something was destroyed or added, since it will always get a current list of bullets.

 

One final point: you may want to consider delaying the destruction of entities until the end of the update cycle. Just mark them to be deleted, then have some code that actually removes them at the end or beginning of the update cycle.

Edited by phil_t

Share this post


Link to post
Share on other sites

If I create some component/system that depends on two (e.g. BulletRocketSystem) , I will have to aware the order of execution.

BulletRocketSystem should execute after Bullet and Rocket's system.  

If Bullet will be deleted by Bullet_System's command, the BulletRocketSystem must be notified and remove reference of it.

 

This promotes couple between System, which is bad.

Moreover, there have to be a headquarter to enforce the order, I don't know if there is non-ugly way to do it.

 

Order of systems is a very basic thing that will need to be set one way or the other. You can't have all systems running in whatever order. Doesn't makes sense to render if the transforms haven't been updated. Doesn't makes sense to run the sound system or the asset streaming system if no movement was processed yet.

 

In Artemis ECS, and consequently, the fork I've been working on (dustArtemis, look at my signature), all systems get notified when entities get added, removed or changed. So they have the opportunity to react to situations like yours, or if an entity mutates (ie, you remove/add an effect over an entity).

 

Order is enforced because you set it indirectly (by the order in which you added sub systems to the World instance) or directly (via a priority value). Here:

https://github.com/dustContributor/dustArtemis/blob/master/src/com/artemis/World.java#L306

 

Thats the main processing routine. First it notifies all systems of entity changes made during the last tick, then it notifies the entity and component managers of the same changes (so components can be removed/entities deleted) then it lets systems process their main workload, which might end up in more entity additions/removals. Rinse and repeat.

 

Moreover, the main focus of decoupling system ordering always seemed to be so you can run each system in their own thread. Over the years developers realized it isn't a good idea. Workloads of each subsystem vary a lot, so most of the time one thread would get chewed while the others would barely do anything. Nowadays the approach is to go "wide"  at each system. Got a couple thousand tasks to run in the movement system? Parallelize them in groups, then collect the results (if such step is needed) and move to the next system.

Edited by TheChubu

Share this post


Link to post
Share on other sites

>>  It's how often you combine these things into game objects like ships, or cars, and make new ones with tweaks etc.  

 

that's what i call defining a new "entity type". IE a new kind of "game object".   some games must do this often and have many people who must be able to do it with various skill sets, and some games do not. like most things it depends on the work to be done, and the size and skills of the team.

 

IE if you were going to bang out pong, pacman, galaga, space invaders, or missile command, etc. by yourself on weekend just for fun, you wouldn't bother with ECS.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!