- Home /
How to cast base class as child using base class enum
I have parent item..it contains itns and bools and that class has enum....(quest, equipment etc... Now in other class called Equipment Show..im using prefab to show some text and item...
Now question is how to make item that is base class to a child class of equipment based of this
if (CLone.itemtype == Item.ItemType.Equipment)
{
CLone = Instantiate(item) as Equipment;
}
i tried to and failed.
I'm not sure I understand your object layout. I sense there may be some confusion as to what is a base class and what is a parent gameObject, but I may be reading that incorrectly. Show the object derivation (the opening lines of the class declaration), so we can see what is a child and what is a parent.
$$anonymous$$y point here is that the language used does confuse. A child is typically a gameObject owned by another gameObject, the owner is the parent. Classes are derived from base classes, but we don't typically reference them as parents and children. They are base or derived classes (derived from the base).
Item CLone; void Start() {
if (CLone.itemtype == Item.ItemType.Equipment)
{
//do sometring with equipment or show values
//text.text = CLone.$$anonymous$$axDurability
}
else if(CLone.itemtype == Item.ItemType.Gems)
{
//do sometring with equipment or show values
//text.text = CLone.GemRank
}
///and so on
basicly cast Clone as diffrent class that has those varibles for that class but all are derived form single parent called item. So if enum says its gem, then cast it as an gem and display to screen its varibles and pictures
Answer by Glurth · Jul 17, 2018 at 05:57 PM
It sounds like the goal is to be able to display equipment variables for equipment tooltips, and quest variable for quest tooltips, etc...
This is WHY you created a base class. Define a ToolTip function in your base class, then "override" this function in your derived classes, to display the stuff appropriate to them. (lookup the c# override keyword, for more details.)
Now when you call "Tooltip" on a BItem (your baseclass), you don't need to know what type it is- the correct version of tooltip is called automatically.
pseudo code:
class ItemBase
{
virtual string Tooltip(){ return "";}
}
class Equipment: ItemBase
{
float weight;
override string Tooltip(){ return "Equipment weight: "+ weight.ToString();}
}
class Quest: ItemBase
{
float XP;
override string Tooltip(){ return "Quest XP value: "+ XP.ToString();}
}
usage:
ItemBase a= new Equipment();
ItemBase b= new Quest();
string s=a.Tooltip(); // s will contain "Equipment wieght"
string t=b.Tooltip(); // t will contain "Quest XP value"
Answer by ShadyProductions · Jul 16, 2018 at 09:12 PM
Following your logic, an Item can never be an equipment but an equipment can be an item. This make sense because if you have a car base class, and a honda and citroen,
A honda can be a car, but a honda can never be a citroen. A honda has specific attributes, fields, properties that a base car doesn't have so it makes sense that a honda can't be a car, but a car can be a honda, because a honda has the base attributes, fields, properties of a car.
You will need to make a constructor to pass your item and set the correct fields and properties that you need
so new Equipment(item);
etc
But seeing that item is a monobehaviour so will equipment be so can't you just do Instantiate(equipment);
public class BItem
{
public ItemType ztype;
public enum ItemType
{
Equipments,
Gems,
Quest
//etc
}
}
public class Equipmentz : BItem
{
public EquipmentType EType;
public enum EquipmentType
{
Armor,Helm, Boots, Weapon, Null ///etc
}
public Equipmentz()
{
ztype = ItemType.Equipments;
}
}
public class Weapon : Equipmentz
{
public Weapon()
{
EType = EquipmentType.Weapon;
}
}
public class Armorz : Equipmentz
{
public Armorz()
{
EType = EquipmentType.Armor;
}
}
public class LegGem : BItem
{
public LegGem()
{
ztype = ItemType.Gems;
}
}
Here is structure of item... So i can create item of type equipment and say
If( ztype = ItemType.Gems)
{
//Cast it as Gem Class
// display gem varibles class to a tooltip
{
Else if (If( ztype = ItemType.Equipmentz)
{
//cast it as Equipmentz class.
// display Equipmentz class varibles to a tooltip
}
and so on.. so if items is hovered based on itemtype...and other stuff show me a tool tip.
so if i have sword and gem...show me sword on hover...and when i hover gem show me gem. Gem can't be sword and other way around...they all have in common is base class Baseitem. and itemtype {equipment, gem, potion) and other attribures such as name, staksize, isstackable, itemvalue..and so on.
You can't cast a derived class to a base class, or any other derived class. So not possible.
Your answer
Follow this Question
Related Questions
Shadow To Ignore Specific Objects? 1 Answer
AssetBundle.Load() to Texture2D 2 Answers
Raycasting is not working! 2 Answers
IntVector2 Struct useful? 1 Answer
Casting Object to Rigidbody 2 Answers