![]() ![]() If you raised one eyebrow already, prepare to raise the other, because there is a very high chance that your game designer will, at some point, try to balance the game and give a discount to all low-level potions. If we stick to the ItemStruct approach, it is probably the case now that the different instances of the merchants now have copies of the same ItemStructs. For the sake of supporting slower players, the more advanced areas in the game have merchants still selling starting potions that cost less than their higher-level alternatives. ![]() ![]() One of them, in the starting area, is supposed to sell low-level potions and weak temporary buffs. However, your merchant system allows for different merchants across the game world. The serializable structs provide an easy way of making changes via the Inspector, while also using the advantages of being a struct in C# such as being more adequate for smaller data aggregation compared to classes, and for having value semantics. This approach is actually quite versatile, and for many simple systems, it is certainly the easiest and more stable way to go. The code and inspector for such a system can be seen below: Thus, via the inspector, the game designer can input each one of these items manually while leaving you to program the other systems for the game. Then, you would add a List of ItemStruct in the merchant. I believe the initial attempt to tackle this system is to use a serializable struct that encapsulates the items’ properties. Additional advantages of scriptable objects.In this article, we’re going to be exploring fast prototyping in Unity and how you can use scriptable objects effectively in your game development. As the lead programmer, you need to decide where to store each one of these items in your system, so where would you? The game designer in your team already wrote a 100-page document on various items with an entire chapter dedicated to the multiple potion types. Fast prototyping in Unity: Scriptable objectsĬonsider that you are developing a merchant system for your game in Unity, where the merchant sells a list of items with a different name, price, description, and image. You can also create a custom Editor to define the look of the inspector for your type to help manage the data that it represents.Yvens Rebouças Serpa Follow I am a Brazilian Nordestino in the Netherlands working as a Game Dev & Design teacher. Tip: When working with ScriptableObject references in the inspector, you can double click the reference field to open the inspector for your ScriptableObject. Once you have defined a ScriptableObject-derived class, you can use the CreateAssetMenu attribute to make it easy to create custom assets using your class. Please see the scripting reference for examples. Your shop script would reference a ShopContents object that defines what items are available. In a scenario where the game has three zones, each zone could offer different tier items. You could create multiple assets of your custom ShopContents ScriptableObject, each defining a set of items that are available for purchase. An example of this would be to imagine a NPC shop in a RPG game. The intended use case for using ScriptableObject is to reduce memory usage by avoiding copies of values, but you could also use it to define pluggable data sets. 10 instances of a prefab that has a reference to a ScriptableObject, that holds 4MB data, would total to roughly 4MB and not 40MB as discussed in the other example. The ScriptableObject in turn stores the array. If you have a script with a reference to the ScriptableObject with the million integers, Unity will only store a reference to the ScriptableObject in the script data. ScriptableObject fields, or any UnityEngine.Object fields, such as MonoBehaviour, Mesh, GameObject and so on, are stored by reference as opposed to by value. The instances are thought to own their individual data. This means that if you created a ScriptableObject and stored the million integers in an array it declares then the array will be stored with that instance. Unity serializes all primitive types, strings, arrays, lists, types specific to Unity such as Vector3 and your custom classes with the Serializable attribute as copies belonging to the object they were declared in. If you created 10 game objects, then you would end up with 40MB of array data for the 10 instances. Each time you instantiate that prefab, you will get a copy of that array. The array occupies 4MB of memory and is owned by the prefab. Consider for example that you have made a prefab with a script which has an array of a million integers. Do not confuse this class with the similarly named SerializableObject, which is an editor class and fills a different purpose. ScriptableObject is a class that allows you to store large quantities of shared data independent from script instances. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |