{"record":{"id":"bf2956584f8fbc3b","repo":"EllanJiang/GameFramework","slug":"fsm-states-is-invalid","errorCode":null,"errorMessage":"FSM states is invalid.","messagePattern":"FSM states is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Fsm/Fsm.cs","lineNumber":143,"sourceCode":"        }\n\n        /// <summary>\n        /// 创建有限状态机。\n        /// </summary>\n        /// <param name=\"name\">有限状态机名称。</param>\n        /// <param name=\"owner\">有限状态机持有者。</param>\n        /// <param name=\"states\">有限状态机状态集合。</param>\n        /// <returns>创建的有限状态机。</returns>\n        public static Fsm<T> Create(string name, T owner, params FsmState<T>[] states)\n        {\n            if (owner == null)\n            {\n                throw new GameFrameworkException(\"FSM owner is invalid.\");\n            }\n\n            if (states == null || states.Length < 1)\n            {\n                throw new GameFrameworkException(\"FSM states is invalid.\");\n            }\n\n            Fsm<T> fsm = ReferencePool.Acquire<Fsm<T>>();\n            fsm.Name = name;\n            fsm.m_Owner = owner;\n            fsm.m_IsDestroyed = false;\n            foreach (FsmState<T> state in states)\n            {\n                if (state == null)\n                {\n                    throw new GameFrameworkException(\"FSM states is invalid.\");\n                }\n\n                Type stateType = state.GetType();\n                if (fsm.m_States.ContainsKey(stateType))\n                {\n                    throw new GameFrameworkException(Utility.Text.Format(\"FSM '{0}' state '{1}' is already exist.\", new TypeNamePair(typeof(T), name), stateType.FullName));\n                }","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Fsm/Fsm.cs#L125-L161","documentation":"Fsm<T>.Create requires at least one state to construct a usable state machine. The params overload validates states == null || states.Length < 1 at Fsm.cs:143 and throws this exception when the state array is missing or empty.","triggerScenarios":"Fsm<T>.Create(name, owner) with no state arguments, or Fsm<T>.Create(name, owner, (FsmState<T>[])null), or explicitly passing an empty FsmState<T>[0].","commonSituations":"Forgetting to list states in the params call; building the state array dynamically and ending up with an empty array; a config-driven setup where the state list failed to load.","solutions":["Pass at least one FsmState<T> instance to Create.","If states are built dynamically, guard the call: only call Create when the array has Length >= 1."],"exampleFix":"// before\nFsmState<Enemy>[] states = CollectStates(); // may be empty\nvar fsm = Fsm<Enemy>.Create(\"Enemy\", this, states);\n// after\nvar states = CollectStates();\nif (states != null && states.Length > 0)\n{\n    var fsm = Fsm<Enemy>.Create(\"Enemy\", this, states);\n}","handlingStrategy":"validation","validationCode":"if (states == null || states.Length == 0)\n{\n    throw new InvalidOperationException(\"An FSM needs at least one state.\");\n}\nvar fsm = Fsm<Enemy>.Create(\"Enemy\", this, states);","typeGuard":"bool HasStates<T>(FsmState<T>[] states) where T : class => states != null && states.Length > 0;","tryCatchPattern":"try { fsm = Fsm<Enemy>.Create(\"Enemy\", this, states); }\ncatch (GameFrameworkException ex) { Log.Error(\"FSM states invalid: \" + ex.Message); }","preventionTips":["List states inline in the Create call to avoid accidentally empty dynamic arrays.","Validate dynamically built state collections before Create."],"tags":["csharp","fsm","empty-collection"],"backgroundTag":"empty-required-field","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}