-2

I've built a small text RPG in python, which has an end state where you run out of health and takes you back to the main menu. The problem is when you start a new game, all of the objects are still in the previous state (enemies dead, loot collected etc).

Is there anyway of effectively resetting it, as if I'd killed and restarted the script without actually doing so?

EDIT: I was wondering if there was a generic way of reinitialising, but if it helps my project is here

vido.ardes
  • 81
  • 2
  • 8
  • 1
    objects will get recycled if references towards them doesn't exist; are you keeping the previous state alive somehow by means of a name still assigned to it? If so, it'll stick around. Drop any names to the previous state and it will eventually get collected. – Dimitris Fasarakis Hilliard Nov 01 '16 at 15:26
  • 2
    You could write your own restart script just initializing the objects to default state, but other than that not sure if you don't provide more details on how you coded your stuff. – MooingRawr Nov 01 '16 at 15:27
  • Irregardless, without code, you're making the answers to this question unnecessarily vague. – Dimitris Fasarakis Hilliard Nov 01 '16 at 15:27
  • [You can view the code here](https://github.com/vidoardes/stg_rpg), I was just wondering if there was a generic way to reintialise everything, not anything specific to my code – vido.ardes Nov 01 '16 at 15:31

1 Answers1

1

The straight forward way to do this is to have an object representing your Game state:

game = GameEngineState()

and then the interactions / state are kept within that object. This object is created when the player presses start, and is recreated when the user starts a new game:

if start:
    game = GameEngineState()

That way there is no global state regarding how the current game is progressing, and you can implement saving/loading by serializing the active game object.

If you need to have explicit deallocation routines (for example resources loaded to the GPU or libraries that require a notification when things are shutting down), you can implement a close() method in your game state that you call before restarting the game, or even better, use with to let Python handle it for you when the object goes out of scope.

with GameEngine() as game:
    # do stuff to game / read inputs / change state / let the player play..
    pass
Community
  • 1
  • 1
MatsLindh
  • 49,529
  • 4
  • 53
  • 84