I am trying to use player prefs to save, and subsequently load, my player’s inventory. The inventory is handled by the c# file Inventory3.cs, and saving and loading are handled in the javascript file SaveLoad2.js. My issue is that the players inventory is comprised of “Item” objects, a class in the Inventory3.cs file. How can I call the constructor for Item in my scripts below?
Thanks! Any and all advice is appreciated.
//SaveLoad2.js
#pragma strict
public static var player : GameObject;
player = GameObject.FindGameObjectWithTag("Player");
public static var invScript : Inventory3;
invScript = player.GetComponent("Inventory3");
public static var invSize : int;
invSize = invScript.invSize;
public static var slots : int[];
slots = new int[invSize];
public static function Save() {
PlayerPrefs.SetFloat ("playerX", player.transform.position.x);
PlayerPrefs.SetFloat ("playerY", player.transform.position.y);
PlayerPrefs.SetFloat ("playerZ", player.transform.position.z);
PlayerPrefs.SetInt("sceneToLoad", Application.loadedLevel);
for (var i : int = 0; i< invSize; i++) {
if (invScript.inv *!= null) {*
_ PlayerPrefs.SetString(“InventoryItemName”+i, invScript.inv*.name);_
_ PlayerPrefs.SetString(“InventoryItemDesc”+i, invScript.inv.description);
PlayerPrefs.SetInt(“InventoryItemNumber”+i, invScript.inv.number);
PlayerPrefs.SetInt(“InventoryItemUses”+i, invScript.inv.uses);
PlayerPrefs.SetInt(“StackInv”+i, invScript.stackInv);
slots = 1;
}
else {
slots=0;
}
}
}*_
public static function Load() {
* Application.LoadLevel(PlayerPrefs.GetInt(“sceneToLoad”));*
* player.transform.position = new Vector3(PlayerPrefs.GetFloat (“playerX”),PlayerPrefs.GetFloat (“playerY”), PlayerPrefs.GetFloat (“playerZ”));*
* for (var i : int = 0; i < invSize; i ++) {*
_ if (slots == 1) {
//Somehow populate the inventory with the other items *********************************************************
}
}
}
//Inventory3.cs_
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory3 : MonoBehaviour {
* public int invSize = 10;*
* public Item[] inv;*
* public int invCount = 0;*
* public int[] stackInv;*
* public void Start() {*
* Item tester = new Item(“tester”,“this is a test”,1,0);*
* inv = new Item[invSize];*
* stackInv = new int[invSize];*
* for (int u=0; u<invSize; u++) {*
* inv = null;*
* stackInv = 0;*
* }*
* *
* *
* inv[0] = tester;*
* stackInv[0] = 1;*
* *
* }*
* *
* public void addItem(Item item) {*
* for (int i = 0; i < invSize; i++) {*
if (inv != null && inv*.name == item.name) {*
_ stackInv += item.number;
* break;
}
if (inv == null) {
inv = item;
stackInv += item.number;
break;
}
else {
Debug.Log (“Inventory is full”);
}
}
}
public void Update() {
if (Input.GetKeyUp(KeyCode.I)) {
for (int j = 0; j < invSize; j++) {
if (inv[j] != null) {
Debug.Log (j + " " + inv[j].name);
}
else {
Debug.Log (“end of inventory”);
break;
}
}
}
}
}
public class Item {
public string name;
public string description;
public int number;
public int uses;
public Item(string name,string description,int number,int uses)
{
this.name = name;
this.description = description;
this.number = number;
this.uses = uses;
}
public Item makeItem(string n, string d, int num, int u) {
Item result = new Item(n, d, num, u);
return result;
}
}
public class ItemsDatabase : MonoBehaviour {
public Dictionary <string, Item> dictionary = new Dictionary<string, Item>();
public Item Apple = new Item(“Apple”,“You should know what an apple is”,1,1);
public void Start() {
dictionary.Add(Apple.name,Apple);
}
}*_