Search Unity

Unable to provide a default for GetValue on Object ValueInput

Discussion in 'Visual Scripting' started by xavierdhjr, Jul 12, 2021.

  1. xavierdhjr

    xavierdhjr

    Joined:
    Jan 20, 2020
    Posts:
    5
    Greetings,

    I have written an EventUnit that should be able to accept an optional input to further filter whether the Event should trigger. The issue is that I get a MissingValuePortInputException when calling GetValue on this input despite telling the ValueInput that it should have a default value / allow null.

    I would like for either GetValue to return a default value if none has been connected in the script graph, and then be able to check for that in script.

    Am I going about this the right way?

    upload_2021-7-12_13-3-14.png

    Inside the EventUnit class:
    Code (CSharp):
    1.  
    2. public class ActionUnitReference
    3. {
    4.     // Data ...
    5. }
    6.    
    7. protected override void Definition()
    8. {
    9.         base.Definition();
    10.         Action= ValueInput<ActionUnitReference>("Action", null).AllowsNull();
    11.         Action.SetDefaultValue(new ActionUnitReference());
    12. }
    13.  
    14. protected override bool ShouldTrigger(Flow flow, ActorProjectileHitArgs args)
    15. {
    16.         bool result = false;
    17.         ActionUnitReference actionUnitReference= flow.GetValue<ActionUnitReference>(Action);
    18.         // ^ Produces exception
    19.  
    20.         // check if incoming ActorProjectileHitArgs should trigger this event
    21.  
    22.        return result;
    23. }
    24.  
    25.  
    Any help would be appreciated :)
     
  2. MasterSubby

    MasterSubby

    Joined:
    Sep 5, 2012
    Posts:
    252
    Make sure AllowsNull attribute is above the port field too. If you're still having trouble, the unit has its own defaultValues field that is protected. I've had much more success with that, and if your type is Inspectable, will also show it visually, any Inspectable type directly on the unit.
     
  3. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,338
    Sorry for necroing. I ran into this too and here is some context. The problem is
    ValueInput.SupportsDefaultValue(Type type)
    . It returns false for any type that is not derived from UnityObject or its decendants or is primitive (int, float, ..) or is in a predefined list of types. So if you have a custom class it will always return false and thus no default value will be stored.

    Here is an excerpt from the ValueInput class:
    Code (CSharp):
    1. private static readonly HashSet<Type> typesWithDefaultValues = new HashSet<Type>()
    2. {
    3.     typeof(Vector2),
    4.     typeof(Vector3),
    5.     typeof(Vector4),
    6.     typeof(Color),
    7.     typeof(AnimationCurve),
    8.     typeof(Rect),
    9.     typeof(Ray),
    10.     typeof(Ray2D),
    11.     typeof(Type),
    12. #if PACKAGE_INPUT_SYSTEM_EXISTS
    13.     typeof(UnityEngine.InputSystem.InputAction),
    14. #endif
    15. };
    16.  
    17. public static bool SupportsDefaultValue(Type type)
    18. {
    19.     return
    20.         typesWithDefaultValues.Contains(type) ||
    21.         typesWithDefaultValues.Contains(Nullable.GetUnderlyingType(type)) ||
    22.         type.IsBasic() ||
    23.         typeof(UnityObject).IsAssignableFrom(type);
    24. }

    You can add a defaut value manually via unity.defaultValues:

    Code (csharp):
    1.  
    2. InputABC= ValueInput<YourType>("InputABC", null);
    3. InputABC.unit.defaultValues.Add("InputABC", null);
    4.  
    But it leads to some unpleasant rendering of the node as there is no inspector for it:
    upload_2023-7-11_10-50-53.png

    This can be avoided by adding the
    [Unity.VisualScripting.Inspectable]
    attribute to your custom type (source).

    Code (csharp):
    1.  
    2. [Unity.VisualScripting.Inspectable]
    3. public class YourType
    4. {
    5. }
    6.  
    Imho, that's close to a bug (flaw in design). It's not broken per se but very very cumbersome to deal with and introduces dependencies on VisualScripting in classes that need not know of their use in VisualScripting.

    This should be addressed. But as rumours go, VisualScripting is now being just "maintained" as Unity is working on a complete rewrite (still years away though).
     
    Last edited: Jul 11, 2023
    iScriptz likes this.