• Find in sphere problem
    5 replies, posted
Hello i am coding something that should find and halo printers i can effectivly find in a sphere and higihlight the ents in the sphere but i cant i tried using k,v in pairs then doing if v != "money_printer" then return end and then adding halo to the v at the end of the code that did not work i tried finding it by its class (FindByClass) and measuring the distance between the players eye trace and the entity to give it a radius that did not work and i am currently stuck.
Return exits the function, not the loop. Just do a conditional to check if the entity is a print, and inside do what you want, not the opposite.
[QUOTE=James xX;50566982]Return exits the function, not the loop. Just do a conditional to check if the entity is a print, and inside do what you want, not the opposite.[/QUOTE] well i got that out of the way but when i try to halo out the print using halo.add and targeting the entity using v:GetClass() this error happens [QUOTE][ERROR] lua/includes/modules/halo.lua:71: bad argument #1 to 'pairs' (table expe cted, got strin 1. pairs - [C]:-1 2. Render - lua/includes/modules/halo.lua:71 3. fn - lua/includes/modules/halo.lua:153 4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 [/QUOTE] this is the halo part of my code [QUOTE] halo.Add(v:GetClass(), Color( 255, 0, 0 ), 0, 0, 2, true, true )[/QUOTE]
[QUOTE=shootbysteve;50567155]well i got that out of the way but when i try to halo out the print using halo.add and targeting the entity using v:GetClass() this error happens this is the halo part of my code[/QUOTE] halo.Add doesn't take the class name as a first argument. [lua] hook.Add( "PreDrawHalos", "AddHalos", function() for k, ent in pairs( ents.FindInSphere(POS,RADIUS) ) do if ( ent:GetClass() == "my_class_name" ) then halo.Add({ent}, Color( 255, 0, 0 ), 0, 0, 2, true, true ) end end end) [/lua] Not checked this code at all, but this is what you should be aiming for. In the correct hook, search for all entities within your sphere, if they are the right type, add a halo. If I remember correctly though, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/FindInSphere]ents.FindInSphere[/url] is an expensive function, but you could do something like this. [lua] local function GetMyPrintsInRadius( POS, Radius ) local table_of_entities = {} for k, ent in pairs( ents.FindByClass( "my_class_name" ) ) do local _pos = ent:GetPos() if ( ( POS.x - _pos.x )^2 + ( POS.y - _pos.y )^2 + ( POS.z - _pos.z )^2 <= Radius^2 ) then -- Referenced below table.insert( table_of_entities, ent ) end end end hook.Add( "PreDrawHalos", "AddHalos", function() halo.Add( GetMyPrintsInRadius( POS, Radius ) , Color( 255, 0, 0 ), 0, 0, 2, true, true ) end) [/lua] This way instead of checking all the entities to see if they are in your sphere, you only check those you want to know about. the line with the comment on is a simple check for a position within a sphere.
Thanks alot for helping guys ! &#304; tired making and insterng it to a table but i realized the table was conflicting with sck base my problems are solved thanks agsin !
Sorry, you need to Log In to post a reply to this thread.