|
Title: RPG Inventory with Unity Post by: newobj on October 20, 2013, 09:46:43 AM I'm curious what the "right" way to do inventory in an RPG with Unity is. Not interested in being pointed at something I can buy, I want to *learn*. I've done this before in C++/Java/etc, but Unity's different. The items have a representation in the world as a GameObject but it's not clear to me if the "Unity way" to do things is to keep the GameObjects around but hidden when the items are picked up, or to turn them into some other representation. Seems like a lot of wasted/duplicated code so I was hesitant to do that. Not sure if the answer depends on whether you're doing save/load at any time, etc.
Thanks for the help! Title: Re: RPG Inventory with Unity Post by: Brainswitch on October 22, 2013, 06:44:36 AM It depends on a lot of things, and you can do it in a lot of ways. Here is a short explanation of how I am doing it in my current project:
For each type of item (as in weapon, armor etc) I have two classes: One is the data, which is a class that has all the actual information and statistics. Like name and for example damage stats for weapons. This class is not a Monobehaviour. Then I have a sort of 'controller' class, which is a Monobehaviour and it contains the code for how the item interacts with the world and one of the variables in this class points to the data class. Like: Player GameObject has a Weapon (I actually have separate classes for Melee and Ranged weapons) controller class which has a data variable that points to an WeaponData class that has Broadsword loaded in it. So when it comes to inventory - the inventory only contains the data classes, and when the Player wants to equip a new weapon I just switch out the Player GameObjects Weapon controller data variable to the new weapon. TLDR; I separate data and monobehaviour classes for items. Title: Re: RPG Inventory with Unity Post by: Saishy on October 22, 2013, 07:26:15 AM If you take Unreal example, weapons and weapons attachment are two separate things.
Meaning you have: - Pawn (Character) - Inventory Manager - Weapon - Weapon Attachment Weapon have the logic, when the inventory manager ask it to equip it calls the Weapon equip logic that then shows the weapon attachment and sync animation. Title: Re: RPG Inventory with Unity Post by: newobj on October 25, 2013, 07:38:46 AM It depends on a lot of things, and you can do it in a lot of ways. Here is a short explanation of how I am doing it in my current project: For each type of item (as in weapon, armor etc) I have two classes: One is the data, which is a class that has all the actual information and statistics. Like name and for example damage stats for weapons. This class is not a Monobehaviour. Then I have a sort of 'controller' class, which is a Monobehaviour and it contains the code for how the item interacts with the world and one of the variables in this class points to the data class. Like: Player GameObject has a Weapon (I actually have separate classes for Melee and Ranged weapons) controller class which has a data variable that points to an WeaponData class that has Broadsword loaded in it. So when it comes to inventory - the inventory only contains the data classes, and when the Player wants to equip a new weapon I just switch out the Player GameObjects Weapon controller data variable to the new weapon. TLDR; I separate data and monobehaviour classes for items. Thanks. I was considering this approach. But I had one question; how do you set up the Data class in the editor? Since those variables are not properties on the Weapon component itself... ? Title: Re: RPG Inventory with Unity Post by: newobj on October 25, 2013, 07:59:50 AM Oh, I see, I just make WeaponData Serializable and Unity is smart enough to automatically surface it. Man, Unity rocks! :gentleman:
|