Hey guys!
Continuing with my studies with mySQL + Unity I’m trying some other things. Now, I’m trying to save my transform.position to a mySQL table in order to reimport it when I launch the game again. Like a savegame, or something. I know Unity has a function to do this but the focus is integration with a database.
With string such as usernames and passwords I could do whatever I want. Insert, Update, Everything. But the problem now is when I added some numbers, case of a id which every new register player has, and his position. Everytime I execute reader.Read() the int or float value returns me zero, don’t matter what’s his value in mySQL.
When the game closes, I add the funcionality to save my coordinates to the database, via OnDisable and it works! It 's not working when I want to read these values. Here’s the piece of script I’m using.
void updateCharacterPosition() {
connectionParameter = string.Format("Server={0};" +
"Database={1};" +
"User ID={2};" +
"Password={3};" +
"Pooling={4}", "localhost", "register", "root", "********", "False");
MySqlConnection DBCon = new MySqlConnection(connectionParameter);
DBCon.Open();
MySqlCommand DBCmd = DBCon.CreateCommand();
Vector3 newPos = new Vector3(0, 0, 0);
string loadCoordinates = string.Format("select {0}, {1}, {2} from {3} where {4} = '{5}';",
"positionX", "positionY", "positionZ", "login", "user", PLAYER_NAME);
//The loadCoordinates return a string like this, for example: "select positionX, positionY, positionZ from login where user = 'exampleUserName';"
DBCmd.CommandText = loadCoordinates;
MySqlDataReader reader = DBCmd.ExecuteReader();
while(reader.Read()) {
newPos.x = (float)reader["positionX"];
newPos.y = (float)reader["positionY"];
newPos.z = (float)reader["positionZ"];
}
reader.Close();
reader = null;
DBCmd = null;
DBCon.Close();
DBCon = null;
print(newPos);
transform.position = newPos;
}
Here I have an image of what my table parameters looks like:
http://img94.imageshack.us/img94/8339/capturarmq.png
*All these position values inside my table are set inside Unity, with OnDisable method.
*Resuming, They can be applied, but I can’t get them back.
This is the sintax I’ve used to create the table, if is needed:
CREATE TABLE login (
user_id INT AUTO_INCREMENT,
user VARCHAR(25) NOT NULL,
pass VARCHAR(25) NOT NULL,
positionX FLOAT DEFAULT 0,
positionY FLOAT DEFAULT 0,
positionZ FLOAT DEFAULT 0,
player_name VARCHAR(25),
PRIMARY KEY(user_id));
Any help would be really appreciated!
Thanks from now!