Info
This post was imported from a personal note. It may contain inside jokes, streams of consciousness, errors, and other nonsense.
#include <iostream>
#include "agent.hpp"
Angle brackets are generally for system includes and the compiler will look in system folders and the include paths first. Double quotes are for application headers and the local directory will be searched first.
Boid storing a reference to the simulation #
Boids need to know which simulation they’re in so they can get information like which food sources are closest. I considered passing the simulation into the step function but that’s limiting and doesn’t work when the boid renderer tries to get information about the simulation when it’s drawing.
void Boid::step(Simulation & sim, float timeDelta) { ... }
So now I’m looking to change this:
class Boid {
Simulation *simulation;
public:
Boid(Simulation *sim) {
simulation = sim;
}
}
…to something with smart pointers.
I don’t want boids to keep their simulation alive. If the simulation has been destroyed then its boids should be destroyed, too. (Something is probably wrong if they’re not.)
So weak_ptr makes sense here. Now how do I pass the simulation into a function and assign it to the weak pointer?
Here’s a useful answer. I pass in a weak ptr https://stackoverflow.com/a/53599523
Hmm, passing this as a weak pointer seems problematic. Maybe some help from this answer? https://stackoverflow.com/q/56415222
Actually, nah, seems like having these references back to the parent is super weird and awkward. Maybe passing it through during the step call is the way to go. I can probably do the same during render. Or the boid renderer can get the Simulation _and_ the Boid it’s rendering.
Serialization Chicken & Egg Problem #
It’s tricky. Serialization just takes my simulation instance and populates the relevant values from my JSON file. But what if I want to place the boids randomly throughout the width and height of the simulation? The JSON file doesn’t store that, I just want it for the genetics (or maybe the brains). So it goes ahead and adds a bunch of boids using the default constructor and they all start at position (0, 0).
I’m thinking I need some kind of population class? Or World class? And it just contains information about Boids but not the Boids themselves. Maybe just the genetic information? Well, anything I want to save. I shouldn’t tie it to whether it’s genetic or not. And then I should implement something like Simulation::setPopulation(Population &pop)
so I have control over the actual instantiation of boids and notifying listeners, etc.
Hrm. I thought I was so close to finishing the serialization but still another step to jump through. Oof, that means I wouldn’t have had to change everything to make it work with default constructors, too. I guess that’s for the best anyway.
I’ll commit this for now but I’ll add a todo item to have a separate class for the simulation state that I want to load and save.
Hmm, maybe I can just use BoidProps for now. I already have it. This is the purpose for which I created them. Make them serializable and I’ll already be halfway there.
I need a break.
Differentiate Between Simulation Boid and File Boid (and probably Mutation Boid) #
Or maybe agents? Okay, I just did it using BoidProps and it works great. I’ll leave it be for now.
Todoist board is working nicely for tracking tasks. Though maybe I need a “won’t do” column as well as a “done” column because some things turned out to be unnecessary.
Also I want to track stuff that was added after the initial planning with the tag @newscope
.