Inspiaaa / UnityHFSM

A simple yet powerful class-based hierarchical finite state machine for Unity
MIT License
1.13k stars 125 forks source link

ArgumentNullException from AddTransitionFromAny shortcut method #10

Closed stevewoolcock closed 1 year ago

stevewoolcock commented 2 years ago

An ArgumentNullException is thrown when using the AddTransitionFromAny(to, condition, forceInstantly) shortcut extension method for FSMs where TStateId is a string type.

This occurs because the extension method passes default through to CreateOptimizedTransition(), where the default value for a string is null. I'm not sure there's a 'clean' way to fix this, other than adding string-specific extension method variants.

ArgumentNullException: Value cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) (at <cdc4992cc04a4e77a24a09cd121af77b>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) (at <cdc4992cc04a4e77a24a09cd121af77b>:0)
FSM.StateMachine`3[TOwnId,TStateId,TEvent].GetOrCreateStateBundle (TStateId name) (at Assets/_Game/Scripts/Salvage.Core/Runtime/HFSM/StateMachine.cs:322)
FSM.StateMachine`3[TOwnId,TStateId,TEvent].AddTransition (FSM.TransitionBase`1[TStateId] transition) (at Assets/_Game/Scripts/Salvage.Core/Runtime/HFSM/StateMachine.cs:368)
FSM.StateMachineShortcuts.AddTransitionFromAny[TOwnId,TStateId,TEvent] (FSM.StateMachine`3[TOwnId,TStateId,TEvent] fsm, TStateId to, System.Func`2[T,TResult] condition, System.Boolean forceInstantly) (at Assets/_Game/Scripts/Salvage.Core/Runtime/HFSM/StateMachineShortcuts.cs:84)
Inspiaaa commented 2 years ago

Thanks a lot for reporting the bug and your detailed report with a solution proposal. 👍

@mfandreich had a very clean solution to this problem. I have not not merged his solution yet, because it is part of a larger pull request that I‘m working on. If you need a quick fix in the meantime, please see https://github.com/Inspiaaa/UnityHFSM/pull/9/commits/f68818f764b3af869db144398d38aebbcb7dd5b2

JoelCreatesGames commented 2 years ago

Also ran into this (I ended up using the example from the docs and that works fine)