1

I am making like RPG game where user can login and save and load the data. I can login and save. But, when I want to load the data, I am having a trouble to load that.

I will post the necessary code only (PHP code (Load.php)):

<?PHP

$Username = $_POST['Username'];
$Location = $_POST['Location'];
$Gold = $_POST['Gold'];
$Level = $_POST['Level'];
$Attack = $_POST['Attack'];
$Defense = $_POST['Defense'];
$MagicDefense = $_POST['MagicDefense'];
$Evasion = $_POST['Evasion'];
$Health = $_POST['Health'];
$MaxHealth = $_POST['MaxHealth'];
$Mana = $_POST['Mana'];
$MaxMana = $_POST['MaxMana'];
$Exp = $_POST['Exp'];
$NextExp = $_POST['NextExp'];
$AcceptedChiefQuest = $_POST['AcceptedChiefQuest'];
$AddedChiefQuest = $_POST['AddedChiefQuest'];

$con = mysql_connect("SERVER_NAME","DATABASE_NAME","PASSWORD") or ("Cannot connect!"  . mysql_error());

if (!$con)
    die('Could not connect: ' . mysql_error());

mysql_select_db("DATABASE_NAME" , $con) or die ("Could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM Information WHERE `Username` = '".$Username."'");

$numrows = mysql_num_rows($check);

if ($numrows > 0)
{
    while($row = mysql_fetch_assoc($check))
    {
        $Username = $row['Username'];
        $Location = $row['Location'];
        $Gold = $row['Gold'];
        $Level = $row['Level'];
        $Attack = $row['Attack'];
        $Defense = $row['Defense'];
        $MagicDefense = $row['MagicDefense'];
        $Evasion = $row['Evasion'];
        $Health = $row['Health'];
        $MaxHealth = $row['MaxHealth'];
        $Mana = $row['Mana'];
        $MaxMana = $row['MaxMana'];
        $Exp = $row['Exp'];
        $NextExp = $row['NextExp'];
        $AcceptedChiefQuest = $row['AcceptedChiefQuest'];
        $AddedChiefQuest = $row['AddedChiefQuest'];

        echo $Location;
    }

        die("Successfully Loaded!");
}

else
{
    die ("Data does not exist!");
}


?>

And here where I access it by script in Unity (necessary code where the problem is):

IEnumerator LoadCoroutine(WWW _www)
    {
        yield return _www;

        if (_www.error == null)
        {
            message = _www.text;

            if (_www.text == "Successfully Loaded!")
            {
                // Below code from this line, never gets executed (if and else if statement)
                // The `GameManager.CurrentLocation` appear as default value

                if (_www.text == "Village")
                {
                    GameManager.CurrentLocation = "Village";

                    GameManager.Loader = 1;
                }

                else if (_www.text == "Yein Plain")
                {
                    GameManager.CurrentLocation = "Yein Plain";

                    GameManager.Loader = 4;
                }

                // Below code from this line will gets executed.

                message = "Successfully Loaded.";

                mainMenu = false;

                yield return new WaitForSeconds(3);

                Reset();

                GameManager.LoadLevel("Loading Scene");
            }

            else if (_www.text == "Data does not exist!")
            {
                message = "Data does not exist";

                clickedLoadGame = false;
            }

            else if (_www.text == "Saved data does not match!")
            {
                message = "Saved data does not match";

                clickedLoadGame = false;
            }
        }

        else
        {
            message = "Error while trying to connect to the server.\nMessage: " + _www.error;

            clickedLoadGame = false;
        }
    }

When I Debug.Log the message, it appear like this: VillageSuccessfullyLoaded! and it never appear like this Village or Yein Plain. Seems like the echo and die in PHP code gets combined together.

My question is, how to retrieve only the $Location from php and not the other code? So it will return only Village or Yein Plain.

Really appreciate your answer!

Thank you very much

Yunnan
  • 79
  • 2
  • 11

1 Answers1

1

Change

die("Successfully Loaded!");

to

die();

Then:

        if (_www.text == "Data does not exist!")
        {
            message = "Data does not exist";

            clickedLoadGame = false;
        }

        else if (_www.text == "Saved data does not match!")
        {
            message = "Saved data does not match";

            clickedLoadGame = false;
        }

        else
        {
            if (_www.text == "Village")
            {
                GameManager.CurrentLocation = "Village";

                GameManager.Loader = 1;
            }

            else if (_www.text == "Yein Plain")
            {
                GameManager.CurrentLocation = "Yein Plain";

                GameManager.Loader = 4;
            }

            message = "Successfully Loaded.";

            mainMenu = false;

            yield return new WaitForSeconds(3);

            Reset();

            GameManager.LoadLevel("Loading Scene");
        }
Mandi
  • 414
  • 1
  • 4
  • 12
  • But if I do that, I can't check whether it successfully loaded from the database or not in the unity. – Yunnan Mar 11 '15 at 06:16
  • 1
    The problem is that that the message you are printing to your unity script is not in a structured format, which will cause bigger problems later on. I would print a JSON and then parse it on my Unity script and do whatever I want with the data there. – Mandi Mar 11 '15 at 06:30
  • Thank you so much. If you dont mind, i have another question. What about if I want to retrieve multiple values from the database? But I just want to that value appear as `_www.text`. Example like: `echo $Location` and `echo $Username`. So the `_www.text` will be like `Village` or `Yunnan`, not `VillageYunnan`. Thank you. How would you print a JSON and parse it? Do I have to use JSON Script which can be downloaded on the internet? – Yunnan Mar 11 '15 at 06:36
  • 1
    So yeah, that's what I mean. You need a structured message that you can then parse on your unity script. Basically you can print a comma or space between each value something like echo $Location . ","; The problem with that is that I'll be very hard to maintain. My recommendation is to check some message formats, like JSON, and print that on the PHP, then parse it on the Unity script. I'm glad I could help. – Mandi Mar 11 '15 at 06:41
  • So checkout this php function: json_encode() You'll see it applied the way you need it in this question: http://stackoverflow.com/questions/383631/json-encode-mysql-results Then you will need to find by yourself how to parse it on the unity side, which I can't really help there.@Yunnan – Mandi Mar 11 '15 at 06:47