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

How to configure dependencies between options? For example, whether option B is displayed depends on option A. #3

Open
YzZ-ZzY opened this issue Aug 23, 2024 · 5 comments
Assignees
Labels

Comments

@YzZ-ZzY
Copy link

YzZ-ZzY commented Aug 23, 2024

No description provided.

@zenvin-dev
Copy link
Owner

zenvin-dev commented Aug 23, 2024

Right now, you would have to reference the dependency in every dependee and subscribe to their respective change event during Setting initialization. Then you would need to have your event handler react to any changes by updating the dependee's Visibility (see below for an example).

I am aware that this is not an ideal workflow, and I am currently in the process of conceptualizing a way that remedies things like this (and also related stuff like Setting value validation).

Example for what I mentioned above.
Note that I typed this out in GitHub, so syntax or name errors may be present.

public abstract class DependantSetting<T, U> : SettingBase<T>
{
  [SerializeField] private SettingBase<U> dependency;
  [SerializeField] private U compareValue;
  [SerializeField] private SettingVisibility matchingVisibility = SettingVisibility.Visible;
  [SerializeField] private SettingVisibility nonMatchingVisibility = SettingVisibility.Disabled;

  protected override void OnInitialize()
  {
    if(dependency != null)
    {
      dependency.ValueChanged += DependencyValueChangedHandler;
      SetVisibilityWithoutNotify(GetGoalVisibility());  // Make sure the Setting starts out correctly
    }
  }

  private void DependencyValueChangedHandler(ValueChangeMode mode)
  {
    var visibility = GetGoalVisibility();
    SetVisibility(visibility);
  }

  private SettingVisibility GetGoalVisibility()
  {
    if(dependency == null)
      return Visibility;  // If there is no dependency configured, stick with current visibility

    var dependencyFulfilled = dependency.CompareEquality(dependency.CurrentValue, compareValue);
    return dependencyFulfilled ? matchingVisibility : nonMatchingVisibility;
  }
}

Some of the SettingControls from the samples will automatically reflect any changes in a Setting's visibility; others may need to implement it manually. The easiest way to do that would be to provide a custom implementation for SettingControl<T,U>.OnVisibilityChanged.

For an example implementation, see the ToggleControl.

@zenvin-dev zenvin-dev self-assigned this Aug 23, 2024
@zenvin-dev
Copy link
Owner

Alternatively, you could reverse the process I described earlier and have the dependency maintain a list of dependees and their required values, and proactively update them when its value updates.

@YzZ-ZzY
Copy link
Author

YzZ-ZzY commented Aug 26, 2024

Thanks for the replies!

@zenvin-dev
Copy link
Owner

You're welcome!

@zenvin-dev
Copy link
Owner

Update:
It is now possible to add Components to Settings and Setting Groups. Those allow to do stuff like this more easily.

See commit e805487 and the ConditionalVisibility sample. @YzZ-ZzY

Note:
The feature works without issues as far as I could see, but I haven't tested it extensively.
So if you encounter any (apparent) bugs, let me know.

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

No branches or pull requests

2 participants