{"record":{"id":"09a5fb43313fa435","repo":"OpenRA/OpenRA","slug":"cvec-is-read-only-use-cvec-new-to-create-a-new-va","errorCode":null,"errorMessage":"CVec is read-only. Use CVec.New to create a new value","messagePattern":"CVec is read-only\\. Use CVec\\.New to create a new value","errorType":"exception","errorClass":"LuaException","httpStatus":null,"severity":"error","filePath":"OpenRA.Game/CVec.cs","lineNumber":140,"sourceCode":"\t\t\t\t\t$\"({left.WrappedClrType().Name}, {right.WrappedClrType().Name})\");\n\n\t\t\treturn new LuaCustomClrObject(a / b);\n\t\t}\n\n\t\tpublic LuaValue this[LuaRuntime runtime, LuaValue key]\n\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\tswitch (key.ToString())\n\t\t\t\t{\n\t\t\t\t\tcase \"X\": return X;\n\t\t\t\t\tcase \"Y\": return Y;\n\t\t\t\t\tcase \"Length\": return Length;\n\t\t\t\t\tdefault: throw new LuaException($\"CVec does not define a member '{key}'\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tset => throw new LuaException(\"CVec is read-only. Use CVec.New to create a new value\");\n\t\t}\n\n\t\tpublic LuaValue ToString(LuaRuntime runtime) => ToString();\n\n\t\t#endregion\n\t}\n}\n","sourceCodeStart":122,"sourceCodeEnd":148,"githubUrl":"https://github.com/OpenRA/OpenRA/blob/a520984d91eda9de48a62b1d15c1e3bad0d4fb1a/OpenRA.Game/CVec.cs#L122-L148","documentation":"Thrown from CVec's Lua table indexer (set) when any attempt is made to assign a value to a CVec member. CVec is a readonly struct: its X and Y fields are immutable after construction. The Lua interface throws this LuaException to direct the developer to use CVec.New.","triggerScenarios":"In a Lua script, writing vec.X = 5 or vec.Y = vec.Y + 1 on an existing CVec. All setter access on CVec via Lua is forbidden.","commonSituations":"Script authors try to mutate vectors in place, forgetting that OpenRA coordinate types are value types designed for immutability.","solutions":["Create a new CVec using CVec.New(x, y) with the desired values, then reassign the variable.","Use arithmetic operators (vecA + vecB, vec * n, -vec) to compute new vectors from existing ones."],"exampleFix":"-- before\nvec.X = vec.X + 1 -- error: read-only\n\n-- after\nvec = CVec.New(vec.X + 1, vec.Y) -- create a new CVec","handlingStrategy":"validation","validationCode":"-- Never attempt to set members; always construct new values\nlocal new_vec = CVec.New(5, vec.Y)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat CVec as immutable - never assign to members.","Use CVec.New(x, y) to create modified copies.","Use arithmetic operators (vec * n, vec + vec2) to derive new vectors."],"tags":["cvec","lua","scripting","openra","immutable","readonly"],"backgroundTag":null,"analyzedSha":"a520984d91eda9de48a62b1d15c1e3bad0d4fb1a","analyzedAt":"2026-08-13T15:30:14.787Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}