0
Answered

Instantiating items from Loot Table

F0X 4 months ago in Inventory updated by Marti (Lead Developer) 3 months ago 5

This post is a follow up from the discord question, but I'm trying to get a 3D, instantiated objects to spawn from the loot table (as opposed to appearing directly in an inventory or container). I've been trying to combine the logic from the Loot Table Action, and the Instantiate Item Action, but I'm a beginner and this is a little over my head. (apologies if there is a better way to post code here, I thought this would be better than a screenshot)

namespace GameCreator.Inventory
{
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Events;
    using GameCreator.Core;

    #if UNITY_EDITOR
    using UnityEditor;
    #endif

    [AddComponentMenu("")]
    public class ActionLootTableInsantiate : IAction
    {
        public LootTable lootTable;

        // EXECUTABLE: ----------------------------------------------------------------------------

        public override bool InstantExecute(GameObject invoker, IAction[] actions, int index)
        {
            {
                if (this.lootTable == null) return true;
                LootTable.LootResult loot = this.lootTable.Get();

                if (loot.item != null && loot.amount > 0)

                    // Loot Table Field

                    void retrieveLoot(

                    var result = this.lootTable.Get());
                result.item.uuid;
                result.amount;

                return true;
            }

            {
                if (this.result != null && this.result.item.prefab != null)

                // Instantiate Field
                {
                    Vector3 position = invoker.transform.position;
                    Quaternion rotation = Quaternion.identity;

                    Instantiate(this.result, position, rotation);
                }

                return true;
            }

            /// {
            /// One More Field to Add Components(To Pickup, Animate Scattering Within Radius, Float, Etc.)
            /// } 
        }
        // +--------------------------------------------------------------------------------------+
        // | EDITOR |
        // +--------------------------------------------------------------------------------------+

        #if UNITY_EDITOR
        public const string CUSTOM_ICON_PATH = "Assets/Plugins/GameCreator/Inventory/Icons/Actions/";

        public static new string NAME = "Custom/Loot Table Insantiate";
        private const string NODE_TITLE = "Example value is {0}";

        // PROPERTIES: ----------------------------------------------------------------------------

        private SerializedProperty spExample;

        // INSPECTOR METHODS: ---------------------------------------------------------------------

        public override string GetNodeTitle()
        {
            return string.Format(NODE_TITLE, this.example);
        }

        protected override void OnEnableEditorChild ()
        {
            this.spExample = this.serializedObject.FindProperty("example");
        }

        protected override void OnDisableEditorChild ()
        {
            this.spExample = null;
        }

        public override void OnInspectorGUI()
        {
            this.serializedObject.Update();

            EditorGUILayout.PropertyField(this.spExample, new GUIContent("Example Value"));

            this.serializedObject.ApplyModifiedProperties();
        }

        #endif
    }
}

Unity version:
2019.2.16
Game Creator version:
1.1.2

Answer

+1
Answer
Answered

Hi!

I've edited your topic to wrap the code in code blocks. Hope that's okay with you. Regarding the Action, I've taken yours as a template and modified it a bit: Basically you were calling the "Loot Table" Get() method twice, getting two random items each time the Action was executed.

Also, when you get a random amount of a certain item, you'll randomly instantiate each of them around a certain radius. You can import the attached file into Unity.

action-inventory.unitypackage

PS: I haven't tested the code but should work fine. If there's any error, please let me know and I'll take a more thorough look :-) Cheers!

GOOD, I'M SATISFIED

Thanks so much!

Satisfaction mark by F0X 4 months ago
+1
Answer
Answered

Hi!

I've edited your topic to wrap the code in code blocks. Hope that's okay with you. Regarding the Action, I've taken yours as a template and modified it a bit: Basically you were calling the "Loot Table" Get() method twice, getting two random items each time the Action was executed.

Also, when you get a random amount of a certain item, you'll randomly instantiate each of them around a certain radius. You can import the attached file into Unity.

action-inventory.unitypackage

PS: I haven't tested the code but should work fine. If there's any error, please let me know and I'll take a more thorough look :-) Cheers!

I'm getting " error CS0034: Operator '+' is ambiguous on operands of type 'Vector3' and 'Vector2' "

I'm not sure why, I guess Random.insideUnitCircle is Vector2 but the documentation on it specifically says assigning a Vector2 to a Vector3 is fine. 

+1

Figured it out! Just needed to to change the random.insideUnitCircle to (Vector3)Random.insideUnitCircle.

Hey sorry for the late reply. Happy to hear you found the solution :-)