godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add @editor_only, @game_only annotation #8902

Open GameDevLlama opened 8 months ago

GameDevLlama commented 8 months ago

Describe the project you are working on

A game project which relies on several nodes with @tool scripts.

Describe the problem or limitation you are having in your project

Engine.is_editor_hint() will sometimes make it very hard to see at a first glance whether a block of code is executed inside the editor or not.

When I for example start configuration inside the _ready function, of course in most cases I will do something like this:

func _ready() -> void:
  if Engine.is_editor_hint(): _init_some_feature_for_editor()

Then probably the method will call some function which is intended to reduce code for the editor part

func _init_some_feature_for_editor() -> void:
  _some_editor_helper_function()

It will easily happen, that it's unclear which function is intended to run inside the editor mode only and which are supposed to be run during game. Only thing to further clearify the code, adding Engine.is_editor_hint() conditions over and over to prevent code from accidentally being executed during gameplay.

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

Adding annotations for example @editor_only or @game_only for functions would enable developers to quickly mark methods for where they should be executed.

This feature would reduce the time to analyze / understand the code significantly.

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

func _ready() -> void:
  _init_some_feature_for_editor()
  _init_some_game_feature()

@editor_only
# would only be called inside the editor because the function is marked by "@editor_only" annotation
func _init_some_feature_for_editor() -> void:
  _some_editor_helper_function()

@editor_only
# would only be called inside the editor because the function is marked by "@editor_only" annotation
func _some_editor_helper_function() -> void:
  pass # do stuff

@game_only
func _init_some_game_feature() -> void:
  pass # do stuff

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

I guess it would be used quite often. Of course it currently can be worked around by excessively using Engine.is_editor_hint(), but it makes code not only ugly, but hard to read and understand which code will be executed in the editor or in the game or both.

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

Well, it extends the GDScript language, so my guess is it cannot be added by an add-on

AThousandShips commented 8 months ago

See also:

Calinou commented 8 months ago

https://github.com/godotengine/godot-proposals/issues/7375 and https://github.com/godotengine/godot-proposals/issues/7496 are also tangentially related.