I am new to C# and I am having an issue, I am wanting to use a function like one of these:
But I want to be able to reference player data, an example would be to check if player can build with player.CanBuild();Code:void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info) { Puts("OnEntityTakeDamage works!"); }
-
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"); } }
Referencing player data inside of function?
Discussion in 'Rust Development' started by Vypr, Sep 30, 2015.
-
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.
-
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?
-
The easiest way is to use a .net decompiler to see all the contents of the dll.
-
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.
-
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:
#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 as BasePlayer; 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)Code:void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { BasePlayer victim = entity.ToPlayer(); if (victim != null) { // Do your stuff here. } }
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 -
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
-
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
-
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.
