For loop to select every NPC entity when a NPC is spawned (AddEntityRelationship)
9 replies, posted
Been messing with this for hours now, basically every time a NPC spawns I want it to have the Disposition D_NU added to every other NPC on the map
This currently will only apply the Disposition to the entity spawned so he is neutral to all the NPCs currently on the map
Example: Spawn a Combine then Spawn a Zombie, Then Spawn Antlion
The combine will attack the Zombie but the Zombie wont attack the Combine, then when the Antlion is spawned he wont Attack either the Zombie or the Combine but they will attack him
[lua]
hook.Add("OnEntityCreated", "mag_ApplyNPCLove", function(ent)
if ValidEntity(ent) && ent:IsNPC() then
for k, v in pairs( ents.GetAll() ) do
ent:AddEntityRelationship(v, D_NU, 100)
print(v:GetClass())
end
end
end)
[/lua]
I know why it isn't working, its applying it to only the entity that's spawned and I want it to apply on every entity every time a new one is spawned. In my head I can sort of see how it needs to be done but when it comes to actually making it work it doesn't
Apologies for the bad thread title, wasn't really sure how to describe the problem in a sentence
While looping trough all ents you should check if the ent is a NPC.
Also, what is the error you get - if you get any.
I dont get any errors its just not functioning as I want
[lua]
hook.Add("OnEntityCreated", "mag_ApplyNPCLove", function(ent)
if ValidEntity(ent) && ent:IsNPC() then
for k, v in pairs( ents.GetAll() ) do
if v:IsNPC() then
v:AddEntityRelationship(ent, D_NU, 100)
ent:AddEntityRelationship(v, D_NU, 100)
end
print(v:GetClass())
end
end
end)[/lua]
Cheers :) worked perfectly, Now to sort out the dammn Create error
[code] [@gamemodes\magister\entities\weapons\weapon_magister\shared.lua:37] bad argument #1 to 'Create' (string expected, got nil) [/code]
Prints this about 6 times each time I shoot to spawn a npc, the npc spawns though and this never appears in singleplayer only multiplayer -_-
Post code?
SWEP Function to spawn NPCs
[lua]
function SWEP:PrimaryAttack()
local eyetrace = self.Owner:GetEyeTrace()
self:EmitSound ( self.Primary.Sound )
self:ShootEffects()
local magNPC = ents.Create(npcName)
magNPC:SetPos( eyetrace.HitPos )
magNPC:SetKeyValue( "additionalequipment", npcGive )
magNPC:Spawn()
magNPC:EmitSound("self.Primary.Sound", 400, 400 )
end
[/lua]
Function that sets variables and NPC tables
[lua]
magNPCS={}
magNPCS["NPC_COMBINE"]="npc_combine_s"
magNPCS["NPC_ZOMBIE"]="npc_zombie"
magNPCS["NPC_ROLLERMINE"]="npc_rollermine"
magNPCS["NPC_ANTLION"]="npc_antlion"
magNPCS["NPC_HEADCRAB"]="npc_headcrab"
magWEAPONS={}
magWEAPONS["NPC_COMBINE"]="weapon_ar2"
magWEAPONS["NPC_ZOMBIE"]="none"
magWEAPONS["NPC_ROLLERMINE"]="none"
magWEAPONS["NPC_ANTLION"]="none"
magWEAPONS["NPC_HEADCRAB"]="none"
function setNPC( player, command, arguments )
GetName = arguments[1]
Msg(GetName) --Debugging
npcName = magNPCS[GetName]
Msg(magNPCS[GetName]) --Debugging
npcGive = magWEAPONS[GetName]
Msg(magWEAPONS[GetName]) --Debugging
end
concommand.Add( "mag_setNPC", setNPC )
[/lua]
Function called by buttons
[lua]
function setNPC(arg)
RunConsoleCommand( "mag_setNPC", arg )
end
[/lua]
I wrote this all from my head with the little knowledge i have so it might be wayy off but it works but just throws out that error
local magNPC = ents.Create(npcName)
npcName is nil.
Its not, its set by the SWEP if it was nil then nothing would spawn
[lua]if npcName == nil then return end[/lua]
That fixed it, bit hacky but it works
PrimaryAttack runs on both client and server, ents.Create can only be used on server (does work on client, but should rarely be used there). Put a "if CLIENT then return end" at the top of the primaryAttack and see if that solves anything.
Sorry, you need to Log In to post a reply to this thread.