1. I am new to C# and I am having an issue, I am wanting to use a function like one of these:
    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
    {
        Puts("OnEntityTakeDamage works!");
    }
    But I want to be able to reference player data, an example would be to check if player can build with player.CanBuild();

    -

    If I do something like this it says that player is not in this context or does not exist.
    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
    {
        if(player.CanBuild()){
           Puts("Player can build");
    }
    }


    So I then try this, but it doesn't let me call the bool because I need an argument for the BasePlayer player... but I cannot access that in the void. I am confused!
    Code:
    bool BuildingPrivilege(BasePlayer player){
        if (player.CanBuild()) return false;
                return true;
    }
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
    {
        if(BuildingPrivilege()){
           Puts("Player can build");
    }
    }
     
  2. The thing to understand here is that this hook has a source and destination for the damage and in both cases these can or can not be a player. Look at the GetComponent<BasePlayer>() function and check for null. The hitInfo structures has a field for the attacker on which you can do the same thing.
     
  3. Ah, okay that makes sense. How could I print/export all the data of say hitinfo into the console or something so I can look at all the data?
     
  4. The easiest way is to use a .net decompiler to see all the contents of the dll.
     
  5. I use Visual Studio and import all the references in a dummy project which I use to compile the plugins I make. Since the references are there I can successfully compile (but not run) the plugin and use intellisense. This is a tremendous help in finding what's where and what. Using a decompiler is also a good thing to do when you have to dig deeper or do some digging in the game structures to find stuff.
     
  6. The C# class for a player is BasePlayer. This is a sub-class of BaseCombatEntity. So in the specific case you gave, you can do either of the following:

    #1 The Standard C# Way:

    Code:
    void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
    {
        BasePlayer victim = entity as BasePlayer;
        if (victim != null)
        {
            // Do your stuff here.
        }
    }
    
    #2 The Facepunch way: (In this specific case, the Dev's add a virtual method to do the cast)

    Code:
    void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
    {
        BasePlayer victim = entity.ToPlayer();
        if (victim != null)
        {
            // Do your stuff here.
        }
    }
    
    (I prefer the first way - anyone C# dev knows what's happening. ToPlayer() looks more like conversion that cast, but it really is just a cast)

    Note that in both cases, it might not be a player who got killed, so you must check for nulls if you don't want NREs
     
  7. Wow that helps out a ton thanks! I was still struggling on this, when I get on my computer I'll try that out. It makes sense
     
  8. Just wanted to point out that the "as Player" way is much slower for some reason... I fixed TimeWarning messages in some of my plugins by changing to the 2nd method. It makes absolutely no sense to me, but I tell it how I see it... lol
     
  9. I doesn't surprise me that a vtable call is faster than a cast, but I would have thought the difference was a matter of microseconds - certainly not enough to worry about in OnEntityDeath. I'll try measuring the difference tonight.