Protect Your Plant -- Loading Screens

A bit of fit and finish

Posted January 13 2022 9:31am

My wife's family put together a short vacation post-Christmas when they could all be together, and we spent four days in a cabin playing games and cooking amazing food for each other. While there, I decided it'd be nice to try to make a small feature I've been wanting to add to the game for a while, sort of as a souvenir of the trip. The biggest surprise was how easily it all went together.

When I first worked out level changes in Protect Your Plant, I knew there was existing code and tutorials for loading screens, but settled for setting up someone's level change code in my global script and calling Global.goto_scene("res://Level_Loading.tscn") for example. The result was that it worked, but the game locked up solid while it loaded the new level, which always bugged me a little.

I ended up taking the code from https://godotengine.org/asset-library/asset/530, pasting it into a new scene, giving it a progressbar for a child, and creating a new variable in the global script called destination_level. Then, because there's nothing I can't kludge, I just called my existing goto_scene function to transition to the loading screen scene.

I think the only modification I made to the example code was to add a ready(): function and add load_scene(Global.destination_level) to it. My previous setup already had each door track where on the destination level it had to go by changing the global.startpos variable, so I just had it add what level to go to as well.

I think I also got an error and changed get_tree().current_scene.free to get_tree().current_scene.call_deferred('free'). This seemed to resolve it.

The loading screen was now working great, and I spent some time adding a placeholder sprite, and an array of hints and lore to randomly display during loading. Then I tried moving from level to level and found that I had an issue with following the new level_loading script with the origional goto_scene code (which always happened, because goto_scene called level_loading, so if you loaded one level, then tried to transition to the next you got goto_scene->level_loading->goto_scene->error.

The issue looked like it involved threads and was going to take me a while to figure it out, when my brother in law looked at my code, opined that the local variable current_scene seemed out of place, and should be standardize on get_tree().current_scene. I honestly couldn't tell you why I had it set up the other way before, it's entirely possible I understood this code when I implemented it, but it's been a while and I make no promises. Either way, it worked, and saved me a ton of debugging time I used for drinking and board games instead.

So there's the progress on loading screens. A nice little bit of fit and finish for the game, and a nice souvenir from a great visit with family. I'd like to upgrade the sprites with more painterly renders of the plantbuddy, possibly based on what kind of terrain the player is crossing during the level transition (like stairs, for example) or maybe just nice renders of the game world for lore building purposes. But as with most things, I'm settling for functional and good enough, and will add another layer of polish when there's more of a real game working here.