Skip to content

Added feature to support optional priority for transitions#27

Open
advra wants to merge 1 commit intoprooheckcp:mainfrom
advra:feature/optional-priority-for-transitions
Open

Added feature to support optional priority for transitions#27
advra wants to merge 1 commit intoprooheckcp:mainfrom
advra:feature/optional-priority-for-transitions

Conversation

@advra
Copy link

@advra advra commented Apr 6, 2025

📝Description

Before this change, transitions are treated with random ordering with no guarantee of deterministic behavior. Creating complex State Machine Diagrams results in behavior that may be hard to reproduce. This also prevents extending behaviors with specific use case.

This change adds Optional Priorities Transitions. Transitions are defined like before but can also be defined with an optional Priority value. Upon initialization, a list of sorted transitions is built then used when evaluating states.

This solution is performant and satisfies the original requirement Time Complexity O(1) look up.

How to use:
In this example we have an Idle State with various Transition Outputs defined by priorities. We define Transitions as usual with an optional priority number. If no priority is defined then the fallback default priority of 0 is used instead.

Note: If a state has multiple Transitions defined with no priority specified the order may vary per runtime (ie CanFollow and RandomEmote in the example below). Under the hood each Transition id is randomly generated therefore theres no guarantees on its order.

local ReplicatedStorage = game.ReplicatedStorage
local RobloxStateMachine = require(ReplicatedStorage.RobloxStateMachine)
local State = RobloxStateMachine.State
local Idle = State.new("Idle")

Idle.Transitions = {
	{require(ReplicatedStorage.AI.Transitions.AttackedByPlayer),3},   -- highest priority, this is always evaluated first
	{require(ReplicatedStorage.AI.Transitions.CanWander), 1},         -- Note: Ordering does not matter as the priority number value is used
	{require(ReplicatedStorage.AI.Transitions.SeesPlayerNearby),2},   
        require(ReplicatedStorage.AI.Transitions.CanFollow),              -- default 0 priority 
        require(ReplicatedStorage.AI.Transitions.RandomEmote),            -- default 0 priority 
}

function Idle:OnEnter(data)
	print("Entered Idle")
end

return Idle

🔧Changes

  • Added method _BuildTransitionsExecutionOrder to sort priorities (occurs only once upon init)
  • Added support for Priority number value when defining Transitions in a State
  • Ensures legacy support when no defined priority is needed

👣Next steps

No further steps are necessary

@prooheckcp
Copy link
Owner

Hi, I just wanted to let you know that I didn't drop this project I'm just wrapped in too many things right now (applying for graduate programs right now). Once things cool down I'll look into all of this in detail. Thank you for taking the time for this

@advra
Copy link
Author

advra commented Apr 7, 2025

No worries. I appreciate for getting back to me. When you are ready let me know I will gladly follow up on this when you have the time. (:

Also, good luck in grad school!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants