@@ -71,7 +71,8 @@ def GlobRecursive(path, pattern, **kwargs):
71
71
'GBIND_METHODS_PREPEND' ,
72
72
'GRESOURCE_LOADER' ,
73
73
'GRESOURCE_SAVER' ,
74
- 'GEDITOR_PLUGIN'
74
+ 'GEDITOR_PLUGIN' ,
75
+ 'GSINGLETON'
75
76
] + INIT_LEVELS
76
77
77
78
ALL_KEYWORDS = KEYWORDS + TARGETLESS_KEYWORDS
@@ -530,6 +531,8 @@ def process_macros(item, macros, properties, is_ignored=False):
530
531
class_defs ['init_level' ] = 'EDITOR'
531
532
class_defs ['is_editor_plugin' ] = True
532
533
534
+ case 'GSINGLETON' :
535
+ class_defs ['is_singleton' ] = True
533
536
534
537
535
538
return not is_ignored
@@ -696,7 +699,7 @@ def write_header(file, defs, env):
696
699
header_rpc_config = 'void {}::_rpc_config() {{{}}}\n ' .format (
697
700
class_name_full , '\n ' + header_rpc_config if header_rpc_config != '' else '' )
698
701
header_bind_methods = '\n \n ' .join (i for i in [Hmethod , Hvirtual_method , Hstatic_method , Hvaragr_method , Hprop , Hsignal , Henum , Hbitfield , Hconst ] if i != '' )
699
- header_bind_methods = content ['bind_methods_append ' ] + header_bind_methods + content ['bind_methods_prepend ' ]
702
+ header_bind_methods = content ['bind_methods_prepend ' ] + header_bind_methods + content ['bind_methods_append ' ]
700
703
701
704
header_defs += [f'// { class_name_full } : { content ["base" ]} \n ' ,
702
705
'void {}::_bind_methods() {{{}}}\n ' .format (
@@ -730,6 +733,7 @@ def write_register_header(defs_all, env):
730
733
classes_register_levels = {name [12 :] : [] for name in INIT_LEVELS }
731
734
732
735
loaders_savers = []
736
+ has_singleton = False
733
737
def make_register_str_pair (class_name_full , content ):
734
738
register_str = f"\t GDREGISTER_{ content ['type' ]} ({ class_name_full } );\n "
735
739
unregister_str = ''
@@ -751,6 +755,12 @@ def make_register_str_pair(class_name_full, content):
751
755
elif 'is_editor_plugin' in content :
752
756
register_str += f'\t EditorPlugins::add_by_type<{ class_name_full } >();\n '
753
757
758
+ elif 'is_singleton' in content :
759
+ nonlocal has_singleton
760
+ has_singleton = True
761
+ register_str += f'\t Engine::get_singleton()->register_singleton("{ content ["class_name" ]} ", memnew({ class_name_full } ));\n '
762
+ unregister_str += f'\t Engine::get_singleton()->unregister_singleton("{ content ["class_name" ]} ");\n \t memdelete({ class_name_full } ::get_singleton());\n '
763
+
754
764
return register_str , unregister_str
755
765
756
766
for file , filecontent in defs_all ['files' ].items ():
@@ -778,7 +788,10 @@ def make_register_str_pair(class_name_full, content):
778
788
if loaders_savers != []:
779
789
scripts_header += '#include <godot_cpp/classes/resource_loader.hpp>\n '
780
790
scripts_header += '#include <godot_cpp/classes/resource_saver.hpp>\n '
781
-
791
+
792
+ if has_singleton :
793
+ scripts_header += '#include <godot_cpp/classes/engine.hpp>\n '
794
+
782
795
classes_register_str = ''
783
796
if classes_register_levels ['CORE' ] != []:
784
797
minimal_register_level = 'MODULE_INITIALIZATION_LEVEL_CORE'
0 commit comments