C# - beginner questions

Hello. I am studying C# from a course. There are two scripts that attach to the enemy. My questions deal with the second script. Forgive all the commented notes, but I’ll keep those incase any other beginner wants to study it.

—CastSpell(0);
1-Why is it CastSpell(0); and not CastSpell(); I don’t understand the 0.

—void CastSpell(int SpellID = 0; int MinimumPoints = 50)
2-Could you also declare the variables SpellID and MinimumPoints in the first code under the Health and Magic points and in the second script just have…
void CastSpell()
in its place? Is it another way of writing it (keeping those private variables) or am I wrong?

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
// attach this to the enemy
// will also be creating a spellcasting script (also attach to enemy so enemy can cast spells)
{

    //enemy health
    public int Health = 100;

    //points for casting magic spells
    public int MagicPoints = 100;

    // Use this for initialization
    void Start ()
    {
   
    }
   
    // Update is called once per frame
    void Update ()
    {
   
    }
}
using UnityEngine;
using System.Collections;

public class SpellCasting : MonoBehaviour
// attach this to the enemy so enemy can cast spells
{
    // Update is called once per frame
    void Update ()
    {
        //if user press S key then cast spell
        if(Input.GetKey(KeyCode.S))
            CastSpell(0);
    }
    // this is the spell cast function
    // SpellID is the spell to cast
    // MinimumPoints is the cost to cast the spell
    void CastSpell(int SpellID = 0; int MinimumPoints = 50)
    {
        // Check to be sure the enemy has enough points (from Enemy script above)
        Enemy EnemyComp = GetComponent<Enemy>();
        // create a local variable called EnemyComp which is of type Enemy
        // it will equal the GetComponent function
        // pass that function to type <Enemy>
        if(EnemyComp.MagicPoints >= MinimumPoints) //MagicPoints from above script
        {
            //Cast spell and spend the points
            EnemyComp.MagicPoints -= MinimumPoints;
            Debug.Log ("Cast a spell.");
    }
}

There is Castspell() with 0 because you need to fill both requiments (spell id and minimium points) when u use void that you created, you shoult write:

  if(Input.GetKey(KeyCode.S))
            CastSpell(0, 50); //<- that 50 is the points value
    }

so basically the 0 is the id of the spell, the void requires you to put any number in there because you made it so

i think you should make list of spells like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpellList
{

    public int SpellID;
    public int MagicPoints;

}

Thats here you store information about spells;

using System.Collections;
using UnityEngine;
using System.Collections.Generic;

public class: SpellCasting {

    public List<SpellList> spelllist;
}

add that to your other script. and now u have list of spells

1 Like

Umm could you clarify. What does this even mean?

From a design perspective, I am not sure, whoever wrote that code made the parameters optional so you could decide to pass no parameters if you wanted. It would then just use the defaults of SpellID of 0 and MinimumPoints of 50.

From a programming perspective, it is simply because the function asks for 2 numbers, both of which are optional so you supply no parameters, 1 parameter or both, its up to you.

Yes, you could add variables for SpellID and MinimumPoints, but you wouldnt call the function without parameters. First you would get a reference to the SpellCasting script and call something along the lines of:

private int spellID = 5;
private int minPoints = 30;

SpellCasting casting;

void Awake()
{
    casting = GetComponent<SpellCasting>();
}

void Cast()
{
    // Bear in mind, the CastSpell function would have to be public for this to work!
    casting.CastSpell(spellID, minPoints);
}
1 Like

Thought I would throw this at you since you like to comment your code. There is a way to add comments to a function that show up in the tooltip when you hover over the function call elsewhere in your code. I’ve modified the CastSpell function comments as an example.

https://msdn.microsoft.com/en-us/library/z04awywx(v=vs.100).aspx

using UnityEngine;
using System.Collections;

public class SpellCasting : MonoBehaviour
// attach this to the enemy so enemy can cast spells
{
    // Update is called once per frame
    void Update ()
    {
        //if user press S key then cast spell
        if(Input.GetKey(KeyCode.S))
            CastSpell(0);
    }
    /// <summary>
    /// This is the spell cast function.
    /// </summary>
    /// <param name="SpellID">The spell to cast</param>
    /// <param name="MinimumPoints">The cost to cast the spell</param>
    void CastSpell(int SpellID = 0; int MinimumPoints = 50)
    {
        // Check to be sure the enemy has enough points (from Enemy script above)
        Enemy EnemyComp = GetComponent<Enemy>();
        // create a local variable called EnemyComp which is of type Enemy
        // it will equal the GetComponent function
        // pass that function to type <Enemy>
        if(EnemyComp.MagicPoints >= MinimumPoints) //MagicPoints from above script
        {
            //Cast spell and spend the points
            EnemyComp.MagicPoints -= MinimumPoints;
            Debug.Log ("Cast a spell.");
    }
}
2 Likes