alelievr / NodeGraphProcessor

Node graph editor framework focused on data processing using Unity UIElements and C# 4.6
https://github.com/alelievr/NodeGraphProcessor/projects/2
MIT License
2.21k stars 369 forks source link

Calling `AddExposedParameter` in `BaseGraph.OnEnable` causes all ports and edges to disappear. #214

Open rhys-vdw opened 2 years ago

rhys-vdw commented 2 years ago

I'm trying to create a system where I can ensure that certain graph types always expose the correct properties.

My base node with helper:

using GraphProcessor;
using System.Linq;
using UnityEngine;

namespace EffortStar {
  public class ESBaseGraph : BaseGraph {
    protected void EnsureParameter<T>(string name, object value = default) where T : ExposedParameter {
      var existing = exposedParameters.Find(p => p.name == name);
      if (existing != null) {
        if (existing.GetType() != typeof(T)) {
          RemoveExposedParameter(name);
        }
      }
      if (existing == null) {
        AddExposedParameter(name, typeof(T), value);
      }
    }
  }
}

Example graph:

using GraphProcessor;
using System.Linq;
using UnityEngine;
using System;

namespace EffortStar {
  using Events;

  [Serializable]
  public class ActorEventParameter : ExposedParameter {
    [SerializeField] ActorEvent _value;

    public override object value {
      get => _value;
      set => _value = (ActorEvent) value;
    }
  }

  [Serializable]
  public class EventContextExposedParameter : ExposedParameter {
    [SerializeField] EventContext _value;

    public override object value {
      get => _value;
      set => _value = (EventContext) value;
    }
  }

  [CreateAssetMenu(menuName = "EffortStar/Graph/ActorEventGraph")]
  public class ActorEventGraph : ESBaseGraph {
#if UNITY_EDITOR
    protected override void OnEnable() {
      EnsureParameter<ActorEventParameter>(nameof(ActorEvent));
      EnsureParameter<EventContextExposedParameter>(nameof(EventContext));
    }
#endif
  }
}

New graph created with correct exposed parameters: image

Adding some nodes:

image

Close and reopen graph editor: image

Possibly related to #155

rhys-vdw commented 2 years ago

Okay, I think I've worked it out. If I explicitly override GetValueType() in my exposed parameter subclass then the error goes away. Given this I would suggest that the base method should be abstract instead of virtual (or just fix the bug whatever is causing it).

rhys-vdw commented 2 years ago

I was wrong, the issue is back. I am really unsure what's causing it.

rhys-vdw commented 2 years ago

Okay, seems this was caused by a failure to call base.OnEnable()