Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lionel_Leeser · Dec 28, 2016 at 10:56 PM · c#programmingscriptableobjectlogicgeneric

In the need of the light of a genius !

Hi everybody,

I'm looking for the idea of a genius that I hope I'll find here. :D

So let me expose my question.

I'm actually thinking of a Object pool compatible loot system... (If it's a Shaman that I need to find my soul back just say it ;D)

I have a pool system based on a given prefab, so I create pool of a specific prefab and so on, now I want to add loot in my game. But the loot could be stuffs, potions, rubies, etc... But I don't want a pool for every possible rubies or stuff of many different kind that will eventually fall. I'm looking for a more generic solution, like having a unique generic prefab that will able to hold data for every possible loot.

So I have imaginated a whole Scriptables Objects structure with a root Loot Scriptable Object, Stuff or portions or rubies will inherit from the Loot Scriptable object. But I'm facing a wall. Would I be able to make the connection between a Loot object and a specific stuff ? I think not !

So I think a bit further and imagine to put EVERY possible data for EVERY kind of loot in a single scriptable object, and then just complete the needed fields for the kind of loot I want to create. But that seems awful to me...

Could someone genius explain how I can genericatly hold in my generic prefab, specific datas ?

Just for information I will have a Loot_Manager that will hold array or directory of every possible loot (as Scriptable Objects) to be able to access it everywhere I need to. Is that a good starting point ? I think so. Please let me know what you think about that.

I hurt myself on this because my objective is to be able to easly add new content to my game, I already have a fully functionnable ability system with Scriptable Object and this seems the best way to me to old datas for the loot as well.

Thanks in advance for your precious help.

Kind regards,

Lionel

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dpoly · Dec 29, 2016 at 12:14 AM

What you are describing is a data-driven programming methodology. It's a really good way to solve problems like this, but it usually takes quite a bit of code. And it really breaks with the Unity editor methodology.

What you do is first design a data structure that describes the data objects in your game. They could be NPCs, items loot, powerups, etc) as needed. Use classes, dictionaries, lists, whatever you need, with a clearly defined set of interfaces. You can create the data in various ways: by code, by loading an XML data file, by a custom language, etc. This is your data model.

Then you create one or more generic prefabs that act as views on the data. As needed, instantiate a prefab, connect it to a particular item in the data model and let it load whatever game objects are needed to provide a visual representation of the model item. Game logic uses the data model, but visualisation is via the data view.

This is how big systems are built, but it's not for the faint-hearted. Web search for model-view architecture to learn more.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lionel_Leeser · Jan 01, 2017 at 07:00 PM 0
Share

Hello,

Thanks for your answer.

I have looked into what you are talking about.

It's not what I was looking for but you have openned my eyes.

Thanks !

avatar image dpoly · Jan 02, 2017 at 02:47 AM 0
Share

It's often the way -- first q & a don't quite get there. So think some more and ask another question. Note: high probability your data model does not derive from $$anonymous$$onoBehaviour. So consider writing and testing your code in VS outside Unity.

avatar image
1

Answer by RobAnthem · Jan 01, 2017 at 07:50 PM

This is what I did Now keep in mind it might be missing a couple classes that don't actually pertain to the loot aspect. Essentially what I did was create a base definition of what "Item" means, the defined Equipment deriving from Item, then created a properties class so I could add dynamic stats, but I'm probably going to change that specific part. Either way I hope this gives you some decent ideas. Base item class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 namespace MadWizards
 {
     public class Item : MonoBehaviour
     {
         public string Name;
         public int value;
         public string description;
         public bool canSell;
         public bool canDestroy;
         public Sprite Icon;
         
     }
 }

Equipment Class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 namespace MadWizards
 {
     public class Equipment : Item
     {
         public GameData.EquipType slot;
         public int minValue;
         public int maxValue;
         public List<ItemProperty> properties;
         public int reqLevel;
         public bool IsOneHand;
         public GameObject model;
         public Vector3 position;
         public Quaternion rotation;
         public Equipment()
         {
             properties = new List<ItemProperty>();
         }
     }
 }
 

Item property class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 namespace MadWizards
 {
     public class ItemProperty
     {
         public GameData.PropertyType property;
         public int min;
         public int max;
     }
 }

Loot Gen class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 using UnityEngine.UI;
 
 namespace MadWizards
 {
     public static class ItemGenerator
     {
         public static LootManager Manager;
         public static Item GenerateItem()
         {
             if (Manager == null)
             {
                 LoadManager();
             }
             Equipment newLoot = GameObject.FindObjectOfType<Recycler>().gameObject.AddComponent<Equipment>();
             if (UnityEngine.Random.Range(1, 3) > 0)
             {
                 GameData.EquipType[] values = (GameData.EquipType[])GameData.EquipType.GetValues(typeof(GameData.EquipType));
                 newLoot.slot = (GameData.EquipType)values[UnityEngine.Random.Range(0, values.Length)];
                 int rarity = 0;
                 newLoot.Name = Manager.GetLootName(newLoot.slot, out rarity);
                 Debug.Log(rarity);
                 GameData.PropertyType[] propVals = (GameData.PropertyType[])GameData.EquipType.GetValues(typeof(GameData.PropertyType));
                 for (int i = 0; i < UnityEngine.Random.Range(0, 3); i++)
                 {
                     ItemProperty newProp = new ItemProperty();
                     newProp.property = (GameData.PropertyType)propVals[UnityEngine.Random.Range(0, propVals.Length)];
                     newProp.min = UnityEngine.Random.Range(5, 35);
                     newLoot.properties.Add(newProp);
                     newLoot.value += newProp.min * 2;
                 }
                 newLoot.canDestroy = true;
                 newLoot.canSell = true;
                 newLoot.value += newLoot.properties.Count * rarity * 25;
                 newLoot.reqLevel = UnityEngine.Random.Range(GameData.playerData.Player.Level - 2, GameData.playerData.Player.Level + 3);
                 newLoot.Icon = Manager.GetIcon(newLoot.slot);
                 newLoot.description = Manager.GetDescript();
             }
             return newLoot;
         }
         public static void LoadManager()
         {
             Manager = Resources.Load("ItemData", typeof(LootManager)) as LootManager;
         }
 
     }
 }

And the "Manager" aka the Item Data to pick from

 using UnityEngine;
 using System.Collections;
 using MadWizards;
 public class LootManager : MonoBehaviour
 {
     public string[] Prefix, Suffix, Descriptions;
     public string[] ItemSprites;
     public Sprite GetIcon(GameData.EquipType iconType)
     {
         return (Resources.Load(ItemSprites[UnityEngine.Random.Range(0, ItemSprites.Length)]) as GameObject).GetComponent<SpriteRenderer>().sprite;
 
     }
     public string GetLootName(GameData.EquipType slot, out int i)
     {
 
         if ((i = UnityEngine.Random.Range(0, 2)) == 0)
         {
             return Prefix[UnityEngine.Random.Range(0, Prefix.Length)] + " " + slot.ToString();
         }
         else if (i == 1)
         {
             return slot.ToString() + " " + Suffix[UnityEngine.Random.Range(0, Suffix.Length)];
         }
         else
         {
             return Prefix[UnityEngine.Random.Range(0, Prefix.Length)] + " " + slot.ToString() + " " + Suffix[UnityEngine.Random.Range(0, Suffix.Length)];
 
         }
     }
     public string GetDescript()
     {
         return Descriptions[UnityEngine.Random.Range(0, Descriptions.Length)];
 
     }
 
 }


Inspector portion

 #if (UNITY_EDITOR)
 
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI;
 using MadWizards;
 [CustomEditor(typeof(Equipment))]
 public class EquipmentInspector : Editor
 {
     public bool showProperties;
     public override void OnInspectorGUI()
     {
         Equipment Equip = target as Equipment;
         GUILayout.BeginVertical(EditorStyles.helpBox);
         GUILayout.BeginVertical(EditorStyles.helpBox);
         Equip.Name = EditorGUILayout.TextField("Equipment Name:", Equip.Name);
         Equip.Icon = (Sprite)EditorGUILayout.ObjectField("Icon:", Equip.Icon, typeof(Sprite), false);
         GUILayout.EndVertical();
         Equip.description = EditorGUILayout.TextField("Description:", Equip.description);
         Equip.value = EditorGUILayout.IntField("Value:", Equip.value);
         GUILayout.BeginHorizontal(EditorStyles.helpBox);
         Equip.canSell = EditorGUILayout.Toggle("Sellable", Equip.canSell);
         Equip.canDestroy = EditorGUILayout.Toggle("Destroyable", Equip.canDestroy);
         GUILayout.EndHorizontal();
         Equip.slot = (GameData.EquipType)EditorGUILayout.EnumPopup("Slot", Equip.slot);
         if (Equip.slot == GameData.EquipType.Weapon || Equip.slot == GameData.EquipType.Offhand)
         {
             Equip.IsOneHand = EditorGUILayout.Toggle("Is One Handed", Equip.IsOneHand);
             Equip.minValue = EditorGUILayout.IntField("Min Damage:", Equip.minValue);
             Equip.minValue = EditorGUILayout.IntField("Max Damage:", Equip.maxValue);
         }
         if (Equip.slot == GameData.EquipType.Hat || Equip.slot == GameData.EquipType.Robe || Equip.slot == GameData.EquipType.Shoes)
         {
             Equip.minValue = EditorGUILayout.IntField("Min Defense:", Equip.minValue);
             Equip.minValue = EditorGUILayout.IntField("Max Defense:", Equip.maxValue);
         }
         Equip.reqLevel = EditorGUILayout.IntField("Required Level", Equip.reqLevel);
         GUILayout.BeginVertical(EditorStyles.helpBox);
         if (GUILayout.Button("Add Property"))
         {
             Equip.properties.Add(new ItemProperty());
         }
         if (showProperties = EditorGUILayout.Toggle("Show Properties", showProperties))
         {
             foreach (ItemProperty property in Equip.properties)
             {
                 GUILayout.BeginVertical(EditorStyles.helpBox);
                 property.property = (GameData.PropertyType)EditorGUILayout.EnumPopup("Property", property.property);
                 property.min = EditorGUILayout.IntField("Min", property.min);
                 property.max = EditorGUILayout.IntField("Max", property.max);
                 GUILayout.EndVertical();
             }
         }
         GUILayout.EndVertical();
         Equip.model = (GameObject)EditorGUILayout.ObjectField("Item Model", Equip.model, typeof(GameObject), true);
         if (Equip.model != null)
         {
             Equip.position = EditorGUILayout.Vector3Field("Position", Equip.model.transform.position);
             Equip.rotation = Equip.model.transform.rotation;
         }
         GUILayout.EndVertical();
 
     }
 }
 #endif


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lionel_Leeser · Jan 03, 2017 at 06:19 PM 0
Share

Thanks @RobAnthem for your detailled answer.

Is that a common technique or have you imaginated all of this yourself ?

I like how it's done maybe this will help.

Thanks again.

avatar image RobAnthem · Jan 03, 2017 at 08:38 PM 0
Share

No problem, obviously the code would need a bit of work for you, if you want I can make a quick layout with all the actual code processes going on in it notated, if you wanted to try something like it anyway. I would imagine it is a common technique, but I didn't look up how others did it, I just thought about what an item system entails and started coding the different aspects until it worked. It took a few hours, but my biggest snag was wanting to derive my Items from monobehaviour, which led to the very interesting line at 19 of the loot generator. Now I haven't addressed what the recycler does yet, but I will assume most of it will be handled when I incorporate the sell and destroy system.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Add lines to a script from another script 0 Answers

Slowly Rotate a Circle 90 Degrees Each Click 0 Answers

Scriptable Object containing Scriptable Object 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges