0 votes

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?

asked Jul 30, 2018 in Engine by hashak (32 points)

1 Answer

0 votes
Best answer

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.

answered Jul 31, 2018 by hilfazer (1,842 points)
selected Jul 31, 2018 by hashak

Thank you!!!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.