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[i] != null) {
PlayerPrefs.SetString("InventoryItemName"+i, invScript.inv[i].name);
PlayerPrefs.SetString("InventoryItemDesc"+i, invScript.inv[i].description);
PlayerPrefs.SetInt("InventoryItemNumber"+i, invScript.inv[i].number);
PlayerPrefs.SetInt("InventoryItemUses"+i, invScript.inv[i].uses);
PlayerPrefs.SetInt("StackInv"+i, invScript.stackInv[i]);
slots[i] = 1;
}
else {
slots[i]=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[i] == 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 dictionary = new Dictionary();
public Item Apple = new Item("Apple","You should know what an apple is",1,1);
public void Start() {
dictionary.Add(Apple.name,Apple);
}
}
↧