Help with halo drawing swep

Hey everyone, ive been working on a weapon that shows entities starting with k_ through walls for 10 seconds in a range of x units. I have a few things already, any help would be greatly appreciated!


 
hook.Add( "PreDrawHalos", "AddHalos", function()
	halo.Add( ents.FindByClass( "item_*" ), Color( 0, 255, 0 ), 0, 0, 2, true, true )
end )


I tried adding it to the SWEP:PrimaryAttack() function but no dice.



SWEP.PrintName = "Entity Scanner"
SWEP.Author = "sleepy"
SWEP.Contact = ""
SWEP.Purpose = "Scan bases for contraband!"
SWEP.Instructions = "Left click to scan!"

SWEP.Category = "sleepys addons"

SWEP.Spawnable = true

SWEP.ViewModel = ""
SWEP.WorldModel = ""

SWEP.Primary.Clipsize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Slot               = 1
SWEP.SlotPos 			= 5
SWEP.DrawAmmo           = false
SWEP.DrawCrosshair      = true

function SWEP:Initialize()
	self:SetWeaponHoldType( "normal" )
end

function SWEP:PrimaryAttack()
	halo.Add( ents.FindByClass( "k_*" ), Color( 0, 255, 0 ), 0, 0, 2, true, true )
end


i cant figure out how to have it draw only in a certain range, and i want the swep to take 5 seconds for the entities to begin being drawn, and then have the entities stay drawn for 10 seconds, visible through walls and all surfaces. And if it cannot find entities in the range, then it does nothing.

So in the PreDrawHalos, you need to get all entities in the class in a certain distance and put them in a table to use in the hook.

im new to lua, so pardon me for asking, but how would i do that. And i dont see any PreDrawHalos function

ents.FindInBox looks like something that might do the trick but i dont understand vectors, or how i would be able to make it the players current position

Alright, so if you took geometry, you would’ve worked with ordered triples and 3d graphs. If you haven’t, it’s pretty much just a 2d graph, but with a 3rd axis sticking out creating depth. Vectors are pretty much coordinates for that 3d graph.

PreDrawHalos is the hook name that you used at the very top of your post. It’s the hook where you would add halos using halo.Add.

Create an empty local table at the top of the hook.

Create a for statement inside the hook and make it iterate through the table returned by


ents.FIndByClass()

then check for distance from player to object by doing this


LocalPlayer():GetPos():DistToSqr(entity:GetPos())

and compare it using


<=

and your distance SQUARED (^2).

Then in the check, insert the entity into the local table. Then use that table in the halo.Add.

I’m sure there is a more efficient way and if someone knows one, please do say.

Thank you so much for your reply!

I think i have a basic understanding of what you are saying. Ive only begun learning lua in the past few days via you tube series’s, but u think i get it.




function SWEP:PrimaryAttack()
	hook.Add( "PreDrawHalos", "AddHalos", function()
		local foundEntities={} 
		if LocalPlayer():GetPos():DistToSqr(entity:GetPos()) <= 30 then 
			halo.Add( ents.FindByClass( foundEntites ), Color( 0, 255, 0 ), 0, 0, 2, true, true )
		end
	end
end



This is what i did, but it doenst work. Once again i dont have a big clue about what im doing.

Many things need fixing here.

1.) You need to end the hook and make sure you add the end parentheses after it.

2.) That for statement should be this


for k, v in pairs(ents.FindByClass("class")) do

This makes the for statement iterate through the table you want.

Then, you need the distance check INSIDE the for statement.

So, it looks like this.


for k, v in pairs(ents.FindByClass("class")) do
    if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= Insert_Distance_Here then
         table.insert(foundEntities, v)
    end
end

3.) You must LOCALIZE the foundEntities table. Like so


local foundEntities = {}

This is so other addons cannot use this variable.

4.) Your foundEntities table contains a table of entities so all you need to do in halo.Add is


halo.Add( foundEntities, Color( 0, 255, 0 ), 5, 5, 2, true, true )

5.) Get rid of the if statement on the line before halo.Add

30 is very, very small. As reference, if you play DarkRP, if you are looking at a door so you can lock it, the maximum distance you can be everything lower than 4225.


Side Note***: Start learning lua here

Then go on and learn Garry’s Mod specific things.

Thanks again for the reply,

I have done what you suggested, and this is the result



function SWEP:PrimaryAttack()
	hook.Add( "PreDrawHalos", "AddHalos", function() )
		for k, v in pairs(ents.FindByClass("class")) do
		local foundEntities={}
		for k, v in pairs(ents.FindByClass("class")) do
    		if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 6000 then
         		table.insert(foundEntities, v)
    		end
		end
		halo.Add( foundEntities, Color( 0, 255, 0 ), 5, 5, 2, true, true )
	end
end



However, i get a console error:



[ERROR] addons/sleepysaddons/lua/weapons/contraband_scanner/shared.lua:30: unexpected symbol near ')'
  1. unknown - addons/sleepysaddons/lua/weapons/contraband_scanner/shared.lua:0



is 6000 an appropriate distance?

By the way, i really appreciate the help. You are extremely helpful. Thanks :slight_smile:

Read Number 1 closer. It says add an end parentheses after the end of the hook. Also, please your scoping.

ok, i was able to complete the script with no errors, yet it still doesnt draw anything, the halos dont show up after left clicking with the swep.




function SWEP:PrimaryAttack()
    hook.Add("PreDrawHalos", "AddHalos", function()
        local foundEntities = {}

        for k, v in pairs(ents.FindByClass("item_*")) do
            if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 6000 then
                table.insert(foundEntities, v)
            end
        end

        halo.Add(foundEntities, Color(0, 255, 0), 5, 5, 2, true, true)
    end)
end



Also, how do i make the halo go away after 10 seconds?

The error is not in the code you provided. Mind pasting more of your file?

Also, create a timer for however much time you want them to see the halo. Then use this to see if the timer exists and if it does, then recreate the timer using the timer.Create() for another 10 seconds. Then at the end of the if statement write


return end

which will prevent the execution of the code below in the function (Which will be the PreDrawHalo hook)

Then in the callback argument of the both timer.Creates, remove the hook using this

This is the code of the whole swep:




SWEP.PrintName = "Contraband Scanner"
SWEP.Author = "sleepy"
SWEP.Contact = ""
SWEP.Purpose = "Scan bases for contraband!"
SWEP.Instructions = "Left click to scan!"

SWEP.Category = "sleepys addons"

SWEP.Spawnable = true

SWEP.ViewModel = "models/weapons/c_arms.mdl"
SWEP.WorldModel = "models/weapons/c_arms.mdl"

SWEP.Primary.Clipsize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Slot               = 1
SWEP.SlotPos 			= 5
SWEP.DrawAmmo           = false
SWEP.DrawCrosshair      = true

function SWEP:Initialize()
	self:SetWeaponHoldType( "normal" )
end

function SWEP:PrimaryAttack()
    hook.Add("PreDrawHalos", "AddHalos", function()
        local foundEntities = {}

        for k, v in pairs(ents.FindByClass( "item_*" )) do
            if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 6000 then
                table.insert(foundEntities, v)
            end
        end

        halo.Add( foundEntities, Color( 0, 255, 0 ), 2, 2, 2, true, true )
    end)
end




You still getting the error cuz I can’t find one in your code.

im not getting erros, it just simply wont create the halo around the entities.

I thought it was supposed to be


k_*

not


item_*

since im in single player, im doing it for props. and it wont create the halo.




for k, v in pairs(ents.FindByClass( "props_*" )) do



Sry, can’t really help you because I’m not on my computer which has GMod installed. I reccomend you use the print function and start printing stuff to see if you can find where it went wrong.

ok ill try that.

Would the timer be like this?



function SWEP:PrimaryAttack()
    hook.Add("PreDrawHalos", "AddHalos", function()
        local foundEntities = {}

        for k, v in pairs(ents.FindByClass( "props_*" )) do
            if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 600000000	 then
                table.insert(foundEntities, v)
            end
        end

        halo.Add( foundEntities, Color( 0, 255, 0 ), 2, 2, 2, true, true )
    end)

    timer.Simple(15, function()

    	hook.Remove( "PreDrawHalos", "AddHalos" )
    end )
end


Use timer.Create so you can remove the timer and check if it exists, etc.

like this?




SWEP.PrintName = "Contraband Scanner"
SWEP.Author = "sleepy"
SWEP.Contact = ""
SWEP.Purpose = "Scan bases for contraband!"
SWEP.Instructions = "Left click to scan!"

SWEP.Category = "sleepys addons"

SWEP.Spawnable = true

SWEP.ViewModel = "models/weapons/c_arms.mdl"
SWEP.WorldModel = "models/weapons/c_arms.mdl"

SWEP.Primary.Clipsize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Slot               = 1
SWEP.SlotPos 			= 5
SWEP.DrawAmmo           = false
SWEP.DrawCrosshair      = true

function SWEP:Initialize()
	self:SetWeaponHoldType( "normal" )
end

function SWEP:PrimaryAttack()
    hook.Add("PreDrawHalos", "AddHalos", function()
        local foundEntities = {}

        for k, v in pairs(ents.FindByClass( "item_*" )) do
            if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 6000 then
                table.insert(foundEntities, v)
            end
        end

        halo.Add( foundEntities, Color( 0, 255, 0 ), 2, 2, 2, true, true )
    end)

    timer.Create( "drawEnts", 10, 1, function() 
    end)
    
    if ( timer.Exists( "drawEnts" ) ) then

	else
		hook.Remove( "PreDrawHalos", "AddHalos" )
	end
end



[editline]20th September 2017[/editline]

Ok, so i think i have everything set up correctly. To summarize, my goal is to have a SWEP, that when left clicked on, draws specific entities through walls for 10 seconds. After 10 seconds, the drawing stops, and you have to weait another 10 seconds to look at the entities again.



SWEP.PrintName = "Contraband Scanner"
SWEP.Author = "sleepy"
SWEP.Contact = ""
SWEP.Purpose = "Scan bases for contraband!"
SWEP.Instructions = "Left click to scan!"

SWEP.Category = "sleepys addons"

SWEP.Spawnable = true

SWEP.ViewModel = "models/weapons/c_arms.mdl"
SWEP.WorldModel = "models/props_junk/garbage_takeoutcarton001a.mdl"

SWEP.Primary.Clipsize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

SWEP.Secondary.Clipsize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Slot               = 1
SWEP.SlotPos 			= 5
SWEP.DrawAmmo           = false
SWEP.DrawCrosshair      = true

function SWEP:Initialize()
	self:SetWeaponHoldType( "normal" )
end

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + 10)

    hook.Add("PreDrawHalos", "AddHalos", function()
        local foundEntities = {}

        for k, v in pairs(ents.FindByClass( "props_*" )) do
            if LocalPlayer():GetPos():DistToSqr(v:GetPos()) <= 6000 then
                table.insert(foundEntities, v)
            end
        end

        halo.Add( foundEntities, Color( 0, 255, 0 ), 2, 2, 2, true, true )
    end)

    timer.Create( "drawEnts", 10, 1, function() 
    end)
    
    if ( timer.Exists( "drawEnts" ) ) then
    	print( " " )
	else
		hook.Remove( "PreDrawHalos", "AddHalos" )
	end
end

function SWEP:SecondaryAttack()

end


With the following in shared.lua, when i left click with the swep it fails to create the halo. thoughts anyone???

:snip: See below for updated code.