Questions about the code here, want to understand it more

I’m also assuming no namespace or controls at the top since it’s a smaller project?

So for this code in AttackDamage :

private void PlayerSelfDamage(Collider2D other) {
if (other.gameObject.CompareTag(playerString)) {
other.GetComponent().TakeDamage(damageAmount);
other.GetComponent().getKnockedBack(transform, knockBackThrust);
}
}

This means :s

private void PlayerSelfDamage(Collider2D other) {

  • if player collides with someone that is not player (other)

if (other.gameObject.CompareTag(playerString)) {

This compares the other to Player (identifies it as player? or is that done by the above string?)

        other.GetComponent<PlayerHealth>().TakeDamage(damageAmount);

TakeDamage is called, with damageAmount as parameter

        other.GetComponent<KnockBack>().getKnockedBack(transform, knockBackThrust);
    }

getKnockedBack is called with transform and knockBackThrust as parameters.

How is getKnockedBack called when it’s in another script and not in AttackDamage.CS?

I see that knockBackThrust is defined, but without a value?

Because the method is defined with two parameters, does it always take two parameters? Do they have to be of the type defined?

You want us to answer your assumption? Only you can answer that.

You’re questioning your own sight? Sounds like another one of those, that’s for you to answer bud.

Mostly yes but sometimes no.

That depends on what it is. Typically yes.

1 Like

I’m asking clear yes or no questions here. As much as I appreciate the help, I don’t much appreciate smarmy replies.

Then the answer is yes, as in yes you did.

What’s that supposed to mean?

Nevermind, since the instructors have recommended I do the course in order I’ll do that and come back with more knowledge.

If I am posting too much you can tell me. I don’t believe I am unreasonable;.

Well, that depends on a lot of factors, but yes, generally people don’t use namespaces in smaller projects. I always use them tho.

This block of code, according to what I’m reading and assuming the playerString variable is the player’s tag.

private void PlayerSelfDamage(Collider2D other)
{
    if (other.gameObject.CompareTag(playerString)) 
    {
        other.GetComponent().TakeDamage(damageAmount);
        other.GetComponent().getKnockedBack(transform, knockBackThrust);
    }
}

The variable other refers to the collider the player collided with, and it’s checking if the tag of that said collider is the same as the player’s tag, if it is, the player will deal damage to itself. So, yes, it is checking if the collider is the player, or at least, tagged as player.

Because you are using this line:

other.GetComponent

It gets the component of the collider, in this case, the KnockBack script, so it can call it from the AttackDamage script.

Yes, parameters have to be of the defined type, unless it’s a generic type, but that’s a completely different topic, but you probably have seen generics before, like List<T>.

Yes, it always takes two parameters unless one of them has a default value or the function is overwritten by a method with fewer parameters. You also have seen this in functions like Raycast where there are multiple versions of it with different parameters, but they all do the same, you can also write those types of functions, but I do not recommend beginners digging into that because the rabbit hole goes quite deep. Go there if you know when to stop to avoid getting confused.

Hope this helps.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms