So someone gave me this code to get a list of enemies to attack my NPC, but it just doesn't work, it will print "Working" but the relationship is not being set up properly and I don't know why.
local tHateNPCs = {
["npc_combine_s"] = true,
["npc_metropolice"] = true,
["npc_hunter"] = true,
["npc_poisonzombie"] = true,
["npc_zombie"] = true,
["npc_zombie_torso"] = true,
["npc_headcrab"] = true,
["npc_antlion"] = true,
["npc_headcrab_fast"] = true,
["npc_barnacle"] = true,
["npc_antlionguard"] = true,
["CombinePrison"] = true,
["CombineElite"] = true,
["PrisonShotgunner"] = true,
["ShotgunSoldier"] = true,
["npc_combine_s"] = true,
["npc_strider"] = true
}
hook.Add("OnEntityCreated", "NPCTest", function(pEntity)
if IsValid(self) and (pEntity:IsNPC() and tHateNPCs[pEntity:GetClass()]) then
pEntity:AddRelationship("recruitables D_HT 99")
end
end)
If you need another specific part of the code just ask. But I don't think it is a problem with somewhere else.
IsValid(self)
-- self is not defined in hook
I've tried it with and without that.
CombinePrison
CombineElite
PrisonShotgunner
ShotgunSoldier
-- These names will not work. Get real name class from console
Even the npc_combine_s won't work, that's the one I use to test.
Ok. Let's check.
Does hook works?
print if bools
check if opening
Hook works, prints properly under the if bools, but I think it is having an issue with setting up the relationship.
However, it I made an else statement for the bool, and it runs both, so I don't know whats wrong with the bool.
local tHateNPCs = {
["npc_combine_s"] = true,
["npc_metropolice"] = true,
["npc_hunter"] = true,
["npc_poisonzombie"] = true,
["npc_zombie"] = true,
["npc_zombie_torso"] = true,
["npc_headcrab"] = true,
["npc_antlion"] = true,
["npc_headcrab_fast"] = true,
["npc_barnacle"] = true,
["npc_antlionguard"] = true,
["CombinePrison"] = true,
["CombineElite"] = true,
["PrisonShotgunner"] = true,
["ShotgunSoldier"] = true,
["npc_combine_s"] = true,
["npc_strider"] = true
}
hook.Add("OnEntityCreated", "NPCTest", function(pEntity)
print("1")
if (tHateNPCs[pEntity:GetClass()]) then
print("2")
pEntity:AddRelationship("recruitables D_HT 99")
else
print("wrong!")
end
end)
Output:
1
2
1
wrong!
This is after spawning npc_combine_s
Hook Test
npc_combine_s
If Test true
Hook Test
weapon_shotgun
If Test Fail nil
Hook is working
First Entity spawned = it's npc_combine_s
Is it in the table? Yes
-
New entity = it's weapon_shotgun
Is it in the table? No
If you wish, you can use PlayerSpawnNPC.
-
My server crached twice. Do not add relationship for NPC that is in function
--Crashed by:
-- NPC == npc_combine_s
-- NPC:AddRelationship("npc_combine_s D_HT 99")
-- If NPC:GetClass() == relatioship npc class then crash. (By my test)
https://files.facepunch.com/forum/upload/104786/1b3cbdb8-a3f1-42a2-8a26-271508c7a241/hl2_2018-02-20_01-39-27.png
I don't understand what you mean I can use PlayerSpawnNPC, it just won't spawn the NPC and then makes a ton of errors.
[</DLib>] lua/entities/recruitables/init.lua:732: attempt to index a string value with bad key ('IsNPC' is not part of the string library)
1. error - [C]:-1
2. __index - lua/includes/extensions/string.lua:297
3. nextevent - lua/entities/recruitables/init.lua:732
4. Call - lua/dlib/modules/hook.lua:439
5. Spawn_NPC - gamemodes/sandbox/gamemode/commands.lua:428
6. LeftClick - gamemodes/sandbox/entities/weapons/gmod_tool/stools/creator.lua:27
7. unknown - gamemodes/sandbox/entities/weapons/gmod_tool/shared.lua:227
hook.Add("OnEntityCreated", "NPCTest", function(pEntity)
-- TO
hook.Add("PlayerSpawnedNPC", "NPCTest", function(ply,pEntity)
Still does nothing, it prints for both the bool, but also else. Here's the code:
local tHateNPCs = {
["npc_combine_s"] = true,
["npc_metropolice"] = true,
["npc_hunter"] = true,
["npc_poisonzombie"] = true,
["npc_zombie"] = true,
["npc_zombie_torso"] = true,
["npc_headcrab"] = true,
["npc_antlion"] = true,
["npc_headcrab_fast"] = true,
["npc_barnacle"] = true,
["npc_antlionguard"] = true,
["CombinePrison"] = true,
["CombineElite"] = true,
["PrisonShotgunner"] = true,
["ShotgunSoldier"] = true,
["npc_strider"] = true
}
hook.Add("PlayerSpawnedNPC", "NPCTest", function(ply,pEntity,weapon)
if (pEntity:IsNPC() and tHateNPCs[pEntity:GetClass()]) then
print("2")
pEntity:AddRelationship("recruitables D_HT 99")
else
print(pEntity:GetClass())
end
end)
pEntity will be everytime an NPC, so you don't need to pEntity:IsNPC()
I'm testing your code now. It works.
It works with other types of NPCs, such as npc_zombie, they attack the zombie, but it will not attack recruitables, and that is the name of my NPC. I think I need to make a bullseye ent so they know where to attack.
The hook doesn't matter - the original code adds the relationship fine. It's something to do with the nextbot code, but I haven't had the time to debug nextbot/NPC relationships.
I got it! Just needed ent_bullseye inside of the NPC, I did this before, but it would always crash, but now it works!
Sorry, you need to Log In to post a reply to this thread.