Shipment detector

Hi
Beware first LUA attempt.
I’ve been trying to modify the money detector for darkrp by philxyz so it outputs shipment content and count.

It kinda works but it seems that if too many entities gets in the detector range it outputs 0 (more than 2 entities) I have tried adding a model check but that didn’t work at all. Hope someone can help.

my code:
[lua]local en = ents.FindInSphere(self:GetPos(), self.range)

for k, v in pairs(en) do
	if v:GetNWInt("count") then
		Wire_TriggerOutput(self.Entity, "Count", v:GetNWInt("count"))
		Wire_TriggerOutput(self.Entity, "Contents", v:GetNWString("contents"))
	end
end

[/lua]
original code:
[lua]for k, v in pairs(en) do
if v.GetTable and v:GetTable().MoneyBag and v:GetTable().Amount then
total = total + v:GetTable().Amount
end
end
[/lua]
best case scenario would be if it output the values of the nearest shipment.

My Attempt to take the closets one:
[lua]
local distance = self.range

for k, v in pairs(en) do
	if v:GetNWInt("count") and v:GetName()=="Shipment"  then
		if self:GetPos():Distance( v:GetPos() )<distance then
		count = v:GetNWInt("count")
		contents = v:GetNWString("contents")
		end
		distance=self:GetPos():Distance( v:GetPos() )
	end
end

[/lua]

doesn’t work either ofc :slight_smile:

Are you making an entity out of a script?
Before trying to make anything, you should have a clear set of goals about what it should do. Your entity looks for entities near it and then does what? And how does it know what differentiates the entities it’s looking for from the others?

it should find the nearest entity with the name shipment. then it should output the count (number of weapons in the shipment) and the contents of the shipment. I’ll post the whole thing so it makes more sence.
init.lua
[lua]-- RP Shipment Detector by philxyz

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

ENT.WireDebugName = “Shipment Detector”
ENT.OverlayDelay = 0

/---------------------------------------------------------
Name: Initialize
Desc: First function called. Use to set up your entity
---------------------------------------------------------
/
function ENT:Initialize()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

local phys = self.Entity:GetPhysicsObject()
if phys:IsValid() then phys:Wake() end

-- Default range
self.range = 50

self.Inputs = Wire_CreateInputs(self.Entity, {"Range"})
self.Outputs = WireLib.CreateSpecialOutputs(self.Entity,{"Count", "Contents"},{"NORMAL", "STRING"})

end

/---------------------------------------------------------
Name: TriggerInput
Desc: the inputs
---------------------------------------------------------
/
function ENT:TriggerInput(iname, value)
if iname == “Range” and value > 0 and value <= 500 then
self.range = value
end
end

/---------------------------------------------------------
Name: Think
Desc: Thinks :stuck_out_tongue:
---------------------------------------------------------
/
function ENT:Think()
self.BaseClass.Think(self)

local en = ents.FindInSphere(self:GetPos(), self.range)
local count = 0
local contents = ""
local distance = self.range

for k, v in pairs(en) do
	if v:GetNWInt("count") and v:GetName()=="Shipment"  then
		if self:GetPos():Distance( v:GetPos() )&lt;distance then
		count = v:GetNWInt("count")
		contents = v:GetNWString("contents")
		end
		distance=self:GetPos():Distance( v:GetPos() )
	end
end
	
Wire_TriggerOutput(self.Entity, "Count", count)
Wire_TriggerOutput(self.Entity, "Contents", contents)





self.Entity:NextThink(CurTime() + 2)
return true

end
[/lua]

Try this?
[lua]-- RP Shipment Detector by philxyz

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

ENT.WireDebugName = “Shipment Detector”
ENT.OverlayDelay = 0

/---------------------------------------------------------
Name: Initialize
Desc: First function called. Use to set up your entity
---------------------------------------------------------
/
function ENT:Initialize()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )

local phys = self.Entity:GetPhysicsObject()
if phys:IsValid() then phys:Wake() end

-- Default range
self.range = 50

self.Inputs = Wire_CreateInputs(self.Entity, {"Range"})
self.Outputs = WireLib.CreateSpecialOutputs(self.Entity,{"Count", "Contents"},{"NORMAL", "STRING"})

end

/---------------------------------------------------------
Name: TriggerInput
Desc: the inputs
---------------------------------------------------------
/
function ENT:TriggerInput(iname, value)
if iname == “Range” then
self.range = math.Clamp(value,0,500)
end
end

/---------------------------------------------------------
Name: Think
Desc: Thinks :stuck_out_tongue:
---------------------------------------------------------
/
function ENT:Think()
self.BaseClass.Think(self)
local distance = self.range
local count,contents = -1,""
local pos = self:GetPos()
for _,ent in ipairs(ents.GetAll()) do
if ent:GetNWInt(“count”) and ent:GetName() == “Shipment” then
local dist = ent:GetPos():Distance( pos )
if dist <= distance then
count = ent:GetNWInt(“count”)
contents = ent:GetNWString(“contents”)
distance = dist
end
end
end

Wire_TriggerOutput(self.Entity, "Count", count)
Wire_TriggerOutput(self.Entity, "Contents", contents)

self.Entity:NextThink(CurTime() + 2)
return true

end[/lua]

Lexic’s way would work. But in this case where you only want the contents of one shipment couldn’t you simply use a trace down from the detector? ( I think that’s what you want to use it for anyway)
[lua]function ENT:Think()
self.BaseClass.Think(self)
local count,contents = -1,""
local pos = self:GetPos()

local trace = {}

trace.start = pos 
trace.endpos = pos - Vector(0,0,128)
trace.filter = self

tr = util.TraceLine( trace )

if tr.HitEnt then
	local ent = tr.HitEnt
	if ent:GetNWInt("count") and ent:GetName() == "Shipment"  then
		count = ent:GetNWInt("count")
		contents = ent:GetNWString("contents")
	end
end
	
Wire_TriggerOutput(self.Entity, "Count", count)
Wire_TriggerOutput(self.Entity, "Contents", contents)

self.Entity:NextThink(CurTime() + 2)
return true

end[/lua]

offtopic: what kind of wrapping you use for it to look like that ([lua] ?)

Tried both solutions but none of em works. Both stays at -1. I tried making it look for the model only but that wouldn’t work either.

The name is “Shipment” right?
darkrp spawnshipment shared.lua
[lua]ENT.Type = “anim”
ENT.Base = “base_gmodentity”
ENT.PrintName = “Shipment”
ENT.Author = “philxyz”
ENT.Spawnable = false
ENT.AdminSpawnable = false[/lua]

Download the whole thing here so you can test it without having to send it too me. (Not that I mind)
http://peecee.dk/upload/download/198652

What’s the folder name that had that file in it?

C:\Program Files (x86)\Steam\steamapps\briam\garrysmod\garrysmod\gamemodes\darkrp\entities\entities\spawned_shipment
from that i tried
[lua]if ent:GetNWInt(“count”) and ent:GetName() == “spawned_shipment” then[/lua]
I also tried
[lua]if ent:GetNWInt(“count”) and ent:GetModel() == “models/Items/item_item_crate.mdl” then[/lua]

still with -1 as result.

Try
[lua]if ent:GetClass() == “spawned_shipment” then[/lua]

Yay! it works… thanks for the help really appreciate it

Doesn’t really matter but tried it with Crazy Quebec’s script aswell but that didn’t work.

Ah, you guys figured it out. I just got back from work, but “yes, that’s the correct answer” :smiley:

Don’t forget to update the credits Briam with your name :stuck_out_tongue:

Well I wrote by name and (98% philxyz money detector) as it was a 10 line modification which I couldn’t even manage myself :slight_smile:

http://www.garrysmod.org/img/?t=dll&id=81645