Stupid coding errors that cause CTDs! [Please add your own]

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
Hi my name is ww2commander and I am careless programmer. There I admitted it and now I can take my first step towards rehabilitation! :blush:

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 :hammer2:
 
EDIT: This post is incorrect!

I have removed post info. :(
 
Another one to add to the list...

When triggering any function via GameEvents.PlayerCanTrain.Add you must never use 'return' to get out of it as this kills the turn and causes a CTD.

Example:
Code:
GameEvents.PlayerDoTurn.Add(CheckCityIndustrialCapacity)

The above code will trigger the following function:
Code:
function CheckCityIndustrialCapacity(playerID)
[COLOR="Red"]	if GetCivIDFromPlayerID(playerID) ~= USSR then
                return
        end[/COLOR]
	player = Players[playerID]
	
	for city in player:Cities() do
		UpdateIndustrialCapacityManufactories(playerID, city:GetID())
	end
end
What I expected was for the game to ignore just this bit of code if the player civ is not the USSR. Hwever it kills the whole turn instead.

I've definitely done this before without any issues. Excerpt from my latest mod:

Code:
function ArgastWar(playerID)
	if playerID ~= Game.GetActivePlayer() then
		return
	end
        ... other code here ...
end
GameEvents.PlayerDoTurn.Add(ArgastWar)

And it does what you'd expect, ignores the function except for the active player. Also you said PlayerCanTrain earlier in your post, but based on your code and use elsewhere, you meant PlayerDoTurn?
 
I realised that the last post I made is not correct as I noticed the CTD returned :blush:

...and yes, that should be PlayerDoTurn in the first line but I cut/pasted something else during the post and forgot!

Lets just say I was all over the shop late last night when I posted it. I spent hours looking for that CTD!
 
Back
Top Bottom