• Distance and vector
    16 replies, posted
Hi, I need to get the vector of the vendor(entity) which is the closet to me. In this function I get random vector... My code... local function FindVendor(pl) local VendTable = {} for k ,v in pairs(ents.GetAll()) do if v:GetClass() == "soda_vendor" then table.insert( VendTable, { dist = v:GetPos():Distance(pl:GetPos()) / 25, ent = v } ) table.SortByMember( VendTable, "dist" ) local result = table.GetLastValue( VendTable ) pl:ChatPrint(result.ent:GetPos()) end end end
You can save the closest entity in the variable so if it finds an even closer one it will replace the old one. I recommend to use Vector/DistToSqr here
The problem is that I can't find the way to get the position of the vendor which is closest to me.
v:GetPos() ?
Yes it is, but only for entity which is closest to me.
And what? You compare one vector with another
I have a feeling you're doing something wrong if you're trying to find the vendor closest to you. Why do you need to find the closest vendor? If I'm wrong you can do this by getting a table of the 'soda_vendors' using ents.FindByClass and you could either use table.sort to check if 'apos:Distance(playerpos) < bpos:Distance(playerpos)' or use a for loop to compare the distances of each entity. local closest, found = math.huge for i, ent in ipairs(vendors) do local dist = playerpos:Distance(entpos) if (dist < closest) then closest = dist found = ent end end return found or NULL
You need to go through all the vendors, and check the distance of every single one of them from you.
I do this for GPS system, I type /findvendor and the vector of the vendor that is closest to me sends to the client. P.S still can't find the way to get it...
What do you can't find? SneakySquid is literally gave you a whole code
I get the vectors of every entity, not the closest one. My code. local function FindVendor(pl) local closest, found = math.huge for i, ent in ipairs(ents.FindByClass("soda_vendor")) do local dist = pl:GetPos():Distance(ent:GetPos())   if (dist < closest) then     closest = dist     found = ent    print(found:GetPos())   end   end   return found or NULL end rp.AddCommand("/fvendor", FindVendor)
You need to find the vectors of every vendor to find out which one is closest to you? I don't get what you mean.
well, there are several vendors on the map, and when I use this function, I should get the vector (GetPos ()) of the vendor that is closest to me.
So did you solve your problem already or not? Because you look like you still don't understand what is going on, and neither am I.
No, I didn't...
Ah, I get what he wants. Replace found or NULL to found:GetPos() or Vector()
it finally works! Big thanks to you(Spar) and SneakySquid for this friendly help, also thanks everyone who tried to help ;) P.S Sorry for my English.
Sorry, you need to Log In to post a reply to this thread.