• Why DarkRP dropped weapons are not treated as weapons?
    11 replies, posted
On TTT when you get an Entity object that represents a dropped weapon or ammo you can access informations about it, like ent.Primary.Damage (suppose ent is an Entity object that in dropped some where on the ground), but on DarkRP looks like dropped weapons are treated as "World props" and because of that I'm not being able to get any information from its object (suppose you drawn a trace and got the entity the player is looking at through it). I've tried ent:IsWeapon() and it's not, how is that possible? How can I convert it to a Weapon Entity?
I'm assuming that you want to display information about the weapon the player is looking at to the player. Perhaps you can still get the weapon's class by ent:GetClass() then use [url=http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index01d1.html]weapon.Get( ent:GetClass() )[/url] then you can index the table and take the primary damage, or print name or whatever you need. Obviously you need to do a little check to make sure the table exists beforehand, as demonstrated in the wiki.
It's a work around to allow multiple copies of a weapon be represented by a single model, also it prevents people just walking over the weapon to pick it up as they're forced to +use the weapon. If you're wanting to access the weapon's table, I'd do as Internet1001 said and use weapon.Get(), the actual weapon class is stored in ENT.weaponclass of the spawned_weapon, shown [url=https://github.com/FPtje/DarkRP/blob/master/entities/entities/spawned_weapon/init.lua#L40]here[/url].
[QUOTE=TVC;43709350]It's a work around to allow multiple copies of a weapon be represented by a single model, also it prevents people just walking over the weapon to pick it up as they're forced to +use the weapon. If you're wanting to access the weapon's table, I'd do as Internet1001 said and use weapon.Get(), the actual weapon class is stored in ENT.weaponclass of the spawned_weapon, shown [url=https://github.com/FPtje/DarkRP/blob/master/entities/entities/spawned_weapon/init.lua#L40]here[/url].[/QUOTE] I tried to do it and there is a problem, in the DarkRP code linked above the value returned by self.weaponclass is the actual SWEP name (I figured it out by placing a print function invocation which argument was the self.weaponclass), but on my code it's returning spawned_weapon when I invoke the method GetClass on the Entity's object (the weaponclass field is not available on it) and because of such I can't get the dropped weapon's class name. I'm getting the Entity object of the weapon I'm looking at with: [CODE] local ent = util.TraceLine( { start = ply:GetShootPos(), endpos = ply:GetShootPos() + ( ply:GetAimVector() * 160 ), filter = ply, mask = MASK_SHOT_HULL } ).Entity [/CODE] Does someone knows what can I do?
weapon.Get(ent.weaponclass) You shouldn't use ent:GetClass() since that just gives you the workaround entity's class name. I'd make sure ent is valid and ent.weaponclass is not nil before doing weapon.Get to avoid script errors.
[QUOTE=KingofBeast;43721825]weapon.Get(ent.weaponclass) You shouldn't use ent:GetClass() since that just gives you the workaround entity's class name. I'd make sure ent is valid and ent.weaponclass is not nil before doing weapon.Get to avoid script errors.[/QUOTE] The problem is that ent.weaponclass is nil for me, I don't know why, I checked the returned string from ent:GetClass() and is says "spawned_weapon", so it's not a random world prop it's a DarkRP weapon-prop. Do you have any idea about why it's happening?
Make sure you are accessing ent.weaponclass serverside.
Might look retarded but make sure you don't do [lua] local ent = util.TraceLine( { start = ply:GetShootPos(), endpos = ply:GetShootPos() + ( ply:GetAimVector() * 160 ), filter = ply, mask = MASK_SHOT_HULL } ).weaponclass [/lua] but this: [lua] local ent = util.TraceLine( { start = ply:GetShootPos(), endpos = ply:GetShootPos() + ( ply:GetAimVector() * 160 ), filter = ply, mask = MASK_SHOT_HULL } ).Entity.weaponclass [/lua] common mistake when naming the trace ent
Read the code the OP posted above. He already is accessing .Entity.
[QUOTE=Bo98;43727838]Make sure you are accessing ent.weaponclass serverside.[/QUOTE] How can I do that from client-side I was considering creating a console command, but they do not accept tables or userdata as arguments. What should I do then? I really need to access this field. And use its value on client-side. And I also need to get it from this ent object, that is generated on client-side with: [CODE]local ent = util.TraceLine( { start = ply:GetShootPos(), endpos = ply:GetShootPos() + ( ply:GetAimVector() * 160 ), filter = ply, mask = MASK_SHOT_HULL } ).Entity[/CODE]
Here we go: [url]https://github.com/FPtje/DarkRP/commit/0b3e0c3ef89930cbbd508eaf8b44193259faf2fb[/url] You can now call :GetWeaponClass() both serverside and clientside to get the weaponclass.
[QUOTE=Bo98;43783056]Here we go: [url]https://github.com/FPtje/DarkRP/commit/0b3e0c3ef89930cbbd508eaf8b44193259faf2fb[/url] You can now call :GetWeaponClass() both serverside and clientside to get the weaponclass.[/QUOTE] Thanks Bo98 and Falco, that's really helpful!
Sorry, you need to Log In to post a reply to this thread.