This title is confusing, but idk what else to call it.
I am trying to implement ions and ionic compounds in a mod.
Right now I have an ion implemented as a struct:
struct ion {
int type;
int number;
int charge;
};
Each particle stores 2 of these structs (one anion, one cation).
However, this means that each particle (e.g. water) can only store one ionic compound. In reality, each type of ion has negligible effect on the solubility of another. So, I want to be able to store a seemingly unlimited number of ions in each particle.
What is the best way going about this?
The way memory is handled currently makes it seem like directly storing a vector of ions is not possible because there would be no definite 'offset' due to the amount of memory needed not being constant.
Making a large array would make no sense because it is not infinite and would be a waste of memory for each particle.
The only way I can think of is making the actual data stored in the particle a pointer to a vector of ion structs.
I am still a noob concerning pointers, but how would I implement this? Where/when/how would I allocate and free the memory for the vector of structs?
so in the properties vector I should add
{ "ions", StructProperty::Vector, offsetof(Particle, ions) }
and add definitions for Vector in StructProperty and PropertyTool.
How/why do I store 4 bit numbers in the vector? I used ints because they seem hassle free, and initialize to 0.
If I did something like this would it work?
std::vector<ion> ions; //is in Particle
//in element code
ion ionP;
ionP.type = PT_NA; //NA is sodium element
ionP.number = 1;
ionP.charge = 1;
parts[i].ions.push_back(ionP);
would ions[0].type == PT_NA?
I have a feeling it is much more complicated than this.
ok, what about using a std::vector<ion>* in Particle?
this appears to be working somewhat, but idk
Is there anything wrong with it?
I'm assuming it initializes to NULL and I can then create a new vector<ion>
if(parts[i].ions == NULL)
parts[i].ions = new vector<ion>();
each time a solvent gets something dissolved in it, and push to it to add more ions.
When would I need to delete/free?
You would delete the vector when a particle is killed (Simulation.kill_part) or the simulation is cleared (simulation.Clear I think). You would also need to add the ions to Snapshot.h / .cpp and wherever in Simulation.cpp it saves snapshots, otherwise undo / redo won't work properly as you aren't saving the dissolved ions in the undo / redo state.
@Lord_Bowserinator (View Post)
Thanks, learned a lot about c++ pointers and vectors!