Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to handle dead characters in game?  (Read 2869 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
How to handle dead characters in game?
« on: April 27, 2014, 04:28:00 pm »
Alright, I don't how to start this but I'll try. I do not know if I am dealing with a design problem. I have considered every moving things on my game as Objects class, they can live, they can die. When they die, what I do is I delete them. I don't know how heavy this Object class is in memory, so I just delete them when I don't need them (Assuming it doesn't leak). But not just that, when I want to write on C# or Java, I don't even have slightest idea how do you deal game objects that are dead(e.g. run out of HP etc.). Flagging them though is a nice idea. But if these objects, if ever, for example, becomes big and if the instance of an Object class (e.g enemy1, enemy2) become dead in the game, I don't want them on memory. Especially if the game spawns many of this things. This is what I am actually thinking.

How do you deal with this thing?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: How to handle dead characters in game?
« Reply #1 on: April 27, 2014, 04:31:07 pm »
Are you talking about C#/Java or C++?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: How to handle dead characters in game?
« Reply #2 on: April 27, 2014, 04:35:43 pm »
C++, but think also in terms of C# and Java when giving answers. Isn't you can write SFML app in C# and Java as there are bindings to it?

In C++ there is a delete keyword, very potential to leak. But assuming you made a very nice mechanism to safely delete objects and notifying all objects pertaining to it. But in managed language like C# and Java they have garbage collectors. I don't intend to emulate C++ on languages like C# and Java, I just don't know how, in general do you manage dead characters. Because deleting object seems like a design problem. I swear there are much better ways in handling this situation. If you flag them like charac.isDead() is very nice idea, but the notion that it is still on memory, bothers me. (being still looped in a general container, etc.)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: How to handle dead characters in game?
« Reply #3 on: April 27, 2014, 04:46:51 pm »
Quote
C++, but think also in terms of C# and Java when giving answers. Isn't you can write SFML app in C# and Java as there are bindings to it?

Yes you can use C# and Java, but I have no clue as to how far out of date the java bindings are.

Quote
In C++ there is a delete keyword, very potential to leak. But assuming you made a very nice mechanism to safely delete objects and notifying all objects pertaining to it

In any modern C++ you want to avoid manual memory management at all costs. Instead use RAII and smart pointers. RAII is similar to the memory management in managed languages as in memory will not leak (in most cases, there are ways to cause memory leaks in managed languages), but there are several fundamental differences.

Quote
I just don't know how, in general do you manage dead characters.

Not strictly speaking, but in managed languages (speaking C#/Java here) the way you let memory be reclaimed is to simply dereference the object. If it is in some sort of collection, just remove the object from the collection and set any other references you have to it to null (this applies to most cases). Then the garbage collector will come around and cleanup the object.
« Last Edit: April 27, 2014, 04:48:59 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Re: How to handle dead characters in game?
« Reply #4 on: April 27, 2014, 04:53:53 pm »
If you don't have thousands of characters (or if your game is designed poorly) then leaving dead characters as corpses is not a problem at all. You can flag them dead like your idea and let them display a corpse image and stop doing their logic. You can even delete the main object and spawn a lightweight corpse object just for display.

That being said, if you do not want to have them visually around after their death, just delete them. I assume you store your entities somewhere, for example in a vector. In that case, just pop them off it.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How to handle dead characters in game?
« Reply #5 on: April 28, 2014, 12:54:30 pm »
The way I do it: store all enemies in a container and delete it from the container as soon as it is dead. This dereferences the object as zsbzsb mentions.