This morning I was working on my new ReplicaManager for RakNet and was stuck with a bug where existing objects were not sent to new systems, because I set the scope of the object in the constructor to be true for all players. The problem was that players that connected later would have the default scope of false, and didn’t get data. So I quickly wrote down a few solutions:
My first idea was to indicate in the comments that the user needs to be aware of this, and don’t set the scope in the constructor. Lame.
My second idea was to have a callback called for all new players, where users could do this. Annoying, because users have to then write a callback, which complicates the system and is out of context.
My third idea was to have a callback for all players and for objects, which is called once for every permutation. In this callback you could put your scoping code.
If I had been sitting in front of the computer at work I would have done just that. But since I was at home, and since I wasn’t happy with that solution, I went to go take a shower instead.
15 minutes later, I race out of the shower, re-open Visual Studio, and write down an even better idea – I already have the third solution in the object serialization pushes to other systems! This has to happen once per object anyway, so if I just had the user put the code in there I don’t have to write any new functions and the user can do the call with full contextual information. Happy with myself, I program that in about 5 minutes and close Visual Studio again.
I go to wash dishes before going to work. Practically as soon as I turn on the water, another idea hits me, which I would have never thought of at work, in front of the computer. If I have a function to automatically set the scope for new objects, the users don’t have to do anything at all! I write down that solution and go back to washing dishes. No sooner than is my first dish washed that I find a problem with my last solution, which would have been a bug had I been at the computer and implemented this.
So the final solution is to have a single function where users can automatically set scope, and when objects go in scope they are automatically serialized to remote systems. I get to work, program this in about 5 minutes, and it compiles the first time.
My current solution is very easy to use and fits very nicely with the existing architecture. In fact I only had to write about 10 lines of code. I ‘programmed’ this entire solution while taking a shower and washing dishes It only adds one optional function call to the manager that only needs to be called once.
My third solution, which is what I would have programmed had I been at work, in front of the computer, would have taken a few dozen lines of code, would have required that the user write and register a callback, and that callback wouldn’t have any useful purpose other than to try to make the user’s code work within my manager’s architecture.
Seems like a good argument for telecommuting!