ww2commander
Emperor
Hi my name is ww2commander and I am careless programmer. There I admitted it and now I can take my first step towards rehabilitation! 
I thought I would start a thread to identify stupid and careless coding errors that cause a crash to desktop!
This morning I wasted some time troubleshooting some code that kept CTD due to my own carelessness (and it's not the first time I have done this). Hopefully this thread will be a reminder to me and enlighten those learning how to code in lua on what 'not to do' when coding!
Dumb code example 1 (100% money-back guarantee to CTD):
The code above looks simple enough, until it gets to the last player in the loop and then tries to look for another 'alive' player. It can't find it and returns a null value to 'player'. The offending line of code is the 'print' statement in red. What happens when the game tries to get the name for a null player?.... a CTD!
Simply moving the print statement into the 'if' statement further below prevents the CTD. Simple, yet I have been guilty of doing this too many times to remember (or you would think I would remember!!!!)
Dumb things we do when not paying attention

I thought I would start a thread to identify stupid and careless coding errors that cause a crash to desktop!
This morning I wasted some time troubleshooting some code that kept CTD due to my own carelessness (and it's not the first time I have done this). Hopefully this thread will be a reminder to me and enlighten those learning how to code in lua on what 'not to do' when coding!
Dumb code example 1 (100% money-back guarantee to CTD):
Code:
for playerID = 0, GameDefines.MAX_CIV_PLAYERS - 1 do
player = Players[playerID]
[COLOR="Red"]print("Processing player "..player:GetName())[/COLOR]
if player:IsAlive() and not player:IsMinorCiv() then
-- Do the code stuff here!
end
end
The code above looks simple enough, until it gets to the last player in the loop and then tries to look for another 'alive' player. It can't find it and returns a null value to 'player'. The offending line of code is the 'print' statement in red. What happens when the game tries to get the name for a null player?.... a CTD!
Simply moving the print statement into the 'if' statement further below prevents the CTD. Simple, yet I have been guilty of doing this too many times to remember (or you would think I would remember!!!!)
Dumb things we do when not paying attention
