Which subtables must exist before a tweakdef works, how to loop through faction units and attach weapons, and why table operations in BAR Lua behave unexpectedly.
Tags: bar modding, tweakdefs, buildoptions, customparams, table copy, unitdefs looping, adding weapons lua
Tweakdefs changes fail silently when the subtable you are writing to does not exist yet. The four subtables that cause the most trouble are buildoptions, customparams, weapondefs, and weapons. Before writing into any of them, initialize the table if it is nil.
A safe pattern is to use UD[unitName].weapons = UD[unitName].weapons or {} before calling table.insert on it. The same guard applies to weapondefs with dictionary-style indexing.
To add a weapon across an entire faction, iterate over UnitDefs using pairs(). Use string.sub(unitName, 1, 3) to filter by faction prefix. "arm" covers Arm faction units, "core" covers Core units.
The function structure is straightforward: check or create the weapons and weapondefs subtables, insert a new weapon entry into the weapons array with the definition name and targeting category, then define the weapon under weapondefs. For shields, add a shield field to the weapon definition and handle the repulsor parameters accordingly.
BAR Lua does not behave exactly like standard Lua when it comes to table operations. table.copy() historically did not work as a local cloning function in some contexts, so modders built their own deep copy utilities. If you see references to a dC function in existing mod code, that is a custom deep-copy implementation, often found in mods like Raptors.
For merging tables, table.insert works for arrays but not for dictionary-style tables. Use a manual loop with pairs() to merge key-value tables, or write a small helper function.
Understanding BAR under the hood makes you a sharper player. Creed of Champions welcomes people who are curious about the game and want to share knowledge. Better teammates. Better games. It is a community where players help each other learn, from basic strategy to deeper technical topics. Training sessions, cooperative team play, and zero toxicity. If that sounds like your kind of environment, look them up.
[Crd] One of the few places where you can for sure coordinate with people in matches with a good supportive attitude. Everybody tends to be understanding and constructive.