#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.Variables { using Opsive.BehaviorDesigner.Runtime.Tasks.Actions; using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; /// /// Base class for setting a SharedVariable by name in a selected scope. /// public abstract class SetVariableBase : TargetBehaviorTreeAction { /// /// Specifies the container scope that should be searched. /// public enum VariableScope { Graph, GameObject, Scene, Project } [Tooltip("The scope of the SharedVariable that should be set.")] [SerializeField] protected VariableScope m_VariableScope = VariableScope.Graph; /// /// Returns the SharedVariable with the specified name. /// /// The variable name. /// The SharedVariable with the specified name. protected SharedVariable GetVariable(SharedVariable variableName) { if (variableName == null || string.IsNullOrEmpty(variableName.Value)) { return null; } return GetVariableContainer()?.GetVariable(variableName.Value); } /// /// Returns the typed SharedVariable with the specified name. /// /// The type of SharedVariable. /// The variable name. /// The typed SharedVariable with the specified name. protected SharedVariable GetVariable(SharedVariable variableName) { if (variableName == null || string.IsNullOrEmpty(variableName.Value)) { return null; } return GetVariableContainer()?.GetVariable(variableName.Value); } /// /// Sets the value of the typed SharedVariable. /// /// The type of SharedVariable. /// The variable name. /// The value to set. /// True if the variable was set. protected bool SetVariableValue(SharedVariable variableName, T value) { var destination = GetVariable(variableName); if (destination == null) { return false; } destination.Value = value; return true; } /// /// Resets the task values back to their default. /// public override void Reset() { base.Reset(); m_VariableScope = VariableScope.Graph; } /// /// Returns the container that owns the target variable. /// /// The target variable container. private ISharedVariableContainer GetVariableContainer() { switch (m_VariableScope) { case VariableScope.GameObject: var targetGameObject = (m_TargetGameObject == null || m_TargetGameObject.Value == null) ? m_GameObject : m_TargetGameObject.Value; return targetGameObject != null ? targetGameObject.GetComponent() : null; case VariableScope.Scene: return SceneSharedVariables.Instance; case VariableScope.Project: return ProjectSharedVariables.Instance; default: return m_ResolvedBehaviorTree; } } } } #endif