cant get custom traits to work

I have a problem when it comes to creating custom traits. I have followed what information I could find on the forums and in the documentation, yet when I generate the assemblies used for the AI my custom traits are not used/found.

I have made a script inheriting ICustomTrait and IEquatable, placed it in the namespace AI.Planner.Domains(also tried Unity.AI.Planner.DomainLanguage.TraitBased and AI.Planner.Custom.mynamespace) and I've made a assembly definition file referencing the necessary assemblies, as well as a trait asset in sync with the trait script in the same folder as the custom trait.

My script is essentially the same as the location trait as I've narrowed it down just to get it working, yet when generating the ai code the generator always creates a new trait. I'm sure I am missing something here but I can't see what..

My asmdef:
5657221--588643--Unity_sQQdQKKyQX.png

My custom trait, copy of location:

using UnityEngine;
using System;
using Unity.AI.Planner.DomainLanguage.TraitBased;

namespace AI.Planner.Domains
{
    [Serializable]
    public struct InRangeLocation : ICustomTrait, IEquatable<InRangeLocation>
    {

        public int transformInstanceId;
        public Vector3 position;
        public Vector3 forward;
        public float range;



        public Transform Transform
        {
            get => null;
            set
            {
                transformInstanceId = value.GetInstanceID();
                position = value.position;
                forward = value.forward;  
            }
        }


        public bool Equals(InRangeLocation other)
        {
            return transformInstanceId.Equals(other.transformInstanceId)
               && position == other.position
               && forward == other.forward;
        }

        public object GetField(string fieldName)
        {
            switch (fieldName)
            {
                case nameof(position):
                    return position;
                case nameof(forward):
                    return forward;
                case nameof(range):
                    return range;
            }
            return null;
        }

        public void SetField(string fieldName, object value)
        {
            switch (fieldName)
            {
                case nameof(position):
                    position = (Vector3)value;
                    break;
                case nameof(forward):
                    forward = (Vector3)value;
                    break;
                case nameof(range):
                    range = (float)value;
                    break;


                case nameof(Transform):
                    Transform = (Transform)value;
                    break;

            }
        }

        public override string ToString()
        {
            return $"Location: {position} {forward} {range}";
        }
    }

}

Unity 2019.3.1f1
AI-planner 0.2.2

Have you added a trait definition for the custom trait? That's how the package knows which traits to include, so I suspect that's what's missing.

Yeah, I've got a trait definition asset in sync with the customtrait script, even set the namespace field to my customtrait namespace. The problem is that the code generator fails to recognize the customtrait script and always generates a new trait script in the domains assembly instead of using my custom one. Am I missing something in the namespace/assembly setup?

So there is an error in the documentation about namespace/assembly for ICustomTrait.

For now the only option to create one is to:
Create an AssemblyDefinitionReference (AsmRef) pointing to Unity.AI.Planner assembly.

Create your ICustomTrait type in the same folder of the AsmRef and with the same name than your Trait asset and put it in Unity.AI.Planner.DomainLanguage.TraitBased' namespace.

Haven't looked too closely at it yet, but at first glance it looks like it's working now, cheers!

Hi there, I'm not sure if this is the right place for this but, when following these steps the Unity.AI.Planner library reference gets overwritten by a .csproj file which breaks Visual Studio 2019's reference pathing

That's not good. Multiple of the team develop with Rider, so we haven't experienced this potential issue with Visual Studio. Can you provide the exact steps you took so we can reproduce this error?

Okay sure. What I did was I created a new folder under the assets directory, in which I created the AsmRef file pointing to the Unity.AI.Planner assembly. Along with a ICustomTrait class and its twin asset ( As shown above. ). Which is working fine in Unity but the AsmRef creates the .csproj which Visual prefers over the actual assembly.

A fix for this is to remove the new fake assembly reference and add the real one in "\Library\ScriptAssemblies"

Edit: Words

1 Like

What version of Unity are you using for this project ?

Sorry for the delay but I'm using version 2019.3.8f1 with Visual studio 16.5.4