-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters