How to Properly Create SharedVariables with Inherited Classes

tsujiha

New member
Hello,

Thank you for providing such a wonderful tool. I am trying to create a SharedVariable for a class "FooEx" that inherits from the "FooBase" class, but I am encountering difficulties in achieving the intended functionality.

I am following the instructions in the following article to create the "SharedFooBase" and "SharedFooEx" classes:[Link to the article: https://opsive.com/support/documentation/behavior-designer/variables/creating-shared-variables/]

Afterward, when I write something like

public SharedFooBase foo;

and select the target class in the Inspector's combo box, both the options for FooBase and FooEx are displayed. However, when I choose the option for the FooEx class, the following error is outputted, and I cannot register it:

  • GUI Error: Invalid GUILayout state in view. Verify that all layout Begin/End calls matchUnityEditor.EditorGUI/PopupCallbackInfo:SetEnumValueDelegate (object,string[],int)
Is there a recommended method for handling inherited classes with SharedVariable? I appreciate your assistance and would be grateful for your response.

Thank you.
 
Last edited:
Which version of Unity are you using? While that error is annoying and I'll get it fixed it should be harmless
 
Justin, thank you for your response.

I am using Unity version 2022.3.12f1. Unfortunately, I haven't tested it with other versions.

Even when selecting the class inherited from the combo box, the value is not set due to an error.

If there is a fix available, I would like to wait for it.

Thank you.
 
Thanks for the details. I am trying to reproduce it but haven't been able to. Can you send the SharedVariable that you have created along with the classes required to not get a compiler error? If you can email it to support@opsive.com that would be great.
 
Thank you for your reply.

I will provide the reproduction steps here.

First, create the following classes:

FooBase.cs
C#:
using UnityEngine;

namespace Test
{
    public abstract class FooBase : MonoBehaviour
    {
    }
}

FooEx.cs
C#:
using UnityEngine;

namespace Test
{
    public class FooEx : FooBase
    {
    }
}

SharedFooBase.cs
C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime
{
    [System.Serializable]
    public class SharedFooBase : SharedVariable<FooBase>
    {
        public static implicit operator SharedFooBase(FooBase value) { return new SharedFooBase { mValue = value }; }
    }
}

SharedFooEx.cs
C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime
{
    [System.Serializable]
    public class SharedFooEx : SharedVariable<FooEx>
    {
        public static implicit operator SharedFooEx(FooEx value) { return new SharedFooEx { mValue = value }; }
    }
}

Next, create a Task as follows:

TestTask.cs
C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    public class TestTask : Action
    {
        [SerializeField]
        private SharedFooBase foo;

        public override TaskStatus OnUpdate()
        {
            if(foo.Value == null)
            {
                return TaskStatus.Failure;
            }

            return TaskStatus.Success;
        }
    }
}

Finally, create a SharedFooEx class named CurrentFooEx in the Variables. When trying to set CurrentFooEx to the foo property in the Inspector of TestTask, the previously reported error occurs.

I have outlined the steps for reproduction, and it's possible that there might be an error in my usage. I would appreciate it if you could review the provided information.

Looking forward to your guidance.
 
Thanks, your steps look like they should work. I just tried that and didn't get any errors. You're on 2022.3.12, right? Can you create a new project with these scripts and send it to support@opsive.com so I can take a closer look?

1706191746344.png
 
I use DYNAMIC VARIABLES.

I am creating a FooEx parameter in a variable and setting it to the property from a combo box.
 
Ah, I see what happened. Ok, the error isn't the most description but it does make sense.

You created two SharedVariables, SharedFooBase and SharedFootEx. While FooEx inherits FooBase the SharedFooEx class does not implement SharedFooBase. This is why you are getting the error.

Your SharedFooEx class should look like:

Code:
public class SharedFooEx : SharedFooBase
 
I tried it, but accessing the variable fooEx declared in SharedFooEx with fooEx.Value was inconvenient because <T> is a FooBase.

Please consider adding an example to the documentation to show how it is intended to be designed.

Best regards.
 
Last edited:
I tried it, but accessing the variable fooEx declared in SharedFooEx with fooEx.Value was inconvenient because <T> is a FooBase.
I can't think of an alternative for this situation. The only way would be to specify SharedFooEx within your task.
 
Top