Exemplo n.º 1
0
        /// <summary>
        /// <para>Attampts to consume the indicated number of items from the instance provided.</para>
        /// <para>Note this method must look up the instance's related Item Definition which can take time and can be error prone. It is recomended that you provide the ItemDefinition with your call to the consume method.</para>
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="count"></param>
        public void ConsumeItem(SteamItemInstanceID_t instanceId, int count)
        {
            var itemDefinition = ItemDefinitions.FirstOrDefault(p => p.Instances.Any(i => i.m_itemId == instanceId));

            if (itemDefinition == null)
            {
                Debug.LogError("Unable to locate the Item Definition for Item Instance " + instanceId.m_SteamItemInstanceID.ToString());
                return;
            }

            var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == instanceId);
            var result = SteamworksPlayerInventory.ConsumeItem(instanceId, (status, results) =>
            {
                if (status)
                {
                    itemDefinition.Instances.RemoveAll(p => p.m_itemId == target.m_itemId);
                    target.m_unQuantity -= System.Convert.ToUInt16(count);
                    itemDefinition.Instances.Add(target);
                    ItemInstancesUpdated.Invoke();
                    ItemsConsumed.Invoke(status, results);
                }
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// <para>Consumes the indicated number of units of this item from this specific instance stack</para>
        /// <para>NOTE: this is the most efficent way to consume multiple units of an item at a time.</para>
        /// </summary>
        /// <param name="itemDefinition"></param>
        /// <param name="instanceId"></param>
        /// <param name="count"></param>
        public void ConsumeItem(InventoryItemDefinition itemDefinition, SteamItemInstanceID_t instanceId, int count)
        {
            var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == instanceId);
            var result = SteamworksPlayerInventory.ConsumeItem(instanceId, (status, results) =>
            {
                if (status)
                {
                    itemDefinition.Instances.RemoveAll(p => p.m_itemId == target.m_itemId);
                    target.m_unQuantity -= System.Convert.ToUInt16(count);
                    itemDefinition.Instances.Add(target);
                    ItemInstancesUpdated.Invoke();
                    ItemsConsumed.Invoke(status, results);
                }
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// <para>Attempts to consume the requested units of the indicated item</para>
        /// <para>NOTE: this may need to iterate over multiple instances and may need to send multiple consume requests any of which may fail and each of which will trigger an Item Instance Update event call.</para>
        /// <para>You are recomended to use the the SteamItemInstance_t overload of this method when consuming more than 1 unit of an item.</para>
        /// </summary>
        /// <param name="itemDefinition">The item to consume for</param>
        /// <param name="count">The number of item units to try and consume</param>
        public void ConsumeItem(InventoryItemDefinition itemDefinition, int count)
        {
            if (count < 1)
            {
                Debug.LogWarning("Attempted to consume a number of items less than 1; this is not possible and was note requested.");
                return;
            }

            List <ExchangeItemCount> counts = new List <ExchangeItemCount>();
            int currentCount = 0;

            foreach (var details in itemDefinition.Instances)
            {
                if (details.m_unQuantity >= count - currentCount)
                {
                    counts.Add(new ExchangeItemCount()
                    {
                        InstanceId = details.m_itemId, Quantity = System.Convert.ToUInt32(count - currentCount)
                    });
                    currentCount = count;
                    break;
                }
                else
                {
                    counts.Add(new ExchangeItemCount()
                    {
                        InstanceId = details.m_itemId, Quantity = details.m_unQuantity
                    });
                    currentCount += details.m_unQuantity;
                }
            }

            bool noErrors = true;

            foreach (var exchange in counts)
            {
                var result = SteamworksPlayerInventory.ConsumeItem(exchange.InstanceId, exchange.Quantity, (status, results) =>
                {
                    if (status)
                    {
                        var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == exchange.InstanceId);
                        itemDefinition.Instances.RemoveAll(p => p.m_itemId == exchange.InstanceId);
                        target.m_unQuantity -= System.Convert.ToUInt16(exchange.Quantity);
                        itemDefinition.Instances.Add(target);
                        ItemInstancesUpdated.Invoke();
                        ItemsConsumed.Invoke(status, results);
                    }
                });

                if (!result)
                {
                    noErrors = false;
                    Debug.LogWarning("Failed to consume all requested items");
                    break;
                }
            }

            if (!noErrors)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This cannot be undone and will remove items from the palyer's inventory
        /// Be very sure the player wants to do this!
        /// </summary>
        /// <param name="count"></param>
        public void Consume(int count)
        {
            if (Count > count)
            {
                var ConsumedSoFar = 0;

                List <SteamItemDetails_t> Edits = new List <SteamItemDetails_t>();

                foreach (var instance in Instances)
                {
                    if (count - ConsumedSoFar >= instance.m_unQuantity)
                    {
                        //We need to consume all of these
                        ConsumedSoFar += instance.m_unQuantity;
                        SteamworksPlayerInventory.ConsumeItem(instance.m_itemId, instance.m_unQuantity,
                                                              (status, results) =>
                        {
                            if (!status)
                            {
                                Debug.LogWarning("Failed to consume (" + instance.m_unQuantity.ToString() + ") units of item [" + instance.m_iDefinition.m_SteamItemDef.ToString() + "]");
                                SteamworksInventorySettings.Current.ItemsConsumed.Invoke(status, results);
                            }
                        });

                        var edit = instance;
                        edit.m_unQuantity = 0;
                        Edits.Add(edit);
                    }
                    else
                    {
                        //We only need some of these
                        var need = count - ConsumedSoFar;
                        ConsumedSoFar += need;
                        SteamworksPlayerInventory.ConsumeItem(instance.m_itemId, Convert.ToUInt32(need),
                                                              (status, results) =>
                        {
                            if (!status)
                            {
                                Debug.LogWarning("Failed to consume (" + need.ToString() + ") units of item [" + instance.m_iDefinition.m_SteamItemDef.ToString() + "]");
                            }

                            if (SteamworksInventorySettings.Current != null)
                            {
                                SteamworksInventorySettings.Current.ItemsConsumed.Invoke(status, results);
                            }
                        });

                        var edit = instance;
                        edit.m_unQuantity -= Convert.ToUInt16(need);
                        Edits.Add(edit);

                        break;
                    }
                }

                //Manually update our instances to account for the quantity changes we expect to see
                foreach (var edit in Edits)
                {
                    Instances.RemoveAll(p => p.m_itemId == edit.m_itemId);
                    Instances.Add(edit);
                }
            }
        }