Is there a quick way to empty the variables of an object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a quick way to empty the variables of an object?

I made an instance of the class Game. Now let's say I want to start a new game all of a sudden, like when your character dies in the game, it says "Game Over" then asks you if you want to quit or start a new game. From my previous tries when I try to run the method I created called NewGame() all the values that I previously assigned to the instance didn't actually reset, they're still there. I solved this problem by emptying each variable before running the NewGame() method. I tought that maybe a Destructor would be useful to quicken this operation, but from my research it isn't possible to call a Destructor inside another method, it simply calls itself when the object isn't used anymore. Now my question is, is there a way to empty these variables automatically when I call the instance NewGame()? Any suggestions?

24th Apr 2021, 7:49 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
2 Answers
+ 2
I think it's hard to tell what would be a "good" way without knowing the structure of your program... If you want to reset each and every attribute of Game() – maybe instantiate a new Game() after game over?
24th Apr 2021, 10:25 AM
Lisa
Lisa - avatar
0
U can call destructor through ur custom method (IDisposable). Also u can create constructor, which will be reassign ur variables (if some of them still not reset due to creating a new instance, how u said) Or do an another class, which will be consider variables and create an instance of this class each time, when new game start
25th Apr 2021, 6:21 PM
Deprion
Deprion - avatar