Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

c++ GDExtension how to get Autoload Global Data #1623

Open
Caesar454905970 opened this issue Oct 14, 2024 · 2 comments
Open

c++ GDExtension how to get Autoload Global Data #1623

Caesar454905970 opened this issue Oct 14, 2024 · 2 comments
Milestone

Comments

@Caesar454905970
Copy link

Caesar454905970 commented Oct 14, 2024

Godot version

4.3

godot-cpp version

4.3

System information

Window10 godot-cpp4.3

Issue description

ERROR: Failed to retrieve non-existent singleton 'GlobalEcgResObj'.
at: (core/config/engine.cpp:294)

image
image
image

Steps to reproduce

// 获取 GlobalEcgResObj 单例
Object *global_ecg_res_obj = Engine::get_singleton()->get_singleton("GlobalEcgResObj");

// 检查是否成功获取单例
if (global_ecg_res_obj) {
    // 获取 check_uid 变量的值
    Variant check_uid_variant = global_ecg_res_obj->get("check_uid");

    // 转换为字符串类型
    String check_uid_str = check_uid_variant.operator String();

    // 打印输出
    UtilityFunctions::print("check_uid: " + check_uid_str);
} else {
    UtilityFunctions::print("Failed to  GlobalEcgResObj singleton.");
}

Minimal reproduction project

Failed to GlobalEcgResObj singleton.

@AThousandShips
Copy link
Member

Autoloads are not singletons, see here

You access them with the node path I believe

@xana43
Copy link

xana43 commented Oct 29, 2024

if this needs to be an autoload that's present in the scene then what @AThousandShips said should be used.

if this is meant to be a singleton in all sense of the word then those can be made

NOTE: there could possibly be a better way to do this, but this is the way I found that works.

like so

Singleton class somewhere in your project
image

In register_types.cpp

Singleton registration
image

Singleton cleanup
image

@dsnopek dsnopek added this to the 4.x milestone Jan 21, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants