I made the following code. BoolObject is just a container for bool. It has nothing but a bool variable called “value”.
I use the setData method to save a vector with BoolObjects.
I use the getData to get the vector afterwards, but once I log the values of the vector that getData returns, I realized that the values were wrong. The vector is still the correct size (15 elements) but the values inside of it are wrong.
Does anyone know how to fix this?
void GameScene::setData(const char* key)
{
Data data;
std::vector<BoolObject*> v;
for (int i = 0; i < item1->getChildren().size(); i++)
{
BoolObject * b = new BoolObject(item1->getChildren().at(i)->isVisible());
v.push_back(b);
}
for (int i = 0; i < item2->getChildren().size(); i++)
{
BoolObject * b = new BoolObject(item2->getChildren().at(i)->isVisible());
v.push_back(b);
}
for (int i = 0; i < item3->getChildren().size(); i++)
{
BoolObject * b = new BoolObject(item3->getChildren().at(i)->isVisible());
v.push_back(b);
}
data.copy((unsigned char*) v.data(), v.size() * sizeof(BoolObject*));
UserDefault::getInstance()->setDataForKey(key, data);
UserDefault::getInstance()->flush();
}
std::vector<BoolObject*> GameScene::getData(const char* key)
{
Data data = UserDefault::getInstance()->getDataForKey(key);
std::vector<BoolObject*> s;
BoolObject * buffer = (BoolObject*) data.getBytes();
ssize_t length = data.getSize() / sizeof(BoolObject*);
for (int i = 0; i < length; i++)
{
s.push_back(&buffer[i]);
}
return s;
}