Jump to content
  • Advertisement
Sign in to follow this  
hyyou

C++ Shutdown : an exceptional case in the last timestep of game (ECS)

Recommended Posts

I have a game loop in Entity-Component-System architecture (heavily simplified) :-

spawn some enemy Turret, only in some timesteps;
every Turret : try to shoot -> create new Bullet();
every Bullet : destroy itself if lifeTime = 0;
every Turret : destroy itself if hp = 0;
entity_component_system->cleanUpAsNeed();

It works, but to make it complete, I am supposed to delete entity at the last time step myself.

Thus, I add many condition involving ☁lastFrame☁ :-

if(!☁lastFrame☁ ) spawn some enemy Turret, only in some timesteps;
if(!☁lastFrame☁) every Turret : try to shoot -> create new Bullet();
every Bullet : destroy itself if (lifeTime = 0 || ☁lastFrame☁);
every Turret : destroy itself if (hp = 0 || ☁lastFrame☁);
entity_component_system->cleanUpAsNeed();

It works but it is very dirty and error-prone.

I imagine that this will also happen when room/scene changes.

How to solve it elegantly?

  • How do good games solve this problem typically?
  • Are there any related "design pattern" I should read?
  • Is it already OK, i.e. am I too nervous?

Some days, I had nightmare because of it .... 

... a nightmare that my code become ☁lastFrame☁-flavor spaghetti.

 

Edited by hyyou

Share this post


Link to post
Share on other sites
Advertisement

Make the bullets owned by the turret, and the turret owned by the scene.

Alternatively... in the ECS pattern, components are allocated within systems. Just tell the Bullet system and the Turret system to delete all of their components at the end of a scene.

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
Sign in to follow this  

  • 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!