This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I am working on a game collection launcher. I have 10 existing games with Autoloaded singletons. I plan to add more in future.
I import existing games through .pck (ProjectSettings.loadresourcepack(...)), but imported games need their own singletons.

1, How can I add autoload singletons after the game (collection launcher) started?
Adding nodes to root won't make the node visible in global scope variables. (cannot access the singleton by its name)
2, Is there any way to add global scope variables?

No details about this in documents:
(https://docs.godotengine.org/en/stable/getting_started/workflow/export/exporting_pcks.html?#summary)
https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html

Thanks.

Godot version 3.3.3.
in Engine by (327 points)

3 Answers

+1 vote
Best answer

My findings is that it is not possible to add singletons after the game started and running.

I solved that like this:
1, create placeholder/dummy singletons manually in editor before the game started.
2, Import the resources/scriptis with ProjectSettings.loadresourcepack(...) in runtime.
3, replace all singletons's script with set_script(...) in runtime.

This requires planning. Singleton names and paths must match.

by (327 points)
selected by
0 votes

Create an EditorPlugin, use its add_autoload_singleton().

by (332 points)

Thanks.
It is good in Editor. I am looking for a solution after the game started and running.

+2 votes

Not really an answer to your question but maybe a "good enough" solution - I think it is easier than calling set_script

  1. create a single autoloaded singleton with fields holding the real singletons. Initially set these fields equal to null:

    # in singletons.gd    ## autload this file
    var GAME = null
    var ECONOMY = null
    var NETWORK = null
  1. in runtime, when initialize the fields.
    eg. load Game.pck file...and then
    Singletons.GAME = Game.new()

  2. in the other files you can use Singletons.GAME directly, or initialize a field for easy access. Of course, you need to make sure to use it only after it was initialized.


    # eg, in enemy.gd
    var GAME = Singletons.GAME
    GAME.enemies += 1
by (23 points)
edited by

That is a good idea. I will try that later.
Thank you.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.