File tree Expand file tree Collapse file tree
src/openvic-extension/singletons Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3939 <description >
4040 </description >
4141 </method >
42+ <method name =" reset_player_singleton" >
43+ <return type =" void" />
44+ <description >
45+ </description >
46+ </method >
4247 <method name =" set_auto_assign_leaders" qualifiers =" const" >
4348 <return type =" void" />
4449 <param index =" 0" name =" value" type =" bool" />
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ StringName const& PlayerSingleton::_signal_province_selected() {
1919}
2020
2121void PlayerSingleton::_bind_methods () {
22+ OV_BIND_METHOD (PlayerSingleton::reset_player_singleton);
23+
2224 // Player country
2325 OV_BIND_METHOD (PlayerSingleton::set_player_country_by_province_number, { " province_number" });
2426 OV_BIND_METHOD (PlayerSingleton::get_player_country_capital_position);
@@ -74,6 +76,14 @@ PlayerSingleton::~PlayerSingleton() {
7476 singleton = nullptr ;
7577}
7678
79+ void PlayerSingleton::reset_player_singleton () {
80+ // This function is called when a game session ends, so we don't use functions like set_player_country or
81+ // unset_selected_province here as they can dereference leftover pointers from the previous game session which
82+ // are either already or soon to be invalid.
83+ player_country = nullptr ;
84+ selected_province = nullptr ;
85+ }
86+
7787// Player country
7888void PlayerSingleton::set_player_country (CountryInstance* new_player_country) {
7989 if (OV_unlikely (player_country == new_player_country)) {
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ namespace OpenVic {
3030 PlayerSingleton ();
3131 ~PlayerSingleton ();
3232
33+ void reset_player_singleton ();
34+
3335 // Player country
3436 [[nodiscard]] constexpr CountryInstance* get_player_country () {
3537 return player_country;
You can’t perform that action at this time.
0 commit comments