Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Storing/Retrieving serialized player data stored on MySQL using PHP

Discussion in 'Scripting' started by reviyee, Apr 29, 2020.

  1. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    I wrote code to Serialize/Deserialize my player character, stats and inventory.
    It stores the player's
    -position
    -quest ids
    -completed quest ids
    -skill id, level, exp
    -inventory item id, quantity

    I'm able to save and load it locally using a byte array. It works fine.
    I've tried to store it on a MySQL database using UnityWebRequest to POST the data to a PHP webserver, however, the data stored on the database's BLOB is bigger than the byte array on Unity. 322 bytes vs 338 bytes on an empty player character.

    If I try to save the character to the database and load it after it finishes, a serialization exception pops up: "SerializationException: No map for object 201326592'." so it doesn't work.

    Anyone knows how to properly save/load byte arrays to/from a database using a webserver? If I'm doing anything wrong I'd appreciate your feedback.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    There's probably a thousand things that could be wrong here, first thing to check is, are you receiving the same data that you are sending? Is that the same data that gets written to the database? Is it the same data you get when retrieving it from the database? Is it the same data when you get it back to your application?

    Follow the data, and find out where it gets corrupted first.
     
    Kurt-Dekker likes this.
  3. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    Ok I'll find a way to check the length of the bytes for now. If I find the issue do you think you could help me with it?
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Depends what the issue turns out to be.
     
  5. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    I managed to store the correct ammount of bytes using AddBinaryData on my C# script

    Code (CSharp):
    1. form.AddBinaryData("characterData", bytes, characterName);
    and using $_FILES['characterData'] on PHP. I was using $_POST['characterData'] before and I was sending the byte as a string, which I guess was screwing up everything.

    Now the data on the editor and the database matches. I'm now stuck on what's the best way to recover the data from the database.

    Data is a byte[] stored as a BLOB. the syntax to get the data would be something like

    Code (Boo):
    1. {
    2.     $id = $_POST['id']; // I know I have to escape this, this is just a test
    3.     $query= "SELECT characterData FROM rpg.characters WHERE id = '$id'";
    4.     $result = mysqli_query($conn, $sql);
    5.     $row = mysqli_fetch_array($result);
    6.     print $row[0];
    7.  
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Looks fine to me, is there a problem with that? If blobs and bytes are giving you too much of a headache, you could always use a base64 encoded string.