Skip to content

Commit e04cb51

Browse files
author
Vano
committed
singleton macro
1 parent 3fdf37e commit e04cb51

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cppscript.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def GlobRecursive(path, pattern, **kwargs):
7171
'GBIND_METHODS_PREPEND',
7272
'GRESOURCE_LOADER',
7373
'GRESOURCE_SAVER',
74-
'GEDITOR_PLUGIN'
74+
'GEDITOR_PLUGIN',
75+
'GSINGLETON'
7576
] + INIT_LEVELS
7677

7778
ALL_KEYWORDS = KEYWORDS + TARGETLESS_KEYWORDS
@@ -530,6 +531,8 @@ def process_macros(item, macros, properties, is_ignored=False):
530531
class_defs['init_level'] = 'EDITOR'
531532
class_defs['is_editor_plugin'] = True
532533

534+
case 'GSINGLETON':
535+
class_defs['is_singleton'] = True
533536

534537

535538
return not is_ignored
@@ -696,7 +699,7 @@ def write_header(file, defs, env):
696699
header_rpc_config = 'void {}::_rpc_config() {{{}}}\n'.format(
697700
class_name_full, '\n' + header_rpc_config if header_rpc_config != '' else '')
698701
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']
700703

701704
header_defs += [f'// {class_name_full} : {content["base"]}\n',
702705
'void {}::_bind_methods() {{{}}}\n'.format(
@@ -730,6 +733,7 @@ def write_register_header(defs_all, env):
730733
classes_register_levels = {name[12:] : [] for name in INIT_LEVELS}
731734

732735
loaders_savers = []
736+
has_singleton = False
733737
def make_register_str_pair(class_name_full, content):
734738
register_str = f"\tGDREGISTER_{content['type']}({class_name_full});\n"
735739
unregister_str = ''
@@ -751,6 +755,12 @@ def make_register_str_pair(class_name_full, content):
751755
elif 'is_editor_plugin' in content:
752756
register_str += f'\tEditorPlugins::add_by_type<{class_name_full}>();\n'
753757

758+
elif 'is_singleton' in content:
759+
nonlocal has_singleton
760+
has_singleton = True
761+
register_str += f'\tEngine::get_singleton()->register_singleton("{content["class_name"]}", memnew({class_name_full}));\n'
762+
unregister_str += f'\tEngine::get_singleton()->unregister_singleton("{content["class_name"]}");\n\tmemdelete({class_name_full}::get_singleton());\n'
763+
754764
return register_str, unregister_str
755765

756766
for file, filecontent in defs_all['files'].items():
@@ -778,7 +788,10 @@ def make_register_str_pair(class_name_full, content):
778788
if loaders_savers != []:
779789
scripts_header += '#include <godot_cpp/classes/resource_loader.hpp>\n'
780790
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+
782795
classes_register_str = ''
783796
if classes_register_levels['CORE'] != []:
784797
minimal_register_level = 'MODULE_INITIALIZATION_LEVEL_CORE'

src/cppscript_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ void function(decltype(property) value) { \
5050
#define GRESOURCE_LOADER(...)
5151
#define GRESOURCE_SAVER(...)
5252
#define GEDITOR_PLUGIN(...)
53+
#define GSINGLETON(...);
5354

5455
#endif // CPPSCRIPT_HEADER

0 commit comments

Comments
 (0)