- Home /
Question by
danny_appel · Jan 18, 2021 at 10:55 AM ·
randominventorysave datascriptable objectequipment
What is the best way for saving Scriptable Objects (With random stats when created)?
Hi all,
Im hitting a roadblock, I got a player and enemies working that drop loot when they die, got the inventory finally working althought now I cannot save my inventory. All the vidoes that im trying to watch are hard to understand and develop in my project. And I fear that creating random stats on the item when instantiated wasnt a good idea, since nobody does that.
Does any one know a good way to do it do I have to redo all the items a different way? I will show what I mean with random stats with instantiating. (Line 163)
using UnityEngine;
namespace Items
{
//Enum of item types
public enum ItemType
{
Weapon,
Armor,
Consumable,
Misc
}
//Enum of equip types
public enum EquipType
{
Weapon,
Armor,
Ammo
}
public enum RarityType
{
Common,
Uncommon,
Rare,
Epic,
Legendary
}
[CreateAssetMenu(fileName = "New Item", menuName = "Items/Base Item")]
[System.Serializable]
public class ItemData : ScriptableObject
{
PlayerStats playerStats;
private float _ItemLevel;
string[] commonNamePrefix = new string[] { "Old ", "Damaged ", "Battered ", "Rusty " };
string[] uncommonNamePrefix = new string[] { "Used ", "Battered ", "Dirty ", "Bloody " };
string[] rareNamePrefix = new string[] { "Balanced ", "Fine ", "Repaired " };
string[] rareNameEnd = new string[] { " of the Warrior", " of the Soldier", " of Clans", " of Raiders", " of the Backstabber" };
string[] epicNamePrefix = new string[] { "Fine ", "Ferocious ", "Massive ", "Perfect "};
string[] epicNameEnd = new string[] { " of the Claw", " of the Master", " of the Falcon", " of Sovereignty", " of the Graverobber", " of Scavangers"};
string[] legendaryNamePrefix = new string[] { "Shiny ", "Golden ", "Brutal ", "Deadly "};
string[] legendaryNameEnd = new string[] { " of the Grandmaster", " of the Fallen Hero", " of the Living Legend", " of Greatness"};
string[] legendaryStatTitle = new string[] { " of Swiftness", " of Fortune", " of Bloodthirst" };
[SerializeField]
private float maxStamina, maxEndurance, maxLuck, maxCool;
private float minStamina, minEndurance, minLuck, minCool;
private string randomPrefix, randomNameEnd;
private string _itemTitle;
public string ItemTitle { get { return _itemTitle; } set { _itemTitle = value; } }
private float _powerLevel;
public float PowerLevel { get { return _powerLevel; } set { _powerLevel = value; } }
private float _stamina;
public float Stamina { get { return _stamina; } set { _stamina = value; } }
private float _endurance;
public float Endurance { get { return _endurance; } set { _endurance = value; } }
private float _luck;
public float Luck { get { return _luck; } set { _luck = value; } }
private float _cool;
public float Cool { get { return _cool; } set { _cool = value; } }
public float Level { get { return _ItemLevel; } set { _ItemLevel = value; } }
[SerializeField]
private float minDamage, maxDamage;
private float _damage;
public float Damage { get { return _damage; } set { _damage = value; } }
[Space(10)] // 10 pixels of spacing here.
[SerializeField]
private float minDefence;
[SerializeField]
private float maxDefence;
private float characterMinLevel, characterMaxLevel;
private float _defence;
public float Defence { get { return _defence; } set { _defence = value; } }
[Space(10)] // 10 pixels of spacing here.
//Rarity reference for the items
[SerializeField]
private RarityType _rarityType;
public RarityType RarityType { get { return _rarityType; } set { _rarityType = value; } }
[SerializeField]
private WeaponType _weaponType;
public WeaponType WeaponType { get { return _weaponType; } set { _weaponType = value; } }
private WeaponItemData _weaponItemData;
public WeaponItemData WeaponItemData { get { return _weaponItemData; } set { _weaponItemData = value; } }
//Item ID for easy referencing
[SerializeField]
private int _id;
public int ItemID { get { return _id; } set { _id = value; } }
//Bool for stackable items
[SerializeField]
private bool _stackable;
public bool Stackable { get { return _stackable; } protected set { _stackable = value; } }
//Item type reference
[SerializeField]
private ItemType _itemType;
public ItemType ItemType { get { return _itemType; } protected set { _itemType = value; } }
//String for display name
[SerializeField]
private string _itemName;
public string ItemName { get { return _itemName; } }
//Sprite for display image
[SerializeField]
private Sprite _itemImage;
public Sprite ItemImage { get { return _itemImage; } set { _itemImage = value; } }
//Float for item weight
[SerializeField]
private float _itemWeight;
public float ItemWeight { get { return _itemWeight; } }
//String for item display description
[SerializeField]
private string _itemDescription;
public string ItemDescription { get { return _itemDescription; } }
//GameObject prefab to spawn when item is dropped
[SerializeField]
private GameObject _itemPrefab;
public GameObject ItemPrefab { get { return _itemPrefab; } }
private float legendaryExtraBonus = 1.4f;
private float epicExtraBonus = 1.3f;
private float rareExtraBonus = 1.2f;
private float uncommonExtraBonus = 1.1f;
//Create and return a new item from the existing data
public Item CreateItem()
{
playerStats = GameObject.FindObjectOfType<PlayerStats>();
Item item = new Item(this);
Damage = Random.Range(minDamage, maxDamage * _ItemLevel);
Defence = Random.Range(minDefence, maxDefence * _ItemLevel);
Stamina = Random.Range(minStamina, maxStamina * _ItemLevel);
Endurance = Random.Range(minEndurance, maxEndurance * _ItemLevel);
Luck = Random.Range(minLuck, maxLuck * _ItemLevel);
Cool = Random.Range(minCool, maxCool * _ItemLevel);
characterMinLevel = playerStats.playerLevel - 1;
characterMaxLevel = playerStats.playerLevel + 2;
_ItemLevel = Random.Range(characterMinLevel, characterMaxLevel);
if (_rarityType == RarityType.Legendary)
{
Damage = Damage * legendaryExtraBonus;
randomPrefix = legendaryNamePrefix[Random.Range(0, legendaryNamePrefix.Length)];
randomNameEnd = legendaryNameEnd[Random.Range(0, legendaryNameEnd.Length)];
}
else if (_rarityType == RarityType.Epic)
{
Damage = Damage * epicExtraBonus;
randomPrefix = epicNamePrefix[Random.Range(0, epicNamePrefix.Length)];
randomNameEnd = epicNameEnd[Random.Range(0, epicNameEnd.Length)];
}
else if (_rarityType == RarityType.Rare)
{
Damage = Damage * rareExtraBonus;
randomPrefix = rareNamePrefix[Random.Range(0, rareNamePrefix.Length)];
randomNameEnd = rareNameEnd[Random.Range(0, rareNameEnd.Length)];
}
else if (_rarityType == RarityType.Uncommon)
{
Damage = Damage * uncommonExtraBonus;
randomPrefix = uncommonNamePrefix[Random.Range(0, uncommonNamePrefix.Length)];
randomNameEnd = "";
}
else if(_rarityType == RarityType.Common)
{
randomPrefix = commonNamePrefix[Random.Range(0, commonNamePrefix.Length)];
randomNameEnd = "";
}
PowerLevel = Stamina + Endurance + Luck + Cool + Damage + Defence;
ItemTitle = randomPrefix + _itemName + randomNameEnd;
return item;
}
}
public class EquipItemData : ItemData
{
//Reference for the equip type of the item
[SerializeField]
private EquipType _equipType;
public EquipType EquipType { get { return _equipType; } set { _equipType = value; } }
//Create and return a new equippable item from the existing data
public EquippableItem CreateEquippableItem()
{
EquippableItem item = new EquippableItem(this);
return item;
}
}
//Item Class
//Variables match the variables from ItemData
[System.Serializable]
public class Item
{
public string Name;
public int ID;
public bool Stackable;
public float Weight;
public Sprite Icon;
public float Damage;
public float Defence;
public float ItemLevel;
public string ItemTitle;
public float Stamina;
public float Endurance;
public float Luck;
public float Cool;
public float PowerLevel;
public float BaseCritChance;
public float BaseItemFindChance;
public float BaseMovementSpeed;
public ItemType ItemType;
public RarityType RarityType;
public WeaponType WeaponType;
public Item(ItemData item)
{
Name = item.ItemName;
ID = item.ItemID;
Stackable = item.Stackable;
Weight = item.ItemWeight;
Icon = item.ItemImage;
ItemLevel = item.Level;
Defence = item.Defence;
Damage = item.Damage;
RarityType = item.RarityType;
ItemType = item.ItemType;
Stamina = item.Stamina;
Endurance = item.Endurance;
Luck = item.Luck;
Cool = item.Cool;
PowerLevel = item.PowerLevel;
WeaponType = item.WeaponType;
ItemTitle = item.ItemTitle;
}
}
//Equippable Item Class
//Variables match those from EquippableItemData
[System.Serializable]
public class EquippableItem : Item
{
public EquipType EquipType;
public EquippableItem(EquipItemData item) : base(item as ItemData)
{
EquipType = item.EquipType;
}
}
}
Comment