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

Add Project Global Constants #11714

Open
rob1gat opened this issue Feb 6, 2025 · 2 comments
Open

Add Project Global Constants #11714

rob1gat opened this issue Feb 6, 2025 · 2 comments

Comments

@rob1gat
Copy link

rob1gat commented Feb 6, 2025

Describe the project you are working on

A 2D top-down game, using tilemaps and sprites.

Describe the problem or limitation you are having in your project

Let's say I have a tilemap with 3 layers (Ground, Furnitures and Top) and a character (which is a sprite) which navigates on my tilemap. In order to properly display this, I want to setup my z-indexes, so I set :

  • Ground z-index = 0
  • Furnitures z-index = 1
  • Character z-index = 1
  • Top z-index = 2

Since I'm going to have many levels, I make a scene level_tilemap.tscn with my tilemap layers and a scene character.tscn so I can reuse them.

Later, I want my character to jump so I add a script with a jump() method to my character and in this method I temporarily modify my character's z-index so that it equals 2 for a short time, and then returns to the value of 1.

Later, I want to add some carpets to my scene, so I have to add another layer to my tilemap that can fit between Ground and Furniture. In order to do so, I have to :

  • Open level_tilemap.tscn and change every z-indexes
  • Open character.tscn and change z-index
  • Open character.gd and changes z-indexes in the jump() method
  • Be aware of all the other scenes or scripts that may rely on these z-indexes

This is a deliberately simplified case that could be solved (or at least mitigated) by a proper and documented z-index strategy but I just want to illustrate that sometimes, one can have constants that are used all across a project and that can be used both in scenes and in scripts.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add project constants that could be defined in project parameters and used in scenes and scripts. With this feature, adding the carpet layer would just need to add a global constant for carpet z-index, update z-index global constants values and update level_tilemap.tscn.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Adding project constants and manage constants values

Image
In project parameters > Globals, a new Constants tab would be added. This tab allows to create new constants and to manage values of each constant.

Referencing constants in scenes

Image
In inspector fields, it would be possible to enter a constant instead of a value. To enter a constant, user should right-click on the field, then select Constant value then choose a constant in a list of all the constants that have the proper type.

Referencing constants in scripts

Some API would be added to access constants values from script. For example :
var top_z_index:int = ProjectConstants.get_constant("z_index_top")
The get_constants() method would have autocompletion for constant names in a similar way to Input methods have autompletion for actions names.

Additional concerns

  • Constants value modification should be reflected in opened scenes
  • Constant renaming could be desired but would update all scripts and scenes that use the constant
  • I wonder if this feature could be an extension of ProjectSetting

If this enhancement will not be used often, can it be worked around with a few lines of script?

Constant in scripts can easily be worked around by adding a script that centralize constants, but these constants could not be used in scenes.

Is there a reason why this should be core and not an add-on in the asset library?

I'm not too familiar with the possibilities of GDExtension and addons, but I guess that adding constants to inspector fields, saving constants in scene file, and adding autocompletion for constant names in script should be handled in core.

@Calinou
Copy link
Member

Calinou commented Feb 6, 2025

This already exists in the form of custom project settings. In fact, ProjectSettings was called Globals before Godot 3.0 🙂

You can already add your own project settings in any category (or even at the top-level) by writing in the name field, choosing a type and clicking Add.

What's not currently supported is being able to reference project settings within inspector properties, but this sounds difficult to implement and serialize reliably. Not to mention the problems around renaming settings (we already have this issue with exported properties).

@KoBeWi
Copy link
Member

KoBeWi commented Feb 8, 2025

What's not currently supported is being able to reference project settings within inspector properties

It can be achieved with inspector plugin.

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

No branches or pull requests

3 participants