Forum begins after the advertisement:

 


[Part 15] Cannot convert from WeaponData to ItemData

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 15] Cannot convert from WeaponData to ItemData

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #18344
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    0
    ::
    using UnityEditor.Experimental.GraphView;
    using UnityEngine;
    
    public abstract class Weapon: Item {
        [System.Serializable]
        public struct Stats {
            public string name, description;
    
            [Header("Visual")]
            public Projectile projectilePrefab;
            //public Aura auraPrefab;
            public ParticleSystem hitEffect;
            public Rect spawnVariance;
    
            [Header("Value")]
            public float lifespan;
            public float damage, damageVariance, area, speed, cooldown, projectileInterval, knockback;
            public int number, piercing, maxInstances;
    
            public static Stats operator + (Stats s1, Stats s2) {
                Stats result = new Stats();
                result.name = s2.name ?? s1.name;
                result.description = s2.description ?? s1.description;
                result.projectilePrefab = s2.projectilePrefab ?? s1.projectilePrefab;
                //result.auraPrefab = s2.auraPrefab ?? s1.auraPrefab;
                result.hitEffect = s2.hitEffect == null ? s1.hitEffect : s2.hitEffect;
                result.spawnVariance = s2.spawnVariance;
                result.lifespan = s1.lifespan + s2.lifespan;
                result.damage = s1.damage + s2.damage;
                result.damageVariance = s1.damageVariance + s2.damageVariance;
                result.area = s1.area + s2.area;
                result.speed = s1.speed + s2.speed;
                result.cooldown = s1.cooldown + s2.cooldown;
                result.number = s1.number + s2.number;
                result.piercing = s1.piercing + s2.piercing;
                result.projectileInterval = s1.projectileInterval + s2.projectileInterval;
                result.knockback = s1.knockback + s2.knockback;
                return result;
            }
    
            public float GetDamage() {
                return damage + Random.Range(0, damageVariance);
            }
        }
    
        protected Stats currentStats;
        public WeaponData data;
        protected float currentCooldown;
        protected PlayerMovement movement;
    
        public virtual void Initialise(WeaponData data) {
            base.Initialise(data);
            this.data = data;
            currentStats = data.baseStats;
            movement = GetComponent & lt;
            PlayerMovement & gt;
            ();
            currentCooldown = currentStats.cooldown;
        }
    
        protected virtual void Awake() {
            if (data) currentStats = data.baseStats;
        }
    
        protected virtual void Start() {
            if (data) {
                Initialise(data);
            }
        }
    
        protected virtual void Update() {
            currentCooldown -= Time.deltaTime;
            if (currentCooldown & lt; = 0 f) {
                Attack(currentStats.number);
            }
        }
    
        public override bool DoLevelUp() {
            base.DoLevelUp();
            if (!CanLevelUp()) {
                Debug.LogWarning(string.Format("cannot level up 0 to level 1, max level 2 already reached", name, currentLevel, data.maxLevel));
            }
    
            currentStats += data.GetLevelData(++currentLevel);
            return true;
        }
    
        //check the weapon cooldown is up or not
        public virtual bool CanAttack() {
            return currentCooldown & lt; = 0;
        }
    
        protected virtual bool Attack(int attackCount = 1) {
            if (CanAttack()) {
                currentCooldown += currentStats.cooldown;
                return true;
            }
            return false;
        }
    
        //get the amount of damage that the weapon supposed to deal and factoring the weapon stat (include damage varaiance, characeter might) for a buff damage
        public virtual float GetDamage() {
            return currentStats.GetDamage() * owner.CurrentMight;
        }
    
        public virtual Stats GetStats() {
            return currentStats;
        }
    }

    Argument 1: cannot convert from ‘WeaponData’ to ‘ItemData’ Why do i get this error and how to fix?

    #18345
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Based on the reference information, you need to cast the WeaponData to ItemData when calling base.Initialise(). Update the Initialise method to:

    public virtual void Initialise(WeaponData data)
    {
        base.Initialise((ItemData)data);
        this.data = data;
        currentStats = data.baseStats;
        movement = GetComponent();
        currentCooldown = currentStats.cooldown;
    }

    This explicitly casts the WeaponData to ItemData when calling the base class’s Initialise method, which should resolve the type conversion error.

    has upvoted this post.
    #18350
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    Still not working by using the code

    public virtual void Initialise(WeaponData data)
    {
        base.Initialise((ItemData)data); //Cannot convert type 'WeaponData' to 'ItemData'//
        this.data = data;
        currentStats = data.baseStats;
        movement = GetComponent<PlayerMovement>();
        currentCooldown = currentStats.cooldown;
    }
    has upvoted this post.
    #18351
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    The error message indicates that WeaponData cannot be directly converted to ItemData. This suggests that WeaponData is not a subclass of ItemData, or there is no implicit conversion defined between them.

    To fix this issue, you need to ensure that WeaponData inherits from ItemData. Here’s how you can do it:

    1. Check the Inheritance: Make sure that your WeaponData class is defined to inherit from ItemData. It should look something like this:

      public class WeaponData : ItemData
      {
          public Stats baseStats; // Assuming baseStats is defined in WeaponData
          // Other properties
      }
    2. Modify the Initialise Method: After ensuring that WeaponData inherits from ItemData, your Initialise method can stay as is:

      public virtual void Initialise(WeaponData data)
      {
          base.Initialise(data); // This should now work if WeaponData inherits from ItemData
          this.data = data;
          currentStats = data.baseStats;
          movement = GetComponent();
          currentCooldown = currentStats.cooldown;
      }
    3. Check for Compilation Errors: Make sure there are no other compilation errors in your project that may be interfering with the recognition of the inheritance.

    If WeaponData cannot or should not inherit from ItemData, you will need to rethink your class structure and how you manage the data for weapons and items. In that case, consider creating a common interface or base class that both ItemData and WeaponData can inherit from.

    Let me know if you need further assistance!

    has upvoted this post.
    #18352
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    I have fixed the problem

    has upvoted this post.
    #18353
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Great! Could you share the specific solution you used to fix the problem? This will help me understand how you resolved the type conversion issue and potentially help other developers facing similar challenges.

    Would you be willing to provide more details about the fix you implemented?

    has upvoted this post.
    #18367
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    You are awesome Alp. Thank you for fixing this issue.

    #18369
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Thank you for the kind words! However, the reference information does not provide details about the specific solution to the type conversion issue.

    Could you share more details about how you fixed the problem with the WeaponData and ItemData type conversion?

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: