Using Invoke Method with custom class

dfego

New member
I'm currently looking at using the Invoke Method action to fetch some data within a class using Behavior Designer. Only one method in this class (other than presumably builtin ones by Unity) is shown by default, and that method has a void return and no parameters. Based on the following thread:

https://opsive.com/forum/index.php?threads/invoke-method-doesnt-show-some-method.419/

I'm led to believe that I need to create a SharedVariable for the type I'd like to return, so I did that. My class is called Item and I created a class called SharedItem:

C#:
[System.Serializable]
public class SharedItem : SharedVariable<Item>
{
    public static implicit operator SharedItem(Item value) { return new SharedItem { Value = value }; }
}

After adding this, I still don't see my method in Behavior Designer. Am I supposed to have my method change to return SharedItem instead of Item, or should it work with my method like so: public Item GetItem() { ... }? I would expect I could use Item, because, for example, I can use int instead of SharedInt (which does exist).

Either way I haven't found much documentation on this topic, so more explicit documentation on how to hook all this up would be helpful.

Thanks in advance for any assistance!
 
Last edited:
Creating that SharedVariable should be enough - as a test can you create a new class and see if this method shows up?

Code:
public class MyClass : MonoBehaviour
{
   public int GetValue() { return 0; }
}
 
Thanks for the response! Okay, so in the example you gave, I created MyClass.cs containing your code, then created an empty GameObject, added MyClass and BehaviorTree to it, and MyClass + GetValue were available for the Invoke Method action. However, this doesn't yet match my scenario. So I created the following in MyContainerClass.cs:

C#:
using UnityEngine;

public class MyContainerClass : MonoBehaviour
{
    public int GetValue() { return 0; }

    public MyClass GetClass() {
        return null;
    }
}

And then also have the following in MyClass.cs:

C#:
using BehaviorDesigner.Runtime;
using UnityEngine;

public class MyClass : MonoBehaviour
{
    public int GetValue() { return 0; }
}

[System.Serializable]
public class SharedMyClass : SharedVariable<MyClass>
{
    public static implicit operator SharedMyClass(MyClass value) { return new SharedMyClass { Value = value }; }
}


On the GameObject, I can't use MyContainerClass + GetClass, which closely mirrors my end-goal. I have a class that has a method that returns another custom type I define.

I'll continue to try anything else you like, I just wanted to try to get a more complete example of what I expect to work written down so it might be easier to see what I may be doing wrong!
 
You are doing things correctly - looks like there was a bug in the editor for showing the methods. The problem exists because MyClass derives from MonoBehaviour and there is a little bit of a different handling for Unity serialized classes. I've fixed this though and can send you a new version - what version of Unity are you using?
 
Oh wow, I'm surprised both that I stumbled across this and that you've already got a fix for it! I'm currently on the latest mainline version of Unity, 2019.1.3f1. Once you send it along I'll confirm it fixes the issue I'm seeing. Thank you!
 
Top