Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Resolved I can't actually affect trait's data from custom effect

Discussion in 'AI & Navigation Previews' started by Noxalus, Sep 29, 2020.

  1. Noxalus

    Noxalus

    Joined:
    Jan 9, 2018
    Posts:
    80
    Hello everyone,

    I'm trying to use a custom effect as described in the documentation to decrease values from a Needs trait that contains 3 fields:

    upload_2020-9-29_14-41-54.png

    So I've made this class:

    Code (CSharp):
    1. using Unity.AI.Planner.DomainLanguage.TraitBased;
    2. using Generated.AI.Planner.StateRepresentation;
    3. using Generated.AI.Planner.StateRepresentation.AgentPlan;
    4. using UnityEngine;
    5.  
    6. public struct NeedsEffect : ICustomActionEffect<StateData>
    7. {
    8.     public void ApplyCustomActionEffectsToState(StateData originalState, ActionKey action, StateData newState)
    9.     {
    10.         TraitBasedObjectId moverObjectId = newState.GetTraitBasedObjectId(action[0]);
    11.         TraitBasedObject moverObject = newState.GetTraitBasedObject(moverObjectId);
    12.         Needs needs = newState.GetTraitOnObject<Needs>(moverObject);
    13.  
    14.         needs.Energy = Mathf.Max(0, needs.Energy - 1);
    15.         needs.Fun = Mathf.Max(0, needs.Fun - 1);
    16.         needs.Restroom = Mathf.Max(0, needs.Restroom - 1);
    17.     }
    18. }
    And I affected this effect to a Move action like that:

    upload_2020-9-29_14-44-30.png

    But it doesn't seem to work if I look at the graph in the plan visualizer.

    From the state selected (before branching), I expect the needs (highlighted in red from the screen) to decrease by one in the next state if the "Move" action is used:

    upload_2020-9-29_14-52-24.png

    But only the Energy decreases (the last effect from the Move action asset file):

    upload_2020-9-29_14-52-28.png

    Do you know why? Did I do something wrong?

    N.B: I generated the AI Planner assemblies with no error. No error neither in the console at runtime. I made a small project to show the issue here.
     
  2. TrevorUnity

    TrevorUnity

    Unity Technologies

    Joined:
    Jun 28, 2017
    Posts:
    118
    You're modifying a local copy of the struct. You need to set the updated version back on the new state.
     
    Noxalus likes this.
  3. Noxalus

    Noxalus

    Joined:
    Jan 9, 2018
    Posts:
    80
    My bad, that was a dumb mistake... Thank you for your help, here is the fixed code:

    Code (CSharp):
    1. using Unity.AI.Planner.DomainLanguage.TraitBased;
    2. using Generated.AI.Planner.StateRepresentation;
    3. using Generated.AI.Planner.StateRepresentation.AgentPlan;
    4. using UnityEngine;
    5.  
    6. public struct NeedsEffect : ICustomActionEffect<StateData>
    7. {
    8.     public void ApplyCustomActionEffectsToState(StateData originalState, ActionKey action, StateData newState)
    9.     {
    10.         TraitBasedObjectId moverObjectId = newState.GetTraitBasedObjectId(action[0]);
    11.         TraitBasedObject moverObject = newState.GetTraitBasedObject(moverObjectId);
    12.         Needs needs = newState.GetTraitOnObject<Needs>(moverObject);
    13.  
    14.         needs.Energy = Mathf.Max(0, needs.Energy - 1);
    15.         needs.Fun = Mathf.Max(0, needs.Fun - 1);
    16.         needs.Restroom = Mathf.Max(0, needs.Restroom - 1);
    17.  
    18.         newState.SetTraitOnObject(needs, ref moverObject);
    19.     }
    20. }