{"record":{"id":"2aa47bed06e50235","repo":"OpenRA/OpenRA","slug":"invalid-data-type-for-initinstance-0-key-e","errorCode":null,"errorMessage":"Invalid data type for '{initInstance[0]}.{key}' (expected {type.Name}, got {kv.Value.WrappedClrType()})","messagePattern":"Invalid data type for '(.+?)\\.(.+?)' \\(expected (.+?), got (.+?)\\)","errorType":"exception","errorClass":"LuaException","httpStatus":null,"severity":"error","filePath":"OpenRA.Mods.Common/Scripting/Global/ActorGlobal.cs","lineNumber":61,"sourceCode":"\t\t\tif (value is LuaTable tableValue && init is CompositeActorInit compositeInit)\n\t\t\t{\n\t\t\t\tvar args = compositeInit.InitializeArgs();\n\t\t\t\tvar initValues = new Dictionary<string, object>();\n\t\t\t\tforeach (var kv in tableValue)\n\t\t\t\t{\n\t\t\t\t\tusing (kv.Key)\n\t\t\t\t\tusing (kv.Value)\n\t\t\t\t\t{\n\t\t\t\t\t\tvar key = kv.Key.ToString();\n\t\t\t\t\t\tif (!args.TryGetValue(key, out var type))\n\t\t\t\t\t\t\tthrow new LuaException($\"Unknown initializer type '{initInstance[0]}.{key}'\");\n\n\t\t\t\t\t\tvar isActorReference = type == typeof(ActorInitActorReference);\n\t\t\t\t\t\tif (isActorReference)\n\t\t\t\t\t\t\ttype = kv.Value is LuaString ? typeof(string) : typeof(Actor);\n\n\t\t\t\t\t\tif (!kv.Value.TryGetClrValue(type, out var clrValue))\n\t\t\t\t\t\t\tthrow new LuaException($\"Invalid data type for '{initInstance[0]}.{key}' (expected {type.Name}, got {kv.Value.WrappedClrType()})\");\n\n\t\t\t\t\t\tif (isActorReference)\n\t\t\t\t\t\t\tclrValue = type == typeof(string) ? new ActorInitActorReference((string)clrValue) : new ActorInitActorReference((Actor)clrValue);\n\n\t\t\t\t\t\tinitValues[key] = clrValue;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tcompositeInit.Initialize(initValues);\n\t\t\t\treturn init;\n\t\t\t}\n\n\t\t\tvar initializers = initType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)\n\t\t\t\t.Where(m => m.Name == \"Initialize\" && m.GetParameters().Length == 1)\n\t\t\t\t.ToList();\n\n\t\t\tforeach (var initializer in initializers)\n\t\t\t{","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/OpenRA/OpenRA/blob/a520984d91eda9de48a62b1d15c1e3bad0d4fb1a/OpenRA.Mods.Common/Scripting/Global/ActorGlobal.cs#L43-L79","documentation":"Raised in the CompositeActorInit path when a field key is valid but the Lua value cannot be coerced to the .NET type that field expects. TryGetClrValue fails and the engine reports both the expected type name and the actual wrapped CLR type of the Lua value.","triggerScenarios":"Passing a string to a numeric composite field, a number where an actor reference/table is required, or a LuaValue of the wrong wrapper type. Example: { Facing = { Facing = \"north\" } } where Facing expects an integer angle.","commonSituations":"Lua's dynamic typing letting a string slip into a numeric field; passing a Lua table where a scalar is expected; off-by-format errors like giving a WAngle/number mismatch.","solutions":["Match the Lua value type to the field's expected .NET type (read the error's 'expected X' and convert: tonumber, tostring, or build the expected object).","Use tonumber()/explicit construction in Lua for numeric fields.","For actor references, pass a string actor id or an Actor object per the ActorInitActorReference handling."],"exampleFix":"-- before\nActor.Create(\"e1\", true, { Owner = player, Facing = { Facing = \"north\" } })\n-- after\nActor.Create(\"e1\", true, { Owner = player, Facing = { Facing = 0 } })","handlingStrategy":"type-guard","validationCode":"-- Coerce Lua values to expected types before placing in a composite init table.\nlocal function AsInt(v) return assert(tonumber(v), \"expected number\") end\ninitTable.Facing = { Facing = AsInt(rawFacing) }","typeGuard":"local function IsNumber(v) return type(v) == \"number\" end","tryCatchPattern":null,"preventionTips":["Use tonumber/tostring explicitly when building init values from dynamic input.","Match the documented .NET type for each composite field.","Test init tables with sample values in the Lua console."],"tags":["openra","lua-scripting","actor-init","type-mismatch"],"backgroundTag":null,"analyzedSha":"a520984d91eda9de48a62b1d15c1e3bad0d4fb1a","analyzedAt":"2026-08-13T15:30:14.787Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}