Skip to content

Commit 68135a8

Browse files
author
Vano
committed
safer singleton creation/deletion
1 parent 04535cc commit 68135a8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

cppscript.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ def write_header(file, defs, env):
707707
elif 'is_resource_saver' in content:
708708
variable_name = content["class_name"] + '_saver'
709709
global_variables.append(f'Ref<{class_name_full}> {variable_name};')
710+
elif 'is_singleton' in content:
711+
global_variables.append(f'{content["class_name"]}* {content["class_name"]}_singleton_ptr;')
710712

711713
for type, name, init in content['static_members']:
712714
global_variables.append(f'alignas({type}) char {class_name_full}::{name + "_impl"}[] = {{0}};')
@@ -777,8 +779,9 @@ def make_register_str_pair(class_name_full, content):
777779
elif 'is_singleton' in content:
778780
nonlocal has_singleton
779781
has_singleton = True
780-
register_str += f'\tEngine::get_singleton()->register_singleton("{content["class_name"]}", memnew({class_name_full}));\n'
781-
unregister_str += f'\tEngine::get_singleton()->unregister_singleton("{content["class_name"]}");\n\tmemdelete({class_name_full}::get_singleton());\n'
782+
loaders_savers.append(f'extern {content["class_name"]}* {content["class_name"]}_singleton_ptr;')
783+
register_str += f'\t{content["class_name"]}_singleton_ptr = memnew({class_name_full});\n\tEngine::get_singleton()->register_singleton("{content["class_name"]}", {content["class_name"]}_singleton_ptr);\n'
784+
unregister_str += f'\tEngine::get_singleton()->unregister_singleton("{content["class_name"]}");\n\tmemdelete({content["class_name"]}_singleton_ptr);\n'
782785

783786
return register_str, unregister_str
784787

0 commit comments

Comments
 (0)