How to use the protected override void function(T component) function

I am using the following two functions after following the Unity Roguelike tutorial and I am trying to modify it so that the player can attack the enemies. The problem comes in because im unfamiliar with the function(T component) function and im unsure of how to call it.

The AttemptMove() function checks if the player collides with anything and if its a wall, then the second function, OnCantMove() is called. How can I check what the ‘hitComponent’ is that is being passed into the OnCantMove() function so that I can create if statements for different objects.

Thanks guys.

   protected virtual void AttemptMove<T> (int xDir, int yDir)
    		where T: Component
    	{
    		RaycastHit2D hit; 
    		bool canMove= Move (xDir, yDir, out hit);
    		if (hit.transform == null)
    			return;
    
    		T hitComponent = hit.transform.GetComponent<T> ();
    
    		if (!canMove && hitComponent != null)
    			OnCantMove (hitComponent); 
    	}
    
and `
           protected override void OnCantMove<T>(T component)
	   {

		Wall hitWall = component as Wall;
		hitWall.DamageWall (wallDamage);
		animator.SetTrigger ("PlayerChop");

	  }`

Like the keyword as, there is also is which can be used in if statements like below.

protected override void OnCantMove<T>(T component) : where T : Component
{
         if (component is Wall)
        {
             Wall hitWall = component as Wall;
             hitWall.DamageWall (wallDamage);
             animator.SetTrigger ("PlayerChop");
        }
        else if (component is Barrel)
       {
            // do barrel related stuff
       }
}

Or you can check if the hitWall == null as the component as Wall statement will fail silently if not the right type.

Roughly:

<T>(T component) is a way of allowing any specified type in that function.

: where T : Component is a way of restricting that type of only types inheriting from Component.

You are calling it correct. Just think of the parameter as a dummy for any component passed through.
Then in that function you can check that dummy to see if it is of type ‘Wall’.
Although, that function needs to be decalred virtual in a super class if you’re overriding it. (or remove that keyword if not needed)

If you’re unsure of virtual functions / overriding then it will be wise to look into it more. Especially understanding Objet Oriented design will help greatly, especially in this object / component heavy engine.