Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Yunnan · Mar 15, 2015 at 02:09 AM · c#saveloadinventoryphp

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dr3th · Mar 18, 2015 at 08:24 PM 0
Share

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);
 
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges