- Home /
How to make method that gets multiple types?
Hi, I have some game objects called: Furnace, Sawmil, etc...
I also have a class call Item, with some data about the game blocks (it's a minecraft clone).
public class Item {
public string itemName;
public ushort itemID;
public string itemDesc;
public Texture2D itemIcon;
public Material itemMaterial;
public string itemType;
public GameObject itemPrefab;
public List<string> itemFlags;
public Interaction[] itemInteractions;
public Item(string name, ushort id, string desc, Material material, Texture2D icon = null,
string type = "None",GameObject prefab = null, List<string> flags = null,
Interaction[] interactions = null) {
itemName = name;
itemID = id;
itemDesc = desc;
itemMaterial = material;
itemIcon = icon;
itemType = type;
itemPrefab = prefab;
itemFlags = flags ?? new List<string>();
itemInteractions = interactions;
}
}
the problem is with the interactions, this holds the information of the block interactions with the machine game objects of the game, to return ingots or plancks or whatever from raw ores.
public class Interaction {
public Furnace machine; //This is the machine that process the block
public int[][] output; // A list of tuples [(itemID, count), (item2ID, count), ...] the items that return the machine
public Interaction(Furnace iMachine, int[][] iOutput) {
machine= iMachine;
output = iOutput;
}
As you can see, it only accepts instances of Furnace.
I want to make it accept instances of Furnace, Sawmill, MagicWorshop, etc. So I can make multiple machines process that block and return different outputs.
I thought of making a parent class like "Machine" and then inherit Furnace, Sawmill and so from it, but that objects already inherit from MonoBehaviour and I dont know how to make them inherit from both classes at the same time (MonoBehaviour and Machine). The next thing I though was using "dynamic" but looks like needs version 4.0 and I'm working on 3.5.
Any idea? Thanks.
Answer by IgorAherne · Jan 01, 2017 at 05:39 PM
You indeed need to inherit them from Machine, which in turn inherits MonoBehaviour
Check with the following:
Machine myMachine; //came as argument in function
Furnace machineAsFurnace = myMachine as Furnace;
if(machineAsFurnace != null){
//machine as furnace is a furnace! success.
//do something with your furnace:
machineAsFurnace.Smelt(myIngots);
return;
}
Sawmill machineAsSawmill = myMachine as Machine;
if(machineAsSawmill != null){
//do something with your sawmill
return;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dropdown resolutions menu 0 Answers
SetActive is not working 2 Answers