- Home /
Invoking Effects of (Random) Items (C#)
Scenario that I'm trying to create: at the start of a game, the player receives a couple random items from a list of possible items. There is going to be an inventory screen with which to access them, but because the items are going to have completely different effects when used, is there an easy way to call an individual method for the used item without creating absurd levels of clutter in the script?
The idea I'm currently working with is using an array to store integers, each corresponding to a possible item. At the start of the game, it will draw 2 random values, and somehow store them in the inventory (possibly a list, or another array). When an item is used, it will then (somehow) call an appropriate function. The problem is, I'm not exactly sure what the best way to do that would be.
Would I have to create a specific 'itemUse' script to put on the player object with a method named after each item and then have the inventory call that method on use? I apologize for the very ignorant question, but I've only been working with C# for a few days, and am pretty stumped about where to start. Most inventory tutorials I've found are more about the interface, and less about the actual mechanics of using (/calling) an item's effect.
This idea just occurred to me as well: what about using a hashtable to store a number and name of an item (for randomly drawing). When the item is added to the inventory (maybe a gameObject, but of generic class 'item'), it sets the name of that object to the name taken from the hashtable. Then, on use, it would execute a method matching its name within the Item class? I think I may be over (or under) thinking this.
Also, I'm planning on there being several types of items (ie, fruit, tools, books, etc.) that all behave differently than each other. Would I need to use multiple arrays in order to accommodate that?
So yeah, I'd love to here some advice from people who aren't stupid like I am.