Sunday, June 5, 2011

Unity: Loading Levels

What a night. I've got a Machiavellian wisdom tooth with aggressive plans for cranial domination waging war on my face. Luckily, I also have a pack of capsules filled with some incredible magical rainbow potion. My teeth feel fuzzy, in an oddly pleasant kind of way, like they're wrapped in two feet of cotton swaddling.

I can't say I'm in the best mental state for programming. Nevertheless, I got inter-level portals working.

It was an uphill struggle. Understanding code isn't an easy task when I'm prone to drifting off into happyland every time I blink. Most problems have been a result of having the memory span of a goldfish, but I did have some major difficulty with the Application.loadLevel() method. It turns out, this method doesn't actually pause operation or anything. So my code kept doing its thing believing it was operating on the newly loaded level, when it was in fact working on the old.

The Application.loadLevel() action doesn't actually occur until the end of the frame, so the solution was to wrap it up in a coroutine (I love those things!) and then yield, to wait a frame before continuing execution.

I still have another problem to solve before I can publish an example: In building the first level, I went ahead and dumped all of the persistent objects (things like the player and camera) in that level. Now, when I return to the level, it loads duplicates of these persistent objects and screams errors in my face. I need to refactor with a main controller that instantiates the persistent objects only if they don't already exist. But I'm going to go ahead and do that tomorrow. It's getting hard to concentrate with all of the unicorns running across my ceiling.

No comments:

Post a Comment