From 7c4e23ed8bdff6433edfcb3b3bbbabf00c3a8f66 Mon Sep 17 00:00:00 2001
From: Relintai <relintai@gmail.com>
Date: Fri, 20 Mar 2020 14:41:13 +0100
Subject: [PATCH] fix automatic save in ProfileManager.

---
 profiles/player_profile.cpp    |  4 ++++
 singletons/profile_manager.cpp | 11 ++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/profiles/player_profile.cpp b/profiles/player_profile.cpp
index 4f2764b..7385aa9 100644
--- a/profiles/player_profile.cpp
+++ b/profiles/player_profile.cpp
@@ -136,6 +136,8 @@ Dictionary PlayerProfile::to_dict() const {
 
 	dict["class_profiles"] = arr;
 
+	dict["custom_data"] = _custom_data;
+
 	return dict;
 }
 void PlayerProfile::from_dict(const Dictionary &dict) {
@@ -158,6 +160,8 @@ void PlayerProfile::from_dict(const Dictionary &dict) {
 		_class_profiles.push_back(c);
 	}
 
+	_custom_data = dict.get("custom_data", Dictionary());
+
 	emit_change();
 }
 
diff --git a/singletons/profile_manager.cpp b/singletons/profile_manager.cpp
index 685846e..b191c09 100644
--- a/singletons/profile_manager.cpp
+++ b/singletons/profile_manager.cpp
@@ -66,6 +66,10 @@ void ProfileManager::adds_player_profile(const Ref<PlayerProfile> &profile) {
 	_s_player_profiles.push_back(profile);
 }
 void ProfileManager::clears_player_profiles() {
+	//for (int i = 0; i < _s_player_profiles.size(); ++i) {
+	//	_c_player_profile->disconnect("changed", this, "_on_player_profile_changed");
+	//}
+
 	_s_player_profiles.clear();
 }
 void ProfileManager::removes_player_profile(const int index) {
@@ -158,9 +162,11 @@ Dictionary ProfileManager::to_dict() const {
 void ProfileManager::from_dict(const Dictionary &dict) {
 	ERR_FAIL_COND(dict.empty());
 
-	//clears_player_profiles();
+	clears_player_profiles();
 
+	_c_player_profile->disconnect("changed", this, "_on_player_profile_changed");
 	_c_player_profile->from_dict(dict.get("cplayer_profile", Dictionary()));
+	_c_player_profile->connect("changed", this, "_on_player_profile_changed");
 
 	Array arr = dict.get("splayer_profiles", Array());
 
@@ -183,6 +189,7 @@ ProfileManager::ProfileManager() {
 	_save_file = GLOBAL_DEF("ess/profiles/save_file", "user://profile.save");
 
 	_c_player_profile.instance();
+	_c_player_profile->connect("changed", this, "_on_player_profile_changed");
 
 	if (_automatic_load)
 		call_deferred("load");
@@ -191,6 +198,8 @@ ProfileManager::ProfileManager() {
 ProfileManager::~ProfileManager() {
 	_instance = NULL;
 
+	_c_player_profile->disconnect("changed", this, "_on_player_profile_changed");
+
 	_s_player_profiles.clear();
 }