0
\$\begingroup\$

i was wondering what are the best practices for restarting the game, say for example in a platformer game when you die the game restarts and you have to reposition all the characters or reset the traps or what have you.

I thought two ways for doing this:

1) You have some level or scene manager that has a reference to all the things that should react to a game state change ,for example when restarting level, you would tell every character to reposition itself or something like that.

2) You have events. Every character or entity will subscribe to this level manager to an event like "OnLevelRestart" using delegates or function pointers, then when the manager restarts the level, it would trigger the event and all the ones who have subscribed to that event, will be informed and reset their own state.

The first approach seems more flexible because i have a reference to those objects, and i can send other kind of messages to them. so if in the future i want to do something different with those objects i already have the ability to do so. Now the second approach is...there, seems of course less flexible than the first option, but apart from that i don't have anything against it. The good thing is that the manager doesn't need to know all the objects that need to reset, so i can create new entities and register to those events ( and from this perspective, it seems flexible too).

Im concerned about the scalability and maintainability of those options in the future. Is there a "best practice" for doing this ? or is just "whatever fits yout project" ? Thanks.

My sincere apologies if this question doesn't meet the requirements to be asked on this site.

\$\endgroup\$
  • \$\begingroup\$ A better( imo ) alternative to the event subscription would be forcing every game object to provide an "IResettable" interface, if it has state to be reset. This just avoids having to manage the event subscription. You can see a somewhat similar idea with Doom 3's saving and loading. Each game object must implement a Save and Restore method. \$\endgroup\$ – Ben Feb 18 '15 at 0:19
  • \$\begingroup\$ I see, it would be a mix between the two, but how do avoid to manage the event subscription ? because you still have to either search for all the objects that implement that interface, or the object will find the manager and "subscribe" to it \$\endgroup\$ – Ramax Feb 18 '15 at 0:26
  • \$\begingroup\$ Yeah I assume you have a reference to all game objects. It's trivial to obtain: answers.unity3d.com/questions/329395/… \$\endgroup\$ – Ben Feb 18 '15 at 5:36
  • \$\begingroup\$ You should think about "destroy all world and recreate it from scratch". In this case your code will be much simpler. If you need to restore level with some changes make save/load mechanism, but don't use same objects between lives, just recreate them. If you use language with garbage collection (C#, Java) then recreation objects may be even more effective than reusing old objects. Of course you don't need to reload all textures, models, sounds and etc, because it's not modifiable resources. Just recreate all modifiable objects in game (player, enemies, etc). \$\endgroup\$ – Kostya Regent Feb 18 '15 at 9:39
1
\$\begingroup\$

You want to use separation of concern which means it would be the scene managers responsibility to reset the scene. It already loaded the scene and created the game objects that fill the scene, you can/should reuse the code you wrote for that already.

Simply keep track of which object matches up with the loaded data, if you don't have an object already (your loading the scene for the first time) then you create one. Then after that you reset it's state (possibly using GameObject.reset(position, hitpoints, aiState))

Since you didn't mention which language your using, here is some pseudo code describing what I meant:

function loadLevel {
   levelInfo = LevelFormat.load(file)
   createStaticLevelObject()
   resetLevel()
}

function resetLevel {
   for each object in levelInfo.dynamicObjects {
      if not levelObjects.exists(object.identifier) {
        levelObjects(object.identifier) = new LevelObject(object.visualInformation, etc)
      }
      levelObjects(object.identifier).resetTo(object.startInformation)
   }
}

Of course, simply dropping the entire level and reloading it is also a possibility, in the worst case this means loading it from the disk, but unless the level information is quite large it won't make a noticeable difference for the player and then you can just skip the check if an object exists or not.

\$\endgroup\$
  • \$\begingroup\$ interesting, and what would be the "startInformation" ? something that is particular to the object that im resetting ? for example if its an enemy it would be health \$\endgroup\$ – Ramax Feb 18 '15 at 15:02
  • \$\begingroup\$ @Ramax Yes, one option would be to have it be a dictionary where the game object reads named values from. Another is to have it be a serialized object of some sort. In the end it's a mapping of parameters to their starting values. Life, x/y coordinates, hostility, items in inventory. Whatever suits your game \$\endgroup\$ – Daniel Carlsson Feb 18 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.