Skip to content

Commit

Permalink
Add custom state actions
Browse files Browse the repository at this point in the history
Fixes #3

Implement custom state actions to enhance the flexibility and functionality of the state machine.

* **Custom Action Interface**:
  - Add `IStateAction` interface with `OnEnter`, `OnExit`, and `OnTransition` methods in `StateMachine/Assets/StateMachine/IStateAction.cs`.
* **State Class Modifications**:
  - Add a list of `IStateAction` to `NewState` class in `StateMachine/Assets/StateMachine/NewState.cs`.
  - Modify `NewState` constructor to initialize the list of `IStateAction`.
* **State Machine Modifications**:
  - Add methods to execute custom actions on state enter, exit, and transition in `StateMachine/Assets/StateMachine/NewestStateMachine.cs`.
  - Modify `SetState` method to call custom actions.
* **Custom Action Implementation**:
  - Implement `CustomAction` class inheriting from `MonoBehaviour` and `IStateAction` in `StateMachine/Assets/StateMachine/CustomAction.cs`.
  - Define `OnEnter`, `OnExit`, and `OnTransition` methods with custom behaviors.
* **UI Integration**:
  - Create a UI script to attach custom actions to states in `StateMachine/Assets/StateMachine/StateMachineUI.cs`.
  - Provide methods to define specific behaviors for each action.
* **Exception Handling**:
  - Add exception handling in `SetData` method in `StateMachine/Assets/Tool Box #2/Data/BoolData.cs`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/MarquisMc/StateMachine/issues/3?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
MarquisMc committed Oct 22, 2024
1 parent 31b6793 commit 4ee8aaa
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 4 deletions.
21 changes: 21 additions & 0 deletions StateMachine/Assets/StateMachine/CustomAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomAction : MonoBehaviour, IStateAction
{
public void OnEnter()
{
Debug.Log("Entering state...");
}

public void OnExit()
{
Debug.Log("Exiting state...");
}

public void OnTransition()
{
Debug.Log("Transitioning state...");
}
}
10 changes: 10 additions & 0 deletions StateMachine/Assets/StateMachine/IStateAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IStateAction
{
void OnEnter();
void OnExit();
void OnTransition();
}
5 changes: 4 additions & 1 deletion StateMachine/Assets/StateMachine/NewState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public class NewState

public List<StateTransition> stateTransitions = new List<StateTransition>();

public List<IStateAction> actions = new List<IStateAction>();

public NewState(string stateName, StateExtension stateScript)
{
this.stateName = stateName;
this.stateScript = stateScript;
this.actions = new List<IStateAction>();
}
}

Expand All @@ -25,4 +28,4 @@ public class StateTransition
{
public string stateTransitionName;
public BoolData boolData;
}
}
51 changes: 50 additions & 1 deletion StateMachine/Assets/StateMachine/NewestStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Start()
currentState = states.Contains(states[0]) ? states[0] : null;
orginalState = currentState;
stateDictionary[currentState].enabled = true;
ExecuteOnEnterActions(currentState);
}
}

Expand Down Expand Up @@ -72,12 +73,14 @@ public void SetState(NewState newState)

if (newState != currentState)
{
ExecuteOnExitActions(currentState);
previousState = currentState;
currentState = newState;

stateDictionary[previousState].enabled = false;
stateDictionary[orginalState].enabled = false;
stateDictionary[currentState].enabled = true;
ExecuteOnEnterActions(currentState);
}
}

Expand Down Expand Up @@ -113,6 +116,7 @@ void StateTransition()
{
if (stateTransition.boolData.GetData())
{
ExecuteOnTransitionActions(currentState);
SetState(states.Find(state => state.stateName == stateTransition.stateTransitionName));
stateTransition.boolData.SetData(false);
}
Expand All @@ -121,6 +125,51 @@ void StateTransition()

}

void ExecuteOnEnterActions(NewState state)
{
foreach (var action in state.actions)
{
try
{
action.OnEnter();
}
catch (System.Exception ex)
{
Debug.LogError($"Error executing OnEnter action: {ex.Message}");
}
}
}

void ExecuteOnExitActions(NewState state)
{
foreach (var action in state.actions)
{
try
{
action.OnExit();
}
catch (System.Exception ex)
{
Debug.LogError($"Error executing OnExit action: {ex.Message}");
}
}
}

void ExecuteOnTransitionActions(NewState state)
{
foreach (var action in state.actions)
{
try
{
action.OnTransition();
}
catch (System.Exception ex)
{
Debug.LogError($"Error executing OnTransition action: {ex.Message}");
}
}
}

// Update is called once per frame
void Update()
{
Expand Down Expand Up @@ -175,4 +224,4 @@ void Update()
// EditorUtility.SetDirty(stateMachine);
// }
// }
// }
// }
59 changes: 59 additions & 0 deletions StateMachine/Assets/StateMachine/StateMachineUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StateMachineUI : MonoBehaviour
{
public Dropdown stateDropdown;
public Button addActionButton;
public InputField actionInputField;
public NewestStateMachine stateMachine;

void Start()
{
PopulateStateDropdown();
addActionButton.onClick.AddListener(AddActionToState);
}

void PopulateStateDropdown()
{
stateDropdown.ClearOptions();
List<string> stateNames = new List<string>();
foreach (var state in stateMachine.states)
{
stateNames.Add(state.stateName);
}
stateDropdown.AddOptions(stateNames);
}

void AddActionToState()
{
string selectedStateName = stateDropdown.options[stateDropdown.value].text;
NewState selectedState = stateMachine.states.Find(state => state.stateName == selectedStateName);

if (selectedState != null)
{
string actionName = actionInputField.text;
IStateAction newAction = CreateAction(actionName);
if (newAction != null)
{
selectedState.actions.Add(newAction);
Debug.Log($"Action {actionName} added to state {selectedStateName}");
}
else
{
Debug.LogError($"Failed to create action: {actionName}");
}
}
}

IStateAction CreateAction(string actionName)
{
// Here you can implement logic to create different types of actions based on the actionName
// For simplicity, we will just create a CustomAction
GameObject actionObject = new GameObject(actionName);
CustomAction customAction = actionObject.AddComponent<CustomAction>();
return customAction;
}
}
11 changes: 9 additions & 2 deletions StateMachine/Assets/Tool Box #2/Data/BoolData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ public class BoolData : DataBase
// Set the bool
public void SetData(bool newData)
{
data = newData;
//Debug.Log(data);
try
{
data = newData;
//Debug.Log(data);
}
catch (System.Exception ex)
{
Debug.LogError($"Error setting data: {ex.Message}");
}
}

// when a bool is true turn another bool false
Expand Down

0 comments on commit 4ee8aaa

Please sign in to comment.