Two frequent pain points when writing tweakdefs scripts — nil values crashing your logic, and filtering units without tripping over undefined fields.
Tags: BAR modding, tweakdefs, stealth, nil handling, unitdef filtering
If you write if stealth then stealth = true end, that check only works when stealth already exists in the unit's definition table. Units without a stealth field entirely cause the check to fail silently. You cannot modify a value that was never defined. For stealth specifically, this means only units with an explicit stealth property in their original unitdef can have it toggled.
Using ud.stealth = ud.stealth or true works to set a default, but again only if you are checking the right table path. If the field does not exist at all, the assignment creates a new entry that the engine may ignore during gameplay.
When filtering by faction using string matching on unit names, name:find("arm") == 1 correctly catches Armada units at the start of the name. But combining multiple string checks without proper nesting leads to nil pointer errors. Each subtable access needs a nil guard before you read from it. The safest pattern is:
for name, ud in pairs(UnitDefs) do
if string.sub(name, 1, 3) == "arm" then
if ud.customparams then
-- safe to modify customparams
end
end
end
When a tweakdef script silently fails, the infolog.txt in your BAR data folder contains the actual error. Opening it in an editor like VS Code that can read a live file shows errors in real time. Most tweakdef problems are malformed definitions, missing subtables, or nil handling failures — all of which log to the infolog even when the game does not show a visible error message.
For small changes, modifying unit entries in place works fine. When changes get complex — adding new fields, removing weapons, restructuring build menus — it becomes cleaner to use table.copy to create a modified clone and then replace the original definition. The tipping point is usually around three or four distinct modifications. After that, a copy-and-replace pattern is easier to debug than a chain of in-place edits.
Debugging takes patience. The best players share what they learn, including their mistakes. Creed of Champions is a community where teaching and learning happen without judgment. People help each other figure things out, then play better games together.
[Crd] Everyone is nice and kind, the atmosphere is relaxed, and I am not getting yelled at for not being optimal.