Home > Software design >  cannot convert from 'System.Collections.Generic.Queue<System.Collections.Generic.KeyValuePai
cannot convert from 'System.Collections.Generic.Queue<System.Collections.Generic.KeyValuePai

Time:08-06

I have this error when i try to complile this C# code on my Unity:

Assets\Scripts\Cauldron.cs(50,57): error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.Queue<System.Collections.Generic.KeyValuePair<PotionCraft.PotionRecipe.Ingredient, uint>>' to 'System.Collections.Generic.Queue<System.Collections.Generic.KeyValuePair<Ingredient, uint>>'

This is the code

using System.Collections.Generic;
using UnityEngine;

namespace PotionCraft.Components
{
    public interface IBrewingCauldron
    {
        public void AddIngredient(PotionRecipe.Ingredient ingredient);
        public GameObject BrewPotion();
    }


    [RequireComponent(typeof(SphereCollider))]
    [RequireComponent(typeof(Rigidbody))]
    public class Cauldron : MonoBehaviour, IBrewingCauldron
    {
        public Dictionary<PotionRecipe.Ingredient, uint> Ingredients { get; private set; } = new();

        [SerializeField] private SphereCollider cauldronCollider;
        private readonly PotionBrewer _potionBrewer = new();
        private uint _numberOfIngredients;

        private void Awake()
        {
            cauldronCollider ??= GetComponent<SphereCollider>();
            // Set the collider as trigger to interact with ingredients GameObject
            cauldronCollider.isTrigger = true;
        }


        public void AddIngredient(PotionRecipe.Ingredient ingredient)
        {
            // Keep track of the number of ingredients added
            _numberOfIngredients  ;

            if (!Ingredients.ContainsKey(ingredient))
            {
                Ingredients[ingredient] = 1;
            }
            else
            {
                Ingredients[ingredient]  ;
            }
        }

        public GameObject BrewPotion()
        {
            var ingredientQueue = new Queue<KeyValuePair<PotionRecipe.Ingredient, uint>>(Ingredients);

            var potionObject = _potionBrewer.MakePotion(ingredientQueue, _numberOfIngredients);

            if (potionObject is not null)
            {
                Debug.Log($"We made a {potionObject.name} !");
                potionObject.transform.position = transform.position;
            }
            else
            {
                Debug.Log("We failed to make any potion !!!");
            }

            Ingredients = new Dictionary<PotionRecipe.Ingredient, uint>();
            _numberOfIngredients = 0;

            return potionObject;
        }
    }
}

Edit: This is another code might be related to the problem

using UnityEngine;

namespace PotionCraft.Components
{
    public class Ingredients : MonoBehaviour
    {
        [SerializeField] private GameObject cauldronGameObject;

        [SerializeField] private PotionRecipe.Ingredient ingredient;

        private SphereCollider _cauldronCollider;

        private IBrewingCauldron _cauldron;

        private void Awake()
        {
            if (cauldronGameObject is not null)
            {
                _cauldron = cauldronGameObject.GetComponent<IBrewingCauldron>();

                if (_cauldron is not null) return;
            }

            var ingredientObject = gameObject;
            ingredientObject.name  = " [IN ERROR]";
            ingredientObject.SetActive(false);

            throw new MissingComponentException($"{ingredientObject.name} is missing the cauldron gameobject");
        }

        private void Start()
        {
            _cauldronCollider = cauldronGameObject.GetComponent<SphereCollider>();

            gameObject.name = ingredient.ToString();
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other != _cauldronCollider) return;

            _cauldron.AddIngredient(ingredient);
            Destroy(gameObject);
        }
    }
}

So what am I doing wrong here?

CodePudding user response:

The PotionBrewer.MakePotion method accepts an argument of type Queue<KeyValuePair<Ingredient, uint>>.

You are trying to pass it an argument of type Queue<KeyValuePair<PotionCraft.PotionRecipe.Ingredient, uint>> instead.

If PotionBrewer.MakePotion is supposed to accept an argument of the latter type, you might need to add this using alias to your PotionBrewer class:

using Ingredient = PotionCraft.PotionRecipe.Ingredient;

  • Related