Update API.md example for PersistentVars to check nil to avoid overwriting value #529
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I spent probably more time than I should have trying to figure out why I had trouble getting
PersistentVarsworking, only to eventually realize that the issue was that I was overwriting it unconditionally each time the mod was loaded, which of course caused anything I wrote into it before to be thrown away. In retrospect, it was a bit obvious, but I do think it's a somewhat subtle issue for people to figure out, since it's pretty hard to distinguish between what's going on and what would happen with any global variable without special persistence semantics.In case it might help future developers, I think updating the example snippet to check for nil before initializing would make sense. Presumably a decent number of people start out by copying the examples directly and then updating them as needed, and if I'm not missing something, it seems like the nil check would be pretty much required in order to actually do anything with the variables being persisted.