@@ -42,6 +42,10 @@ class ProjectSettings : public Object {
4242
4343 bool is_changed = false ;
4444
45+ // Starting version from 1 ensures that all callers can reset their tested version to 0,
46+ // and will always detect the initial project settings as a "change".
47+ uint32_t _version = 1 ;
48+
4549public:
4650 typedef HashMap<String, Variant> CustomMap;
4751 static inline const String PROJECT_DATA_DIR_NAME_SUFFIX = " godot" ;
@@ -218,6 +222,9 @@ class ProjectSettings : public Object {
218222 String get_scene_groups_cache_path () const ;
219223 void load_scene_groups_cache ();
220224
225+ // Testing a version allows fast cached GET_GLOBAL macros.
226+ uint32_t get_version () const { return _version; }
227+
221228#ifdef TOOLS_ENABLED
222229 virtual void get_argument_options (const StringName &p_function, int p_idx, List<String> *r_options) const override ;
223230#endif
@@ -243,3 +250,26 @@ Variant _GLOBAL_DEF(const PropertyInfo &p_info, const Variant &p_default, bool p
243250#define GLOBAL_DEF_RST_NOVAL_BASIC (m_var, m_value ) _GLOBAL_DEF(m_var, m_value, true , true , true )
244251
245252#define GLOBAL_DEF_INTERNAL (m_var, m_value ) _GLOBAL_DEF(m_var, m_value, false , false , false , true )
253+
254+ // ///////////////////////////////////////////////////////////////////////////////////////
255+ // Cached versions of GLOBAL_GET.
256+ // Cached but uses a typed variable for storage, this can be more efficient.
257+ // Variables prefixed with _ggc_ to avoid shadowing warnings.
258+ #define GLOBAL_GET_CACHED (m_type, m_setting_name ) ([](const char *p_name) -> m_type {\
259+ static_assert (std::is_trivially_destructible<m_type>::value, " GLOBAL_GET_CACHED must use a trivial type that allows static lifetime." );\
260+ static m_type _ggc_local_var;\
261+ static uint32_t _ggc_local_version = 0 ;\
262+ static SpinLock _ggc_spin;\
263+ uint32_t _ggc_new_version = ProjectSettings::get_singleton ()->get_version ();\
264+ if (_ggc_local_version != _ggc_new_version) {\
265+ _ggc_spin.lock ();\
266+ _ggc_local_version = _ggc_new_version;\
267+ _ggc_local_var = ProjectSettings::get_singleton ()->get_setting_with_override (p_name);\
268+ m_type _ggc_temp = _ggc_local_var;\
269+ _ggc_spin.unlock ();\
270+ return _ggc_temp;\
271+ }\
272+ _ggc_spin.lock ();\
273+ m_type _ggc_temp2 = _ggc_local_var;\
274+ _ggc_spin.unlock ();\
275+ return _ggc_temp2; })(m_setting_name)
0 commit comments