vector3 to php to mysql and back

Ok first off Im not even sure if I’m doing this right. What I am doing is desiging a log in system and out for my players. It will save the xyz location of the player to a post php witch will save to my sql server. I have looked everywhere people all over have different ways of doing this but I want to save to a mysql server NOT the player perfs. I am also writing this in C# I am trying to save the xyz from a vector3 to int but I’m haveing some trouble getting it to that point. Here is the striped down script. The other stuff is for my login so its unimportant.

using UnityEngine;
using System.Collections;

private int inX;
private int inY;
private int inZ;

public class login : MonoBehaviour {

	// Use this for initialization
	void Start () {
		transform.position = new Vector3(inX, inY, inZ);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

I’m not entirely sure on how to get the xyz of the player, Im assuming its get current.vector3 or something like that. Anyways any help would be greatly appreciated.

Hello,

First of all Vector3 gets an float parameters so you need to typecast/convert those ints.
The easiest way [for me] is to generate an serialized file [binary/xml - doesnt matter] and just upload them via the webserver and in the player database [mysql in this case] just make table that contains fields like player credentials emails and such and the path_to_save_file.
If that’s not enough informations then post a comment and i’ll put some example.

Regards,
M.Rogalski

I guess if you’re posting over http to php, you’ll want to convert the values to string, which you add to a WWWForm, and then submit to your server. Here’s a short example which uploads the player’s x, y & z position to an example php script:

using UnityEngine;
using System.Collections;

public class UploadPosition : MonoBehaviour {

	public int playerID = 1;
	string url = "http://example.com/StorePlayerPosition.php";

	void Update()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			StartCoroutine(Upload ());
		}
	}

	IEnumerator Upload() {

		// Create a Web Form
		var form = new WWWForm();
		form.AddField("playerID", playerID );
		form.AddField("x", transform.position.x.ToString("0.00"));
		form.AddField("y", transform.position.y.ToString("0.00"));
		form.AddField("z", transform.position.z.ToString("0.00"));
		Debug.Log("submitting "+form+" to "+url);
		WWW request = new WWW(url, form);
		yield return request;
		if (!string.IsNullOrEmpty(request.error)) 
		{
			Debug.Log(request+" error:

"+request.error);
} else {
Debug.Log ("Sent “+request+” successfully, result:
"+request.text);
}
}
}

I added a player ID value too just for example purposes because you’re quite likely to want something like this, but it’s not vital to the code.

For floating point values like your position, I’ve put a limit on the accuracy to avoid scientific notation being used (for example, if your player position x is 0.0000001, your post request might contain “x=1E-07”). To do this, I used the form of ToString which accepts a formatting parameter and specificed “0.00” as the format, which limits the value to two decimal places.


To get values back from your server, you’ll first - of course - need to write a php script which retrieves the values from your DB. You’d then print out these values to the result in some format of your choosing. Perhaps XML, YAML, or just plain comma separated values.

I’m going to assume the simplest option, which is that your php script prints something out like this:

1.23,3.14,10.92

So you’d request the php script (passing the ID of the player you want), and read the result text. Then split the result txt using the comma delimiter, and parse each of the three x,y,z strings into real float numbers. If you have more complex information to transmit (the player’s other stats, inventory, quest status, etc), you might want to look at using an industry standard information format like XML or YAML, and use a pre-written encoder and decoder to package your data in text form for transmission over http.

Here’s an example of simple comma-separated version:

using UnityEngine;
using System.Collections;

public class RetrievePosition: MonoBehaviour {

    public int playerID = 1;
    string url = "http://example.com/GetPlayerPosition.php";

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Retrieve());
        }
    }

    IEnumerator Retrieve() {

        // Create a Web Form, including player ID
        var form = new WWWForm();
        form.AddField("playerID", playerID );
        Debug.Log("submitting "+form+" to "+url);
        WWW request = new WWW(url, form);
        yield return request;
        if (!string.IsNullOrEmpty(request.error)) 
        {
            Debug.Log(request+" error:

"+request.error);
} else {
Debug.Log ("Sent “+request+” successfully, result:
"+request.text);
// split the result using comma as the delimiter
string pos = request.text.Split(‘,’);
// parse the three strings into real float numbers
float x = float.Parse(pos[0]);
float y = float.Parse(pos[1]);
float z = float.Parse(pos[2]);
// assign the position by putting the parsed numbers into a vector3
transform.position = new Vector3(x,y,z);
}
}
}