- Home /
This question was
closed May 31, 2018 at 12:15 PM by
jukka-tamminen for the following reason:
The question is answered, right answer was accepted
Question by
jukka-tamminen · May 31, 2018 at 11:56 AM ·
classscriptableobjectinventoryinventory systemitems
Inventory armor wielding proplem,How to convert from derived to base
Hi, i have tried to do armor swapping script for my projet and got stuck with classes. any idea how to make funktion where you can input armor iten thats class is "Armor" an asiing it to variable with type "ArmorTorso". "Armor" is scriptable object and "ArmorTorso" is derived from it. Here is my code and proplems in it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour {
public List<Item> inventory;
private List<Armor> tempWieldetArmor;
public List<Armor> wieldetArmor
{
get { tempWieldetArmor = new List<Armor>();
if (armorChest != null)
tempWieldetArmor.Add(armorChest);
if (armorHead != null)
tempWieldetArmor.Add(armorHead);
if (armorLegs != null)
tempWieldetArmor.Add(armorLegs);
if (armorArms != null)
tempWieldetArmor.Add(armorArms);
return tempWieldetArmor;
}
}
public TorsoArmor armorChest;
public HeadArmor armorHead;
public LegArmor armorLegs;
public ArmArmor armorArms;
public Weapon weapon;
public Shield shield;
public int inventorySize = 20;
public bool WieldFromInventory( Armor armorToWield) // problem 1. how to refer armor correctly
{
Armor tempArmor = new Armor();
if (armorToWield == null)
return false;
if(armorToWield is TorsoArmor)
{
if (armorChest == null)
{
armorChest = armorToWield; // proplem 2, how to armorChest to armorToWield (convert type "armor" to "torsoarmor")
}
else
{
tempArmor = armorChest;
armorChest = armorToWield; // proplem 2, how to armorChest to armorToWield
RemoveFromInventory(armorToWield);
AddToInventory(tempArmor);
tempArmor = new Armor();
}
}
if (armorToWield is HeadArmor)
{
// chacge somne armor
}
if (armorToWield is LegArmor)
{
// chacge somne armor
}
if (armorToWield is ArmArmor)
{
// chacge somne armor
}
}
public bool AddToInventory(Item item)
{
if (inventory.Count >= inventorySize)
return false;
else
{
inventory.Add(item);
return true;
}
}
public bool RemoveFromInventory(Item item)
{
if (!inventory.Contains(item))
return false;
else
{
inventory.Remove(item);
return true;
}
}
}
Blockquote
Comment
Answer by jukka-tamminen · May 31, 2018 at 12:15 PM
problem solved, i just had to add (TorsoArmor) "to line 68 and 77" in front of "armorToWield"