Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Constant dictionaries do not work in autoloads #70469

Closed
belzecue opened this issue Dec 23, 2022 · 0 comments · Fixed by #71051
Closed

Constant dictionaries do not work in autoloads #70469

belzecue opened this issue Dec 23, 2022 · 0 comments · Fixed by #71051

Comments

@belzecue
Copy link

belzecue commented Dec 23, 2022

Godot version

3.5.1-stable

System information

Linux

Issue description

See here for background: https://godotforums.org/d/32258-const-x-dictionary-not-working-in-autoload-script-cannot-assign-to-it/3

The line below creates a CONST property pointing to a new empty dictionary:

const SSSM_GLOBAL_BLACKBOARD: Dictionary = {}

The following behaviors apply, and can be confirmed with simple testing in a regular script:

  1. A const dictionary can start empty or populated. In both cases, you can later add, change, remove keys and values at will.

  2. Once you have defined a const Dictionary property, you cannot later assign it a null value or another dictionary object or a new empty dictionary object. All three actions will throw an engine error, as expected.

HOWEVER, if you include your const dictionary property in an autoload script, the engine will throw an error when you attempt to do anything with that dictionary's contents. The error is "Invalid set index ... ". For example, attempting to add a new keypair to the empty dictionary. Oddly, it does actually add the keypair to the dictionary (confirmed by inspecting the dictionary object at runtime) and only after that throws the error, suggesting this is not intended behavior

What is it about autoloads that change the behavior of a const dictionary? They are nodes in the tree like any other, so I would expect the behavior to be the same as having the const dictionary in a regular node script.

Steps to reproduce

Start a new 2D project.

Create a new Globals.gd script:

extends Node

const SSSM_GLOBAL_BLACKBOARD: Dictionary = {}

Add the script to the project's autoloads.

Create new Node2D off root. Add to it this script:

extends Node

func _ready(): Globals.SSSM_GLOBAL_BLACKBOARD["x"] = 1

Run project and observe the error (see the godotforums link earlier for screenshots).

Remove the Globals.gd script from autoloads and copy the "const SSSM_GLOBAL_BLACKBOARD" property to your Node2D script. Alter the "Globals.SSSM_GLOBAL_BLACKBOARD" to "SSSM_GLOBAL_BLACKBOARD". Run the project and observe that no error is thrown.

Minimal reproduction project

See above for simple repro steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants