Protect Your Plant

Dialogue Tree Improvements

Originally posted Apr 1, 2020, 9:57 AM

The dialogue tree is one of the most complicated parts of Protect Your Plant. Or maybe it feels that way to me because it started out as someone else's code. When I first stated on this feature, it seemed like my options in Godot were fairly limited. There were a few examples of home-built dialogue trees out there, as well as a couple plugins that hadn't been updated in a year or so. Godot had just had a major update and was anticipating another one (which has since happened). All the options seemed to already be out of date, so I picked the one that was also simple enough that I thought I could maintain it myself. This was the example dialogue tree built by TheHappieCat shown here.

Her version did just about everything I needed to do, stored everything in JSON files I could parse, and generally worked. There were only two problems: The website she'd used to generate the JSON files for the conversation had shut down since she posted the video, and all her code was written a for a version of Godot behind the one I was using, with a lot of older syntax. So after going through it, learning it well enough to update it until it ran, and making a few quality of life improvements, I mostly forgot how it worked and treated it like a black box. And that's the story of how I've spent the last year writing all the dialogue manually into the JSON files.

The original code was just a one-off example of what you could do with Godot though, and didn't have all the features I wanted yet. For example, as far as I can tell, the only choices it tracks are whether you interact with an object or talk to a person, not the choices you make within the dialogue itself. So I recently went back, found where the player's choice surfaced in the code as they made them, and wrote them into the global choices dictionary, which gets saved when the rest of the game does. So now every choice the player makes is recorded and I can write future functionalities to use them.

While I was doing that, I hackishly (as in hack-work, not in a cool hacker way) added an if statement to check whether the option was a ">> Yes" or ">> No" and had those change a Decisions entry in the Choices dict, and call a function in the node the player was interacting with. (I just used TheHappieCat's original Action function with an if statement to check that Decisions value.) This let the code support things like ash pile in the above video, where the dialogue tree is used to decide whether to use something found in the game world.

The main goal of adding this was to support these fertilizers -- sun and water are fairly self-explanatory I think, but different fertilizers in the game will provide different balances of Nitrogen, Phosphorous, and Potassium (N-P-K) and different plants will need different balances of them, so I wanted to make sure the game explained what each would do before the player committed to it.

With that said, I think there should be a few other places this sort of thing could be useful.