- Home /
Inventroy Problem- Using different parameters in inherited classes
I have a main class "Item" and then I have classes like food and drinking which inherit from the class Item. But i have a problem with using different voids of my healthsystem to add specific values.
Item main class
using UnityEngine;
using System.Collections;
public class Item : MonoBehaviour {
virtual public void UseItem()
{
}
}
Food class
using UnityEngine;
using System.Collections;
public class Food : Item {
public int amount;
override public void UseItem(HealthSystem h)
{
h.GetFood(amount);
}
}
class for drinks
using UnityEngine;
using System.Collections;
public class Drink: Item {
public int amount;
override public void UseItem(HealthSystem h)
{
h.GetDrink(amount);
}
}
using UnityEngine;
using System.Collections;
public class Tool: Item {
public int strength;
override public void UseItem(Motor m, int a)
{
Repair(m,a)
}
public void Repair(Motor m, int a)
{
m.condition+=a;
}
}
Is this in antoher way possible?
Answer by Gnometech · Feb 10, 2014 at 07:30 PM
The problem is that the overriding method has to have the same "signature" as the base method, i.e. the number and type of parameters need to match.
You have two options:
1) Use the method as you declared it (without parameters) and also override it without parameters. Instead store the things you need in a different variable that can be accessed from the method. For instance, if your HealthSystem is constant in the game (or does not change often), you could store it in a private variable of your Food and Drink classes and refer to it. Another possibility is to let the class "pull" the needed value from somewhere else.
2) If you definitely need parameters because they change often create a custom class (e.g. "ItemUse") with fields for all possible parameters (HealthSystem, Motor, int, etc.) and pass an instance of this class with the correct type of parameters set. This way the signature stays the same across all implementations:
virtual public void UseItem(ItemUse use)
Which of the options you take depends on the circumstances how often parameters set and if it is feasible to pass them to the class in a different way.
Let's see... just as an example.
public class ItemUse
{
public HealthSystem h;
public $$anonymous$$otor m;
public int x;
// Use whatever else you need
// Constructor 1
public ItemUse (HealthSystem hs)
{
this.h = hs;
}
//Constructor 2
public ItemUse($$anonymous$$otor mot, int a)
{
this.m = mot;
this.x = a;
}
}
This will create the class. When you call the UseItem method for food or drink, now you pass the healthsystem directly. Ins$$anonymous$$d, you would call it like
bottle.UseItem(new ItemUse(h));
where h is your healthsystem. Similarly for the tool you would pass a new ItemUse with the motor and int value.
Thank you, that semms to work. Last question, When the food class has a value the item class dont have. how to i acess it in my inventory(list of items) ? Deel
There are several ways to do this. One is to try to cast your item to food and access the value only if it works.
Like this, supposing your Item variable is just called "item":
Food food = item as Food;
if (food != null)
{
// Access value here, because "food" has the correct type
}
The trick is that the "item as Food" statement will return the same item cast to "Food" if it is an instance of the class. Any other item that is not of class food will return null. That is why the check is necessary.
With this method you can safely loop through all your items.
If my above answer works I would appreciate if you could flag it as correct. :-) Thanks.
Your answer
Follow this Question
Related Questions
why cant I see more than one perameter in gameobjects 1 Answer
Calling 'AddItem' Method giving me error NullReferenceException: 0 Answers
Inventory / inhertance driving me insane - help with theory please. 1 Answer
How to ignore base class? 1 Answer
What's the "right" way to deal with inheritance and constructor parameters? 1 Answer