Be the first to post for 'Using Structs For Parameter Sets'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

Hi,
I have a problem with this lecture - basically the architecture does not add the component to my player and I cannot find what has happened. Below is a screenshot of the Unity console log and the part of the code where the error points to.

I keep the code the same as you guys, the only difference is in the way I show Health/Energy (I decided to go for text display for now, so this is the only real difference in code). I’ve spent a whole day trying to make things work and the things I managed to determine are the following:

  • the player does not have the special ability added on runtime
  • this causes the array to not have any entries, hence the error in the log. After special-attacking the enemy the same error keeps popping up and no energy is deducted
  • when I manually add the special ability to the player’s array via the inspector all works fine and energy is deducted as expected.
  • the problem started in the previous lecture, but I kept going until the end of this one hoping that the modifications might help.

Could anybody point me in the right direction or review my codes for possible flaws? Below is the code of my classes if anybody would be a dear and lend me a hand.

Regards,
Paweł

Special Ability

{
    public struct AbilityUseParams
    {
        public IDamagable target;
        public float baseDamage;

        public AbilityUseParams (IDamagable target, float baseDamage)
        {
            this.target = target;
            this.baseDamage = baseDamage;
        }
    }

    public abstract class SpecialAbility : ScriptableObject
    {

        [Header("Special Ability General")]
        [SerializeField] float energyCost = 10f;

        protected ISpecialAbility behaviour;

        abstract public void AttachComponentTo(GameObject gameObjectToattachTo);

        public void Use(AbilityUseParams useParams)
        {
            behaviour.Use(useParams);
        }

        public float GetEnergyCost()
        {
            return energyCost;
        }
    }

    public interface ISpecialAbility
    {
        void Use(AbilityUseParams useParams);
    }
}

PowerAttackConfig
{
[CreateAssetMenu(menuName = (“RPG/Special Ability/Power Attack”))]
public class PowerAttackConfig : SpecialAbility
{
[Header(“Power Attack Specific”)]
[SerializeField] float extraDamage = 10f;

        public override void AttachComponentTo(GameObject gameObjectToattachTo)
        {
            var behviourComponent = gameObjectToattachTo.AddComponent<PowerAttackBehaviour>();
            behviourComponent.SetConfig(this);
            behaviour = behviourComponent;
        }

        public float GetExtraDamage()
        {
            return extraDamage;
        }
    }
}

PowerAttackBehaviour
{
public class PowerAttackBehaviour : MonoBehaviour, ISpecialAbility
{
PowerAttackConfig config;

        public void SetConfig(PowerAttackConfig configToSet)
        {
            this.config = configToSet;
        }
            
        // Use this for initialization
        void Start()
        {
            print("Attached myself to " + gameObject.name);
        }

        // Update is called once per frame
        void Update()
        {

        }

        public void Use(AbilityUseParams useParams)
        {
            print("Power attack Used by " + gameObject.name);
            float damageToDeal = useParams.baseDamage + config.GetExtraDamage();
            useParams.target.TakeDamage(damageToDeal);
        }
    }
}
1 Like

Hi Paul, sorry you’re struggling. We’re about to do a major refactor, so I’m hopeful that after that you’ll be more in control of your code. Bear with us.

I have a weird issue. I got to the end of this lecture and had the following error…

Assets/_Characters/SpecialAbilities/SpecialAbilityConfig.cs(18,23): error CS1501: No overload for method Use' takes0’ arguments

If I comment out the behaviour.Use(); at Line 18 the game works fine, I did the same test Ben did. Changed the energy cost to 25 and energy total to 100, the bar emptied in four hits like it was supposed to. I watched the first 10 mins of the next video to see if this error was addressed but it wasn’t. For this lesson my scripts match those on the GitHub exactly.

I do get the following warning…but again if I increase the energy cost of power attack it all seems to work.

Assets/_Characters/SpecialAbilities/SpecialAbilityConfig.cs(10,32): warning CS0414: The private field `RPG.Characters.SpecialAbilityConfig.energyCost’ is assigned but its value is never used

I am not sure if I should move onto making the AOE skills or not yet without resolving this.

I am getting the same error as paul that the array index is out of range at any time I use the array either position 0 or abilityindex

If you are getting the array index out of range error: When you changed Player.cs to use an array instead, you need to go to Player and assign array size and abilities to use, as it doesn’t carry over automatically from prior implementation.

Privacy & Terms