Hey Folks! I think I finally got the batteries behaving, and I'm back on the reactor again.
I decided to bite the bullet and just write code to handle power distribution on the ship. Puzzling out the Rube Goldberg machine of conditions and triggers to do what is basically a heartbeat function was getting to be both too much of a headache and too unreliable. So now, all my powered items just update on the same heartbeat as other conditions, and the nitty-gritty power use/storage/transmission stuff is all handled in code.
And wow, was it much easier to understand this way. Note to self for future systems. Hack it out in code first, and only bother with data-driven everything later (if ever). Especially while prototyping.
So now, my powered items draw power from all connected batteries in parallel (like Rimworld), and switch off when the needed power isn't available.
Once that was running, I resumed work on getting a working reactor into the ship. Initially, this seemed pretty simple. Just add some stats to the reactor data to control how much it generates, and generate that amount each heartbeat. Any attached items can draw from that generated power, and at the beginning of each heartbeat, I can bleed off any leftover energy to start the next cycle.
I even added a recharging function, which is basically the reverse of the power consumption code, to recharge all attached batteries in parallel.
And one last thing: change the power stat from a single precision to double precision variable, since the difference between the reactor's power level and a light bulb is so vast, a single precision might not notice the tiny difference.
All was pretty straightforward, but then I hit a snag when I fired it up: the json data reader doesn't like double precision! For some reason, it thinks it is a single, and cannot convert it.
It's a really weird bug, in fact. That sort of type casting is supposed to be easy in C#, but I'm using some third party json reader that I had to hack to get working with +/- infinity values. So maybe I broke it, or maybe it wasn't setup for single/double correctly.
However, I notice that Unity has a json reader/writer now. Maybe it was always there? Maybe it's new since the last time I was writing this code (over a year ago!). In any case, I have a date with json tomorrow!