Thursday, January 28, 2016

Overnight Coding Solutions

I have stated this before, but I will state it again.. and probably again and again.

I do some of my best coding while I am asleep!

Sounds weird doesn't it. Lets take last night as an example. I altered the way a certain system works in Very Bunny Haha. I used to have each NPC that you touched register themselves into a list using the OnTriggerEnter2D(), and then unregister with the OnTriggerLeave2D(). Somehow I was occasionally getting ghosts left in the list when combinations of the same NPC names touched and untouched the player. I really felt this was not the best way to do this and so wanted to go to a more live list instead, using the OnTriggerStay2D() function. Then each frame they register themselves and I simply have to clear the list at the beginning (or end) of each frame.

When I changed the code to do this it appeared to be working perfectly, but I knew there would be a bug somewhere from such a fundamental change. I never expected to find it where I did, duh! Terrible tester am I! haha. The bug turned up when I tried to accept a quest. I could trigger the quest offer popup but could not accept it as the list was somehow empty when it ran through that piece of code. I was clearing the list at the end of the Update() function for my player. I felt this should be ok, but it always felt dangerous.

Last night I found this issue and though I was tired from a hard days work and an excellent tennis match in the evening, I felt this should be solvable in an easy way. Nope, not at all. While I watched some TV at the same time as trying to solve the issue I failed. Went to bed instead as I was not getting anywhere after looking at it for like 1.5 hours.

This morning I awake with knowledge of how to solve it. All I had to do was move the clear to a better place in the code. Before all the Updates are called and before Events are handled. Well that seems easy... So I moved the clear to the beginning of my FixedUpdate() for my player and now it all works. The only questions remains... is there another bug waiting for me to find, hidden, seething, ready to pounce when I least expect it?

Have a great Sleep Coding night everyone :)
Da Voodoochief

No comments:

Post a Comment