Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Include a specific error, task, problem, or question in your title

php unity player position sql xampp c# origin

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 Killuah

Killuah

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 July 2018 - 06:06 AM

First of all, I am not sure this is the right place to post this topic sorry about that.[/size]
I am using XAMPP and MYSQL to create my database everything works well but when I close the game and try to load the last position the player just teleports to the origin.[/size]

C# Code on unity:
DataLoader script

 

using UnityEngine;
using System.Collections;

public class DataLoader : MonoBehaviour {
    public int playerxd;
    public int playeryd;
    public int playerzd;
    public string[] pos;
	IEnumerator Start(){

		WWW posData = new WWW("http://localhost/BegoneBD/DataPos.php");
		yield return posData;
		string posDataString = posData.text;
		print (posDataString);
		pos = posDataString.Split(';');
		print(GetDataValue(pos[0], "x:"));
        print(GetDataValue(pos[0], "y:"));
        print(GetDataValue(pos[0], "z:"));
    }

	string GetDataValue(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerxd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue2(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playeryd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue3(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerzd = System.Convert.ToInt32(value);
        return value;
    }

}

Save script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Save : MonoBehaviour {
    public string[] pos;
    bool teste;
    float playerx;
    float playery;
    float playerz;
    int inputplayerx;
    int inputplayery;
    int inputplayerz;
    string CreateUserUrl = "http://localhost/BegoneBD/InsertPos.php";
    public void SavePosition()
    {
        playerx = transform.position.x;
        playery = transform.position.y;
        playerz = transform.position.z;
        inputplayerx = (int)playerx;
        inputplayery = (int)playery;
        inputplayerz = (int)playerz;
        CreatePos(inputplayerx, inputplayery, inputplayerz);
    }
    public void CreatePos(int playerx1, int playery1, int playerz1)
    {
        WWWForm form = new WWWForm();
        form.AddField("playerxpost", playerx1);
        form.AddField("playerypost", playery1);
        form.AddField("playerzpost", playerz1);
        WWW www = new WWW(CreateUserUrl, form);
    }
    public void LoadPosition()
    {
        DataLoader f = new DataLoader();
        f.playerxd = (int)playerx;
        f.playeryd = (int)playery;
        f.playerzd = (int)playerz;
        transform.position = new Vector3(playerx, playery, playerz);
    }
    public void CursorSaveOn()
    {
            Cursor.visible = true;

    }
    public void CursorSaveOff()
    {
        Cursor.visible = false;

    }
}

PHP Code for the connection with the database:

InsertPos.php

<?php
	$servername = "localhost";
	$username =  "root";
	$password = "";
	$dbName = "begonebd";
	$x = "-240";
	$y = "2";
	$z = "-247";
	
	$x = $_POST["playerxpost"];
	$y = $_POST["playerypost"];
	$z = $_POST["playerzpost"];
	Make Connection
	$conn = new mysqli($servername, $username, $password, $dbName);
	Check Connection
	if(!$conn){
		die("Connection Failed. ". mysqli_connect_error());
	}
	else echo("Connection Success");
		
	$sql = "SELECT id, x, y, z FROM playerposition";
	$result = mysqli_query($conn ,$sql);
	
	
	$sql = "INSERT INTO playerposition (x, y, z)
			VALUES ('".$x."','".$y."','".$z."')";
	$result = mysqli_query($conn ,$sql);
	
	if(!result) echo "there was an error";
	else echo "Everything ok.";

?>

DataPos.php

<?php
	$servername = "localhost";
	$username =  "root";
	$password = "";
	$dbName = "begonebd";
	
	//Make Connection
	$conn = new mysqli($servername, $username, $password, $dbName);
	//Check Connection
	if(!$conn){
		die("Connection Failed. ". mysqli_connect_error());
	}
		
	$sql = "SELECT id, x, y, z FROM playerposition";
	$result = mysqli_query($conn ,$sql);
	
	
	if(mysqli_num_rows($result) > 0){
		//show data for each row
		while($row = mysqli_fetch_assoc($result)){
			echo "id:".$row['id'] . "|x:".$row['x']. "|y:".$row['y']. "|z:".$row['z'] . "<br>";
		}
	}
	
	
	


?>---





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download