[highlight]Problem Solved[/highlight]
How would I go on about, allowing a player to run a console command, only if he's close to a specific NPC?
I'm guessing [b][url=http://wiki.garrysmod.com/?title=Ents.FindByName]Ents.FindByName [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and distance <= 20 then ... But how, exactly?
snip
[lua]
local npc = Ents.FindByName("Your NPC name")
if npc[1]:GetPos():Distance(ply:GetPos()) < yourDistance then
-- run your command
end
[/lua]
This is with the assumption that only one entity in your map has that name. You could also do ents.FindInSphere and then just checks the ents name
I may have messed something up in that code, if I did someone correct me.
ents.findinsphere would probably be more efficient.
[QUOTE=sintwins;27894314]ents.findinsphere would probably be more efficient.[/QUOTE]
It's more efficient to sort out the entities with the wrong names before doing expensive distance checks.
[QUOTE=S W;27886921]I may have messed something up in that code, if I did someone correct me.[/QUOTE]
[lua]
local distance = 20
local npc = ents.FindByName("Your NPC name")
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
-- run your command
end
[/lua]
You had ents with a capital E and for comparing distances don't use :Distance()
Thanks guys, I'll try it later :)
[QUOTE=raBBish;27895563][lua]
local distance = 20
local npc = ents.FindByName("Your NPC name")
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
-- run your command
end
[/lua]
You had ents with a capital E and for comparing distances don't use :Distance()[/QUOTE]
You mind if I ask what is wrong with distance, compared you what you are doing? Is distance really that much more expensive?
[QUOTE=S W;27933525]You mind if I ask what is wrong with distance, compared you what you are doing? Is distance really that much more expensive?[/QUOTE]
If you don't use it often, like every frame, nothing. But the distance calculation uses square root which is a relatively slow function.
[QUOTE=raBBish;27934132]If you don't use it often, like every frame, nothing. But the distance calculation uses square root which is a relatively slow function.[/QUOTE]
Ah that is good to know as I am using it quite a bit in my code. I guess I'll be creating my own distance function then. Thanks.
[QUOTE=raBBish;27895563][lua]
local distance = 20
local npc = ents.FindByName("Your NPC name")
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
-- run your command
end
[/lua]
You had ents with a capital E and for comparing distances don't use :Distance()[/QUOTE]
This didnt work.
My current code:
[lua]function BuyColt(ply)
local distance = 20
local npc = ents.FindByName("crazy_pete")
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then -- line 38
ply:Give("weapon_crowbar")
ply:SetHealth(ply:Health() + 50)
end
end
concommand.Add("Colt_buy", BuyColt)[/lua]
The error:
[lua\entities\crazy_pete\init.lua:38] attempt to index field '?' (a nil value)
Still lost :(
My guess is that there are no entities by the name 'crazy_pete' thus causing npc[1] to be nil
Here is one I just wrote up. I'm sure it can be condensed but I didn't care too much since it was just meant to show a point.
[lua]
local v1pos = v1:GetPos()
local v2pos = v2:GetPos()
local x = v1pos.x - v2pos.x
local y = v1pos.y - v2pos.y
local z = v1pos.z - v2pos.z
if (x*x + y*y + z*z) < (150^2) then
--your code
end
[/lua]
[QUOTE=ralle105;27999969]My guess is that there are no entities by the name 'crazy_pete' thus causing npc[1] to be nil[/QUOTE]
Wrong, I made him.
@S W
I'll give it a go. Thanks for the effort :)
[QUOTE=Busymonkey;28014958]Wrong, I made him.[/QUOTE]
I actually took the time to test your code and as I suspected I didn't get any errors when having an entity named 'crazy_pete' in the map. You might wanna double check the name because there's nothing wrong with that code.
[QUOTE=ralle105;28017397]I actually took the time to test your code and as I suspected I didn't get any errors when having an entity named 'crazy_pete' in the map. You might wanna double check the name because there's nothing wrong with that code.[/QUOTE]
Still gives me the error, and honestly, S W - I have no idea what to do with that code.
[Quote=S W]
[lua]
local v1pos = v1:GetPos()
local v2pos = v2:GetPos()
local x = v1pos.x - v2pos.x
local y = v1pos.y - v2pos.y
local z = v1pos.z - v2pos.z
if (x*x + y*y + z*z) < (150^2) then
--your code
end
[/lua][/QUOTE]
[QUOTE=Busymonkey;28018114]Still gives me the error[/QUOTE]
Then you haven't named the entity correctly.
[QUOTE=Busymonkey;28018114]Still gives me the error, and honestly, S W - I have no idea what to do with that code.[/QUOTE]
All you have to do is put in your 2 entities for v1 and v2. Actually I ended up using a more condensed version of the function I wrote above in a gamemode I'm coding for. Here is the function I wrote.
You give it 3 parametersm Entity 1's position, entity 2's position, and the maximum distance that can be between the 2. If the 2 entities are further apart than distance it will return false; otherwise it will return true.
[lua]
function Distance(pos1, pos2, distance)
if ((pos1.x - pos2.x)^2 + (pos1.y - pos2.y)^2 + (pos1.z - pos2.z)^2) < (distance^2) then
return true
else
return false
end
end
[/lua]
[QUOTE=S W;28006146]Here is one I just wrote up. I'm sure it can be condensed but I didn't care too much since it was just meant to show a point.
[lua]
local v1pos = v1:GetPos()
local v2pos = v2:GetPos()
local x = v1pos.x - v2pos.x
local y = v1pos.y - v2pos.y
local z = v1pos.z - v2pos.z
if (x*x + y*y + z*z) < (150^2) then
--your code
end
[/lua][/QUOTE]
That's a bit... unneeded. You just recreated [b][url=http://wiki.garrysmod.com/?title=Vector.LengthSqr]Vector.LengthSqr [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
So your solution is practically the same as mine few posts above.
Yes it is essentially the same, but OP said it didn't work even though it should've.
[QUOTE=S W;28028658]Yes it is essentially the same, but OP said it didn't work even though it should've.[/QUOTE]
The problem isn't that function, it's that there's no entity by that name.
So, I tried some new things:
[lua]function BuyColt(ply)
local distance = 20
local npc = ents.FindByClass('crazy_pete')
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
ply:ChatPrint("Was")
print("It worked!")
end
end
concommand.Add("Colt_buy", BuyColt)
function BuyAUG(ply)
local distance = 20
local npc = ents.FindByModel("models/humans/group03/male_07.mdl")
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
ply:Give("weapon_crowbar")
ply:SetHealth(ply:Health() + 50)
end
end
concommand.Add("AUG_buy", BuyAUG)[/lua]
They didnt give me errors, but they didnt work either. Nothing happend.
Still ents.FindByName gives me the error.
[QUOTE=ralle105;28028983]The problem isn't that function, it's that there's no entity by that name.[/QUOTE]
[IMG]http://i55.tinypic.com/akbkoy.jpg[/IMG]
I spawn him with "ent_create crazy_pete".
[QUOTE=Busymonkey;28033660]
[img_thumb]http://i55.tinypic.com/akbkoy.jpg[/img_thumb]
I spawn him with "ent_create crazy_pete".[/QUOTE]
That's its class name, you'll want to use ents.FindByClass
[QUOTE=ralle105;28033933]That's its class name, you'll want to use ents.FindByClass[/QUOTE]
Read my post above.
[QUOTE=Busymonkey;28033660]So, I tried some new things:
[lua]
function BuyColt(ply)
local distance = 20
local npc = ents.FindByClass("crazy_pete")--use double quotes here
if npc[1]:GetPos():Distance( ply:GetPos()) < distance then --simplified this
ply:ChatPrint("Was")
print("It worked!")
end
end
concommand.Add("Colt_buy", BuyColt)
function BuyAUG(ply)
local distance = 20
local npc = ents.FindByModel("models/humans/group03/male_07.mdl")
if npc[1]:GetPos():Distance( ply:GetPos()) < distance then
ply:Give("weapon_crowbar")
ply:SetHealth(ply:Health() + 50)
end
end
concommand.Add("AUG_buy", BuyAUG)[/lua]They didnt give me errors, but they didnt work either. Nothing happend.
Still ents.FindByName gives me the error
.[/QUOTE]
You mean ents.FindByClass not ents.FindByName because you arent using name?
[QUOTE=blackbird301;28035246]You mean ents.FindByClass not ents.FindByName because you arent using name?[/QUOTE]
I tried using it's name, which gives me an error. I tried using it's class, which didnt give me an error, but didnt work and I tried using its model, which did the same.
20 distance is too small, try 200.
Oh and try and isolate the problem in the future, in this case you could've PrintTable'd npc or commented out the distance check.
Ralle, thank you. It works with 200 as the distance, and the Ents.FindByModel. Thanks for your effort guys :)
Sorry, you need to Log In to post a reply to this thread.