How reset data at memory

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hashak

Sorry for bad English. I try to describe my problem.

I make a sign-up page for my game and set a data at global singleton and save that when player start game at first time.

var user = {
	name = null,
	money = 1000,
	etc
{

I make a remove button for delete data, If the player want to start new game again.

func user_remove_data():
	var dir = Directory.new()
	dir.remove(_global.PATH_USER)

Everything is OK. But my problem is:

  1. Player sign-up (write name).
  2. Save data and start playing.
  3. For example buy something and money become to 700.
  4. Come back to sign-up and remove name (game data)
  5. Write another name and start new game
  6. Money is NOT 1000, money is 700.

I add a for loop to func user_remove_data() for reset data

for data in _global.user:
	if data == 'name':
		_global.user[data] = null
	elif data == 'money':
		_global.user[data] = 1000

I want to know that is there a better way to reset data at memory or my way is okey?

:bust_in_silhouette: Reply From: hilfazer

If you make your ‘singleton’ like this:

var user = initUser()

func initUser():
	return { name = null, money = 1000 }

func resetUser():
	user = initUser()

you will have initial values for user var in just one place. This is good because if you’ll want to change initial values you’ll only need to do this in one place.

resetUser() can be called fron any script you want, no need to write reset code every time.

Thank you!!!

hashak | 2018-07-31 21:25