- Home /
Inventory / inhertance driving me insane - help with theory please.
Ok, I am trying to make an inventory system and I am going in circles. I think if I add one more GetComponent call into my code my computer might burst into flames immedietely. I thought about just stealing one but I really want to understand this. This thread isnt really about how to make the actual OnGUI or anything its about the inheritance and setting up the scripts and the code.
I am not asking for code I just need to understand the flow, and get it through my apparently thick head.
So what I have so far is this:
First I have a script(class) called Item. This script outlines the basic features of an item: Name, cost, description. Ok so far so good.
If I understand correctly, I can then have other scripts, weapon for example, that inherit from item and further give more qualities of the item - damage, for example. The item then also inherits the basic traits, cost, name description which I set in the item script I believe? Ok I get that I think.
So now, I have a script called "Item Manager" which is where I create my list of Items: `public List PlayerInventory = new List();` And so far I have been putting the functions in here for adding to this list. So for example, if I click a button or something it adds a new Item to the inventory list and sets the parameters: Basically like this:
Item newItem = new Item();
newItem.name = new ItemName; // Declared at top of this script.
//etc
//etc
PlayerInventory.Add(newItem);
I guess this is where I get lost:
My biggest question is this: If I have a gameObject in my scene that has a script on it that does a bunch of stuff, how can I set this to be of a type weapon, for example or of type Armor? For example, I have an arrow that gets fired in my scene by the player. It has a script that does a bunch of stuff so I feel like I cant make it inherit from the Item script or all that wont work(because its not inheriting from mono). How do I set it to be of type: Item or type: weapon?
When I then click on the gameObject, how can I tell what kind of object it is? I guess that depends on the answer to the above. I "think" if I can somehow make it inherit from "item" I can use gameobject.GetType I believe? If I cant inherit though I have no idea. I tried to set a variable string called type on the object and then access that but thats making no sense and obviously the wrong way to go about it.
How would you recommend adding gameObjects into the List(Player Inventory)? Right now I have a empty gameobject which holds the script ItemManager which adds the items. Should I just access those AddItem functions using GetComponent to add the Item when my player clicks on an object? Or should I put the script on the player themselves? I see everyone using the empty gameobject with a "inventory manager" of sorts and I am not sure why they go this direction.
Whew I am rambling here I guess I just need some direction on how to manage all this! Feel free to direct me to a link that describes this flow but I havent found anything yet :(
$$anonymous$$ad$$anonymous$$ike, massively step back and relax :)
learn the basics of finding other components etc -- what about unityGE$$anonymous$$S.com for a massive intro
You mention that you "use getcomponent a lot" - well that's normal, i use it in every line of code.
there is a caveat to that - once you know what you're doing, just GET A REFERENCE TO that other item, and then use that reference.
"$$anonymous$$y biggest question is this: If I have a gameObject in my scene that has a script on it that does a bunch of stuff, how can I set this to be of a type weapon, for example or of type Armor"
just learn about ENU$$anonymous$$, it is very simple
"how can I tell what kind of object it is?"
it would be nothing more than a variable called "WhatTheFuckAmI" ? and that would be an ENU$$anonymous$$. learn about ENU$$anonymous$$ immediately (it is dead simple)
regarding your second last paradgraph I really don't understand it at all, but in a word just use an ARRAY. (in fact -- just to confuse you never for any reason LITERALLY use an array in unity, just use the "List" form - if you don't know about that look it up. again unityGE$$anonymous$$S.com)
hope it helps in some way
Actually he wants inheritance because the class Weapon has more parameters than the class Item.
let me be sure of something : your inheritance of item would be like this? $$anonymous$$onoBehaviours <- Item <- Weapon <- Arrow
If so, when you get the gameobject, use GetComponent (c# syntax) to get the first component of type item. Then use component.GetType() == typeof(Weapon) to check if it's a weapon for example.
Hi and thank you both for the answers. I will look into enums. Tourist you are correct that on the inheritance - mono - item - weapon - arrow just as an example. But I can't inherit arrow from weapon can I or won't all my update functions and what not stop working in my arrow script? Or maybe because item originally comes from mono it will be ok?
Tourist on your last paragraph of I understand correctly you are saying when I get the game object as in I click on it with on mouse down or something. Then I can use if GameObject get type == type of? That's what I was thinking but it will only work of o inherit from weapon. Right? And I guess that's where I am not sure if I can do that or not.
Answer by Tourist · Dec 14, 2012 at 03:40 PM
A gameobject is a gameobject. A component is something you can add to a gameobject to add some behaviours. This is the part you can control, and use inheritance on. Here is a sample of code. The function 'GetYourObjectOnCLick' is just there to simulate the way you are getting the gameobject.
GameObject obj = GetYourObjectOnCLick();
Item item = obj.GetComponent<Item>();
if(item.GetType() == typeof(Weapon))
{
// do your stuff there
}
else if(item.GetType() == typeof(Ammo))
{
// do other stuff there
}
else
{
// do something else
}
Ok makes sense but wouldn't i have to have the item script attached to my GameObject for this to work? What if I have the weapon script attached, will it still work since weapon inherits from item?
That's the purpose of inheritance. If you attach the weapon script to your gameobject, then you don't have to attach item as well because Weapon is already an Item (due to inheritance).
I suggest you make some research on your own about inheritance in any program$$anonymous$$g language. This will help you to structure your code. Here is the article about inheritance on Wikipedia : http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_program$$anonymous$$g%29
But you can find others explanations.
Thanks again I marked as answered. Sounds like I have some more reading to do :)
You simply need this inheritance chain:
Item --inherits from--> $$anonymous$$onoBehaviour
Weapon --inherits from--> Item
Ammo --inherits from--> Item
A$$anonymous$$47 --inherits from--> Weapon // just an additional example
therefore a Weapon is also an Item and also a $$anonymous$$onoBehaviour.
$$anonymous$$eep in $$anonymous$$d that when Item is derived from $$anonymous$$onoBehaviour it can't be created with new like you did in your question. It is a component and always have to be attached to a GameObject. But since it's an item in the game this makes perfectly sense.
Hmm. Interesting because ya I have been getting a warning that I can't use new when it inherits from mono. I guess that's why. So if I have a script for an item call arrow, that tells it to move forward and all that, and inherits from mono, would it be better to just create a new script that inherits from item and attach that to my arrow prefab that way my arrow is of type item?
Ins$$anonymous$$d of just trying to add in the new command into my existing arrow script?