Possible bug with editor variables changing upon click

mostlyhuman

Member
Here is a simplified version of the code producing the issue:

C#:
public class NPCController : MonoBehaviour
{
    [ReadOnly] [SerializeField] protected float loseInterestDistance;
     protected SharedFloat loseInterestDistanceBT;

    InEditorInspectorButtonMethod()
    {
        behaviorTree = GetComponent<BehaviorTree>();
        loseInterestDistanceBT = (SharedFloat)behaviorTree.GetVariable("loseInterestDistance");

        loseInterestDistance = NPCTemplate.LoseInterestDistance;
        loseInterestDistanceBT.Value = loseInterestDistance;
        behaviorTree.SetVariable("loseInterestDistance", loseInterestDistanceBT);
    }
}

I press a button in the editor inspector that loads values from the NPCTemplate (which is a scriptable object) by calling the InEditorInspectorButtonMethod(), upon pressing the button and running the in editor method the value for loseInterestDistance inside the Behavior Designer Editor will still show 0, but as soon as I hover my mouse over the Behavior Designer Editor window the value will change to what is set in the template, such as 20 (just be moving my mouse from the inspector over to the BT editor). But then if I actually click anywhere in the BT editor window the value immediately reverts back to 0.

I can send a repro project if needed or if you want anymore details or specifics just lemme know. Cheers!
 
Here is a simplified version of the code producing the issue:

C#:
public class NPCController : MonoBehaviour
{
    [ReadOnly] [SerializeField] protected float loseInterestDistance;
     protected SharedFloat loseInterestDistanceBT;

    InEditorInspectorButtonMethod()
    {
        behaviorTree = GetComponent<BehaviorTree>();
        loseInterestDistanceBT = (SharedFloat)behaviorTree.GetVariable("loseInterestDistance");

        loseInterestDistance = NPCTemplate.LoseInterestDistance;
        loseInterestDistanceBT.Value = loseInterestDistance;
        behaviorTree.SetVariable("loseInterestDistance", loseInterestDistanceBT);
    }
}

Hello, as usual, I passed by. :sneaky:

May be there is an issue with behavior editor, I don't know. But this code, man.. almost every line raises questions.

- Why loseInterestDistance has ReadOnly attribute? I hope you know exactly what it does, because I don't. According to manual, it has to do with native containers and job system... Or is it some kind of custom attribute you made?

- Why loseInterestDistance needs to be serialized and why it exists at all? I'm assuming only to display the value in the inspector. But you can check this value in NPCTemplate and inside the tree. Anyway, this shouldn't be the cause.

- What's the purpose of having loseInterestDistanceBT inside the class, if you getting it from the tree each time you need it? Make it a local variable inside the method, or just don't use it at all.

- What is the purpose of the last line? It suggests that you haven't read the manual and lack understanding of how shared variables work.. By assigning loseInterestDistanceBT = GetVariable("loseInterestDistance") you don't create a new object. Instead, loseInterestDistanceBT becomes the reference to the same object, which is referenced by "loseInterestDistance" inside the tree. You change it's Value, and that's it. Just the basics of programming.. You don't want to call SetVariable() here.

Try this, maybe the problem will disappear?

C#:
InEditorInspectorButtonMethod()
{
    var behaviorTree = GetComponent<BehaviorTree>();
    ((SharedFloat)behaviorTree.GetVariable("loseInterestDistance")).Value = NPCTemplate.LoseInterestDistance;
}
 
To improve performance Behavior Designer only redraws every frame when it has focus. It sounds like the window isn't refreshing so the value doesn't update, but then as soon as it has focus it'll update. I have tried out force refreshing it but that just causes more issues as you see a huge spike in redrawing time.
 
When I assign the value in the editor through my script and then hit play it still remains at 0. Any ideas there? Or do I just need to have it assign at runtime only? What is strange is it will update to 20 after i hover on it, but then for some reason it just reverts back to 0 if i click anywhere in the BT editor.
 
Last edited:
Top