- Home /
Save the inventory items to the database
Hi guys, I am making a RPG which can save and load from the database, I can retrieve other values (such as Character Status, Health Point, Maximum Health Point, Exp, Next Exp and so on (it is only single value, which I am not show it here both for the C# and PHP code, I apologize). However, Inventory comes with multiple values (for example: Inventory have items more than 1), I can save and load that Inventory, but it is only returns the last value which is absolutely wrong. So I changed it from single int getter and setter value to List getter and setter instead, but when I run and save it, it gives me an error which is null in the for loop in GameManager class and "Failed to save items to the database" in SaveItem function in UserInventory class:
Here is the code that I am using:
ItemManager class contains the informations which required for the each item (buy value, sell value, name, description and so on)
Save and Load function in UserInventory class:
public static List<ItemManager> inventory= new List<ItemManager>();
public static void SaveItem()
{
try
{
for (int i = 0; i < inventory.Count; i++)
{
ItemManager.SavedInventory[i] = inventory[i].itemID;
}
}
catch
{
Debug.LogError("Failed to save items to the database!");
}
}
public static void LoadItem()
{
try
{
for (int i = 0; i < inventory.Count; i++)
{
inventory[i] = ItemManager.SavedInventory[i] >= 0 ? ItemDatabase.items[ItemManager.SavedInventory[i]] : new ItemManager();
}
}
catch
{
Debug.LogError("Failed to load items from the database!");
}
}
Here is where I cast the PHP code from the website in GameManager class:
public static void WebsiteConnect(string link)
{
WWW www = null;
wwwForm = new WWWForm();
for (int i = 0; i < UserInventory.inventory.Count; i++)
{
wwwForm.AddField("Inventory", ItemManager.SavedInventory[i]);
}
www = new WWW(link, wwwForm);
}
Here is where I call it:
private void OnGUI()
{
if (GUILayout.Button("Save", customStartButton.customStyles[0]))
{
GameManager.WebsiteConnect("WEBSITE_ADDRESS/Save.php");
UserInventory.SaveItem();
}
if (GUILayout.Button("Continue", customStartButton.customStyles[0]))
{
GameManager.WebsiteConnect("WEBSITE_ADDRESS/Load.php");
for (int i = 0; i < UserInventory.inventory.Count; i++)
{
ItemManager.SavedInventory[i] = int.Parse(jsonObject["Inventory"].Value);
}
UserInventory.LoadItem();
}
}
Here is the PHP code for the Save.php and Load.php:
Save.php:
<?PHP
$Inventory = $_POST['Inventory'];
$con = mysql_connect("WEBSITE_ADDRESS","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)
{
$ins = mysql_query("UPDATE `Information` SET Inventory = '".$Inventory."'WHERE Username = '".$Username."'");
if ($ins)
die ("Successfully Saved!");
else
die ("Error: " . mysql_error());
}
else
{
die("Data does not exist!");
}
?>
Load.php:
<?PHP
$Inventory = $_POST['Inventory'];
$con = mysql_connect("WEBSITE_ADDRESS","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))
{
if ($Username == $row['Username'])
{
echo json_encode($row);
}
else
{
die ("Saved data does not match!");
}
}
die();
}
else
{
die ("Data does not exist!");
}
?>
The reason why I did not use PlayerPerfs is because I have Login and Register feature (I am not sure if PlayerPerfs can compare the LoggedInUser or not, so I decided not to use PlayerPerfs), so each player will have it is own Inventory, Character Status and so on, which you can imagine like RPG.
Your answer much appreciated!
Thank you so much!
Whatever error is being thrown is concealed by your Catch all statement.
Use this ins$$anonymous$$d for the catch block try { //... normal (buggy exception throwing) code
}
catch (Exception ex)
{
Debug.LogError("Failed to save items to the database! Reason:");
Debug.Log(ex);
}
Your answer
Follow this Question
Related Questions
C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer
create text file in C#. 2 Answers
Saving world info 3 Answers
How do I save and load the value of a text? 1 Answer
Filetype association 0 Answers