@@ -74,6 +74,8 @@ class OptionBase {
7474 float _min = 0 ;
7575 float _max = 1 ;
7676
77+ bool _is_once = false ;
78+
7779 SCP_unordered_map<PresetKind, SCP_string> _preset_values;
7880
7981 flagset<OptionFlags> _flags;
@@ -100,6 +102,9 @@ class OptionBase {
100102 const flagset<OptionFlags>& getFlags () const ;
101103 void setFlags (const flagset<OptionFlags>& flags);
102104
105+ bool getIsOnce () const ;
106+ void setIsOnce (bool is_once);
107+
103108 const SCP_string& getConfigKey () const ;
104109 const SCP_string& getTitle () const ;
105110 const SCP_string& getDescription () const ;
@@ -519,6 +524,7 @@ class OptionBuilder {
519524 // The global variable to bind the option to once. Will require game restart to persist changes.
520525 OptionBuilder& bind_to_once (T* dest)
521526 {
527+ _instance.setIsOnce (true );
522528 return change_listener ([dest](const T& val, bool initial) {
523529 if (initial) {
524530 *dest = val;
@@ -555,13 +561,13 @@ class OptionBuilder {
555561 return *this ;
556562 }
557563 // Finishes building the option and returns a pointer to it
558- const Option<T>* finish ()
564+ std::shared_ptr< const Option<T>> finish ()
559565 {
560566 for (auto & val : _preset_values) {
561567 _instance.setPreset (val.first , json_dump_string_new (_instance.getSerializer ()(val.second ),
562568 JSON_COMPACT | JSON_ENSURE_ASCII | JSON_ENCODE_ANY));
563569 }
564- std::unique_ptr<Option<T>> opt_ptr ( new Option<T>(_instance) );
570+ auto opt_ptr = make_shared< Option<T>> (_instance);
565571
566572 if (mpark::holds_alternative<std::pair<const char *, int >>(_title)) {
567573 const auto & xstr_info = mpark::get<std::pair<const char *, int >>(_title);
@@ -573,9 +579,8 @@ class OptionBuilder {
573579 lcl_delayed_xstr (opt_ptr->_description , xstr_info.first , xstr_info.second );
574580 }
575581
576- auto ptr = opt_ptr.get (); // We need to get the pointer now since we loose the type information otherwise
577- OptionsManager::instance ()->addOption (std::unique_ptr<OptionBase>(opt_ptr.release ()));
578- return ptr;
582+ OptionsManager::instance ()->addOption (opt_ptr);
583+ return opt_ptr;
579584 }
580585};
581586
0 commit comments