10-23-2019, 12:15 PM
Hi,
I'm trying to make a custom module that I can use in the Generator graph. It takes some spots, filters them, and then returns new spots based on a filter.
Sadly, I get a lot of errors in Curvy scripts (dictionary key not found, nullrefs, etc.). What am I doing wrong?
My code:
Editor:
The script CurvySpotFilters.Filter(filterFunction, spot.Position) is working as it should. I can implement the module the first time, but if I try to reopen the graph, it starts spitting errors. Sometimes the input node also taken 2 inputs for some reason (two lines are drawn towards it?) and I am unable to draw an edge from the output node.
It would be really convenient to have a tutorial on how to make a simple custom module, by the way.
Thanks in advance!
I'm trying to make a custom module that I can use in the Generator graph. It takes some spots, filters them, and then returns new spots based on a filter.
Sadly, I get a lot of errors in Curvy scripts (dictionary key not found, nullrefs, etc.). What am I doing wrong?
My code:
Code:
using System;
using UnityEngine;
using FluffyUnderware.DevTools;
using System.Collections.Generic;
using FluffyUnderware.Curvy.Generator.Modules;
using UnityEditor;
namespace FluffyUnderware.Curvy.Generator.Modules
{
[ModuleInfo("Build/Filter Spots", ModuleName = "Filter Spots", Description = "Filter spots based on position", UsesRandom = false)]
public class CurvySpotFilter : CGModule, IOnRequestPath
{
[HideInInspector]
[InputSlotInfo(typeof(CGSpots), Name = "Spots to Filter", DisplayName = "Spots")]
public CGModuleInputSlot InSpots = new CGModuleInputSlot();
[HideInInspector]
[OutputSlotInfo(typeof(CGSpots), Name = "The spots filtered after filter function", DisplayName = "Filtered Spots")]
public CGModuleOutputSlot OutSpots = new CGModuleOutputSlot();
#region ### Serialized Fields ###
[SerializeField, Label("Filter Function", Tooltip = "Filter Function defined in CurvySpotsFilters.cs")]
SpotFilterType filterFunction;
#endregion
#region - General Tab -
public SpotFilterType FilterFunction
{
get { return filterFunction; }
set
{ filterFunction = value; }
}
public float PathLength => throw new NotImplementedException();
public bool PathIsClosed => throw new NotImplementedException();
#endregion
#region ### Unity Callbacks ###
/*! \cond UNITY */
protected override void OnEnable()
{
base.OnEnable();
Properties.MinWidth = 350;
}
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
FilterFunction = filterFunction;
Dirty = true;
}
#endif
public override void Reset()
{
base.Reset();
FilterFunction = 0;
}
/*! \endcond */
#endregion
public override void Refresh()
{
base.Refresh();
List<CGSpot> filtered = new List<CGSpot>();
CGSpots all = InSpots.GetData<CGSpots>();
for (int i = 0; i < all.Count; i += 1)
{
CGSpot spot = all.Points[i];
if (!CurvySpotFilters.Filter(filterFunction, spot.Position))
{
filtered.Add(spot);
}
}
CGSpots spotsOut = new CGSpots(filtered);
OutSpots.SetData(spotsOut);
}
public CGData[] OnSlotDataRequest(CGModuleInputSlot requestedBy, CGModuleOutputSlot requestedSlot, params CGDataRequestParameter[] requests)
{
if (requestedSlot == OutSpots)
{
List<CGSpot> filtered = new List<CGSpot>();
CGSpots all = InSpots.GetData<CGSpots>();
if (all != null)
{
for (int i = 0; i < all.Count; i += 1)
{
CGSpot spot = all.Points[i];
if (CurvySpotFilters.Filter(filterFunction, spot.Position))
{
filtered.Add(spot);
}
}
CGSpots spotsOut = new CGSpots(filtered);
OutSpots.SetData(spotsOut);
return new CGData[1] { spotsOut };
}
}
return null;
}
}
}
Editor:
Code:
using UnityEditor;
using FluffyUnderware.Curvy.Generator.Modules;
namespace FluffyUnderware.CurvyEditor.Generator.Modules
{
[CustomEditor(typeof(CurvySpotFilter))]
public class CurvySpotFilterEditor : CGModuleEditor<CurvySpotFilter>
{ }
}
The script CurvySpotFilters.Filter(filterFunction, spot.Position) is working as it should. I can implement the module the first time, but if I try to reopen the graph, it starts spitting errors. Sometimes the input node also taken 2 inputs for some reason (two lines are drawn towards it?) and I am unable to draw an edge from the output node.
It would be really convenient to have a tutorial on how to make a simple custom module, by the way.
Thanks in advance!