Jump to content
  • Advertisement
Andre Zunino

C++ ECS: seeking advice on modeling a particular situation

Recommended Posts


First, a bit of context. I'm having my first contact with the entity-component-system architectural pattern in the simple shoot'em up game I'm writing. I appreciate the flexibility and decoupling the pattern promotes. So far, I believe I have the basics down. I have already defined a handful of components and systems which operate based on them.

I'm now looking to add a cannon to the player's spaceship. I defined a Cannon component and attached it to the player entity (IOW, I "tagged" the player entity as having that capability). That component has a single member which indicates the cannon's rate of fire. Then I defined a PlayerWeapon system that is responsible for actually firing. At the moment, all it's doing is write a message to the console. Because no actual rate control is implemented yet, each press of the assigned fire button causes multiple messages to be output in quick succession. I would like to implement a sort of debouncing mechanism to have the fire rate respect what the cannon component dictates, e.g. fire once every 500ms.

Now for the actual question on ECS modeling. Suppose I'd use a simple elapsed time accumulator to decide when the next shot should be allowed to fire. Where would that information be stored? Should the system be responsible for managing that type of information? Or should systems be considered completely stateless? In that case, the accumulator would be stored in the cannon component, along with the existing fire rate. Would you kindly share some advice on how to handle a situation like the above? As stated, I'm new to ECS and would like to make sure I implement the concepts as properly as possible.

Thank you.

Share this post

Link to post
Share on other sites
3 hours ago, Andre Zunino said:

As stated, I'm new to ECS and would like to make sure I implement the concepts as properly as possible.

There is no definitive guide to ECS.  Different people handle things differently.  Stop worrying about fitting things into a perfect design pattern and write working code around your data.

For example, I would not have made a PlayerWeapon system but a BulletWeapon/MissileWeapon/or just Weapon system that can handle it for all weapons in general or those specific types if you want to.  You could add another data point to the Cannon component that records the last tick it was fired on which the system can check when requested to fire again but ignores until the rate of fire has expired from the previous shot.  Then update the tick time and start over again.  Basically, if ( (current_tick - last_shot_tick ) < rate_of_fire ) then ignore requests to fire.

Share this post

Link to post
Share on other sites

Thanks, Mike. Tutorial videos and articles on the topic corroborate your observation that there is no single way of implementing ECS. I guess I should just relax a bit and go with what works.

I will add the tick counter to my cannon component and be done with it.

Thanks again and I hope I can count on your input for future inquiries on ECS.

Best regards.

Share this post

Link to post
Share on other sites

In this case your Cannon component acts as a context. If you have an ai that has a lot of world state in it(its context) you want to store there everything other components might need, like "lastTimeSincePatrol", it's still a podo, but you can think of it as a current state of the object. In your case you seem to think that the component is like read only. if you view your cannon component as a volatile context then you safely add into it "timeSinceLastShot" and the weapon system will operate on it accordingly. Then question arises if you have a Cannon component and then MissileLauncher component, would your weapon system have to if(getcomp<Cannon>().. else if(getcomp<Missile>()).. or you can just getcomps<Weapon>() , foreach weapon...

and then the Weapon component describes what it is internally, is it a cannon or missile and how big of difference it makes for weapon system, does it even care about the type? both have rate of fire, and all it cares is to launch according projectile.

Edited by AlexanderAntonov

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!