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

Allow conditional exports (or sub-exports) #2582

Closed
Tyrannosaurus1234 opened this issue Apr 10, 2021 · 9 comments
Closed

Allow conditional exports (or sub-exports) #2582

Tyrannosaurus1234 opened this issue Apr 10, 2021 · 9 comments

Comments

@Tyrannosaurus1234
Copy link

Tyrannosaurus1234 commented Apr 10, 2021

Describe the project you are working on

Text-based RPG

Describe the problem or limitation you are having in your project

I have a "door" class that exports a number of variables to the editor for easy configuration, relevant to this topic I have:
pickable: bool
pickLevel: int

pickLevel is useless if the door is not pickable, and only serves to clutter the script exports dock thing in the editor. For one variable this isn't a big deal, but if I were to add other things like kicking down doors or casting spells to unlock them, each depending on a different stat, it would quickly get out of hand.

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

Allow exporting variables conditionally, or attaching exports to other exports, such that pickLevel is only exposed to the editor if pickable is True.

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

I don't know how to cleanly express this in code, but maybe something like this:

export(bool) var pickable
export(int, pickable) var pickLevel

or maybe something with decorators?

export(bool) var pickable
@export_pickable
export(int) var pickLevel

or conditionals:

export(bool) var pickable
export(int) var pickLevel if pickable

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

As far as I'm aware there is no way to achieve this functionality currently.

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

It changes the core export system.

@KoBeWi
Copy link
Member

KoBeWi commented Apr 10, 2021

This can be achieved with _get_property_list().

@Xrayez
Copy link
Contributor

Xrayez commented Apr 10, 2021

Allow exporting variables conditionally, or attaching exports to other exports, such that pickLevel is only exposed to the editor if pickable is True.

Here's an example project showcasing it:

Test project:
conditional-export-property-list.zip

tool
extends Node2D

export(bool) var pickable = false setget set_pickable
var pick_level: int

func set_pickable(p_pickable):
	pickable = p_pickable
	property_list_changed_notify()

func _get_property_list() -> Array:
	var list = []
	if pickable:
		list.push_back(
			{ name = "pick_level", type = TYPE_INT }
		)
	return list

The _get_property_list() is poorly documented, though.

Or would you rather suggest a GDScript-specific solution to this? It depends on how common this feature will be used in other projects to justify implementing it on the language level.

In C++, this is also achieved with _validate_property() callback (which is not exposed) more easily than the GDScript + _get_property_list().

@MarcusElg
Copy link

MarcusElg commented Jun 18, 2022

Maybe there could be a @export_conditionally(expression) annotation? So your example would be:

@export var pickable: Bool
@export_conditionally(pickable == true) var pickLevel: Int

I feel it would be useful to hide properties that depend on other properties, to avoid confusing users with properties that don't affect anything.

@Calinou
Copy link
Member

Calinou commented Jun 18, 2022

Maybe there could be a @export_conditionally(expression) annotation? So your example would be:

@export var pickable: Bool
@export_conditionally(pickable == true) var pickLevel: Int

I feel it would be useful to hide properties that depend on other properties, to avoid confusing users with properties that don't affect anything.

One issue with @export_conditionally is that it implies the property is not exported if the condition isn't met. In this case, this means its value is lost in scene files when saving the scene if the condition isn't met at that time.

We should probably ensure exported-but-hidden properties are always saved by default to avoid data loss.

@MarcusElg
Copy link

MarcusElg commented Jun 18, 2022

Maybe there could be a @export_conditionally(expression) annotation? So your example would be:

@export var pickable: Bool
@export_conditionally(pickable == true) var pickLevel: Int

I feel it would be useful to hide properties that depend on other properties, to avoid confusing users with properties that don't affect anything.

One issue with @export_conditionally is that it implies the property is not exported if the condition isn't met. In this case, this means its value is lost in scene files when saving the scene if the condition isn't met at that time.

We should probably ensure exported-but-hidden properties are always saved by default to avoid data loss.

Yes, data loss should be avoided when possible. Maybe some name with visibility instead? As it's not visible in the inspector, something like @conditionally_visible or @conditionally_hide? Or maybe that's too inconsistent with existing naming.

Regardless of the name I feel like it would be a very useful feature to have as it's quite a common usecase imo, and it would be nice to avoid having to override the property list for it.

@KoBeWi
Copy link
Member

KoBeWi commented Jun 18, 2022

If the property is conditional and the conditional is not met, it means you don't need it and there is no data loss. Property value is chached internally until scene is reloaded, so accidental data loss is unlikely too.

@MarcusElg
Copy link

I wonder if this would be useful for the editor itself. Right now the Button's icon alignment property is shown for example regardless of a icon is selected or not. Showing properties that don't affect anything is defiently confusing for the user.

@KoBeWi
Copy link
Member

KoBeWi commented Jun 25, 2022

We already use conditional properties in the editor. This proposal is about GDScript convenience, which wouldn't have any use in the editor.

@YuriSizov
Copy link
Contributor

This is actually a duplicate of #1056. The OP there proposes a different solution, but an annotation is more likely to be accepted if we implement this (and also proposed in the comments).

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

6 participants