Interacting a SWEP and an entity

Without going into too much detail, what I’m trying to do is have a SWEP an integer that is assigned to the entity and I’m wondering what the best way to do it is.
I’m using util.TraceLine to check if I’m hitting the right ent. Do I need to create a NWInt here or something or can I just change the integer straight away.



function SWEP:SecondaryAttack()
	local attacker = self.Owner
	local tr = util.TraceLine( {
		start = self.Owner:GetShootPos(),
		endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * self.HitDistance,
		filter = self.Owner,
	} )
	
	if (SERVER && IsValid( tr.Entity) && ( tr.Entity:GetClass() == "ent1")) then 
		
		print("hit")
		
	end
end


For the sake of the example, let’s just say that I want to add one to the integer that is assigned to ent1. Let’s call the integer example_1.

I hope I explained it clear enough,
thanks.



if(isHit) then
entity.example_1 = entity.example_1 + 1
end


It depends on what you want to use it for.

I’m working on a bank type system. I need a value to be changed on the entity based on a value on the SWEP.

If you need the variable shared, I would use a DTInt/NetworkVar since traces can occasionally return different values between realms, and your predicted weapon code won’t run clientside single-player.

I tried using network var but it returned an error when I was trying to use


self:GetVar1()

Do I need to set the variable locally as well through


local Var1 = self:GetVar1()

?

You need to declare the NetworkVar in SetupDataTables: https://wiki.garrysmod.com/page/Networking_Entities

I’ve done that already, would it be best to set the local variable to the networked one then only change the network variable from elsewhere (I.e the entity) or only use the networked variable regardless of whether it’s from the self (the entity) or the swep?


local var1 = self:GetVar1()

or


local var1 = 0
self:GetVar1(var1)

Probably not the best way of wording it but I hope you can understand.

Thanks.

You cannot do it the second way – don’t know if you’re a C programmer, but it looks like you are trying to set the variable by treating GetVar1 as a pass-by-reference method, which only exists for tables and userdata (vectors, angles, etc.) in Lua.

Sorry, I forgot a few things.




function ENT:SetupDataTables()
	self:NetworkVar( "Int", 0, "Var1" )
end

function ENT:Initialize()
    self:SetVar1(0)
local var1 = self:GetVar1()
end



or




function ENT:SetupDataTables()
	self:NetworkVar( "Int", 0, "Var1" )
end

local var1 = 0

function ENT:Initialize()
    self:SetVar1(var1)
end


You don’t have to set it to 0 – it’s automatically initialized that way. Also, don’t use GetVar1 until you need the value – there’s no reason to do that in Initialize.

Thanks for your help, I’ve got it working although when I spawn multiple entities, the value is changed on all of them rather than just the one that is hit with the trace. Is there any way to solve this?

Can you post the code that’s changing it? That shouldn’t be happening with NetworkVars.

My mistake, I did some bad code which was setting all of the entities.

Is there any way that I could get the value of the entity to show up on a 2d3d text and the value of the swep on the HUD?