Cause im doing a game so i want to reset the game.. what will be the code so that i can reset all the game and make it like if it was just opened?

Recommended Answers

All 2 Replies

There really isn't one..... without looking at your project, I couldn't tell you. You'll need to reset all your variables, and all your forms objects, but there is no magic quick reset button.

Depending on your coding style, there might be a magic reset button after all. But it all depends on how you code your game.

If all of the variables and controls etc. that you need to reset the values of are contained in forms, a full reset is easy. To test my theory, I set up a simple VB project with a single form and a few controls. After making those controls somewhat functional, I created a single module with only one Public Sub - that's it, nothing else - which had only eight lines of code in it. This is called from the form when you click a button labeled "Reset". Last, I created a second form which is only shown during the game reset, and so doesn't need to be a part of the reset itself. It covers the whole screen on load and has a line object bouncing back and forth between the sides of the form while the game form resets. This form is loaded and displayed when the reset is started, then hidden and unloaded when the reset completes, so it doesn't take up any memory when you aren't resetting. Here's the code of the module, complete in it's entire goodness.

Public Sub ResetGame()
        Form1.Hide       ' Hide the game form...
        Load Form2       ' Load the "Resetting" form...
        Form2.Show       ' Show the "Resetting" form...
        Unload Form1     ' Here's where the actual reset takes place.
        Load Form1       ' If you set up your Form_Load correctly, 
                         ' everything should be loaded with default
                         ' values now.
        Form2.Hide       ' Hide the "Resetting" form...
        Unload Form2     ' Unload the "Resetting" form to save memory...
        Form1.Show       ' Show the game form - reset complete!
End Sub

Note that this will only work if all of your game variables are in your game form(s). Any values stored in modules, for example, will still have to be cleared manually.

With that, your Form_Load or Sub Main should have the proper code to initialize every value to a "new game" value anyway, meaning all you have to do is re-run the Sub Main or the Form_Load (whichever one you use to start the game). The only drawback to this is that if you still have code running (such as an unfinished game loop or a function that only got halfway done), it will try to continue running with the "new game" values. The Unload/Load method I showed above should stop those as well.

Either way, the solutions do vary depending on your coding style. These are some options, though, to get you thinking in the right mindset to figure out what you need to do. IF you have any more questions, or if I just confused the hell out of you, feel free to ask!

- Sendoshin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.