2015-02-23 10:27:09 (edited by ogomez92 2015-02-23 12:43:33)

Hi,
I know this may sound like a stupid question but... Why is my game unable to serialize an array of strings?
I wanted the unlockable packs to be an array of strings and jsut serialize it like any other variable, but I ended up having to use a string and splitting it because an array, no matter how hard I try just won't serialize...
What am I doing wrong?

Thanks

ReferenceError: Signature is not defined.

2015-02-23 12:42:47 (edited by ogomez92 2015-02-23 12:43:15)

OK now I tried to use a dictionary. Same thing happens. It won't serialize nor deserialize my dictionary... I wonder if this is a stupid bgt bug or what, but it's not serializing my dictionary! sad

ReferenceError: Signature is not defined.

2015-02-23 12:57:38

Check out this code

dictionary madness;
dictionary serialized;
void main() {
madness.set("i_am_pissed","Yes, yes I am!");
serialized.set("madness",madness);
serialized.set("anumber",1);
//comment the line above and the f***ing serialize string will be empty!
string srld=serialize(serialized);
alert("result",srld);
//however!
dictionary see;
bool fails=serialized.get("madness",see);
if (!fails) alert("You can't even retrieve","dictionaries from dictionaries!");
string result2;
see.get("i_am_pissed",result2);
alert("result2",result2);
}

//conclusion
You can set a dictionary into another dictionary but it will not be serialized!

ReferenceError: Signature is not defined.

2015-02-23 15:12:16

I think bgt only lets you serialize the primitive types, int, string, bool, float, and maybe a few others. Objects, arrays of objects, and custom classes can't be serialized sadly. You'll have to break them down into primitive types then remake them when you deserialize.

I like to sleep, Sleep is good,
This is how I do it: Lie on a nice warm cozy bed, and dream dreams about how to rule the world!
Follow @TheGreatAthlon5 on twitter for humorous facts and game updates!
If you like my posts, thumb me up!

2015-02-23 15:33:57

ok. I will. Thanks

ReferenceError: Signature is not defined.

2015-02-26 00:07:41

Here's an example of serialization:
// Serialize some data.

// Create a map class.

class map
{
int[][] location; // Stores all the various positions on the map.
int size_x; // The number of squares to place on the X axis.
int size_y; // The number of squares to place on the Y axis.

// Create our constructor.
map()
{
reset();
}

// Implement a reset method for resetting our map.
void reset()
{
location.resize(0);
size_x=0;
size_y=0;
}

// Implement a rudimentary setup procedure.
void setup(int max_x=20, int max_y=20)
{
if((max_x<=0)||(max_y<=0))
{
return;
}

// Fill out our properties.
size_x=max_x;
size_y=max_y;

// Fill up the array.
location.resize(max_x);
for(int x=0; x<max_x; x++)
{
location[x].resize(max_y);

// Assign values to it.
for(int y=0; y<max_y; y++)
{
location[x][y]=random(0, 5);
}
}
}

// This serializes the map.
string save()
{
dictionary data; // We will pass this dictionary to the serialize function.

// First, we store the size of the map so we know how many squares we have available.
data.set("map.size_x", size_x);
data.set("map.size_y", size_y);

// Put our map in the dictionary.
for(int x=0; x<size_x; x++)
{
for(int y=0; y<size_y; y++)
{
data.set("map.location["+x+"]["+y+"]", location[x][y]);
}
}

// Return the serialization.
return serialize(data);
}

// This will deserialize and load the values back into the map.
bool load(string data)
{

// Check if we have no data.
if(data=="")
{
return false;
}
dictionary@ restore=deserialize(data); // This is where the data will be stored initially.
reset(); // So we have a clean start.
restore.get("map.size_x", size_x);
restore.get("map.size_y", size_y);
if((size_x<=0)||(size_y<=0))
{
return false;
}

//Resize our map.
location.resize(size_x);
for(int x=0; x<size_x; x++)
{
location[x].resize(size_y);

// Now we can restore our values.
for(int y=0; y<size_y; y++)
{
restore.get("map.location["+x+"]["+y+"]", location[x][y]);
}
}
return true;
}
}

// Create a player class

class player
{
int x;
int y;
int score;
int health;

player()
{
reset();
}

void reset()
{
x=0;
y=0;
health=100;
score=0;
}

void setup()
{

// This will set random values for each property, merely to demonstrate serialization.
x=random(0, 10);
y=random(0, 10);
score=random(0, 500000);
health=random(1, 100);
}

string save()
{
dictionary data; // We will pass this dictionary to the serialize function.

// Put our values in the dictionary.
data.set("player.x", x);
data.set("player.y", y);
data.set("player.health", health);
data.set("player.score", score);

// Return the serialization.
return serialize(data);
}

// This will deserialize and load the values back into the class.
bool load(string data)
{

// Check if we have no data.
if(data=="")
{
return false;
}
dictionary@ restore=deserialize(data); // This is where the data will be stored initially.
reset(); // So we have a clean start.
restore.get("player.x", x);
restore.get("player.y", y);
restore.get("player.health", health);
restore.get("player.score", score);
return true;
}
}

// Create an enemy class

class enemy
{
int id; //Used to store a unique identifier, typically the position in the array where he is stored.
int x;
int y;
int speed;
int health;
int state;

enemy()
{
reset();
}

void reset()
{
id=-1;
x=0;
y=0;
speed=random(300, 500);
health=100;
state=0; //Can be used to store whether he is attacking, walking etc.
}

void setup(int id)
{

// This will set random values for each property, merely to demonstrate serialization. We ask for the ID so we can serialize the correct enemy when the time comes.
this.id=id;
x=random(0, 10);
y=random(0, 10);
speed=random(300, 500);
health=random(1, 100);
state=random(0, 5); //Can be used to store whether he is attacking, exploring, fleeing etc.
}

string save()
{
dictionary data; // We will pass this dictionary to the serialize function.

// Put our values in the dictionary. Notice we use the enemy ID as a key rather than as a value.
data.set("enemy["+id+"].x", x);
data.set("enemy["+id+"].y", y);
data.set("enemy["+id+"].health", health);
data.set("enemy["+id+"].state", state);
data.set("enemy["+id+"].speed", speed);

// Return the serialization.
return serialize(data);
}

// This will deserialize and load the values back into the class.
bool load(string data, int id)
{

// Check if we have no data.
if((data=="")||(id<0))
{
return false;
}
dictionary@ restore=deserialize(data); // This is where the data will be stored initially.
reset(); // So we have a clean start.
this.id=id;
restore.get("enemy["+id+"].x", x);
restore.get("enemy["+id+"].y", y);
restore.get("enemy["+id+"].health", health);
restore.get("enemy["+id+"].state", state);
restore.get("enemy["+id+"].speed", speed);
return true;
}
}

// Now we declare some values and serialize them to a file.

player me;
map world;
enemy[] monster;

void main()
{

// Declare our variables.
dictionary example;
file storage;

// Set up our environment.
int monsters=random(5, 10);
monster.resize(monsters);
for(int counter=0; counter<monster.length(); counter++)
{
monster[counter].setup(counter);
}
me.setup();
world.setup();

// Construct our serialization.
example.set("enemy.length", monsters);
string data=world.save();
data+=me.save();
data+=serialize(example);
for(int counter=0; counter<monsters; counter++)
{
data+=monster[counter].save();

// We may as well reset our enemies in this loop, since we are going to reset everything after anyway.
monster[counter].reset();
}

// Go about resetting everything.
monster.resize(0);
me.reset();
world.reset();

// Save our serialization to a file.
storage.open("serialize_test.txt", "wb");
storage.write(data);
storage.close();
}

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2015-02-26 02:23:14

Yay, for copying the example from te reference.

I like to sleep, Sleep is good,
This is how I do it: Lie on a nice warm cozy bed, and dream dreams about how to rule the world!
Follow @TheGreatAthlon5 on twitter for humorous facts and game updates!
If you like my posts, thumb me up!