Entities & NPC's Feelings About Them

I’m trying to make NPCs have feelings towards my vehicle SEnts with NPC bullseyes, but I honestly have no idea why this isn’t working. I tried AddEntityRelationship and AddRelationship but neither of them work.

Can anyone see what’s wrong?

[lua]
ENT.Bullseye = NULL

bullseyeHaters = {
“npc_antlionguard”,
“npc_combine*”,
zombie”,
“npc_helicopter”,
“npc_manhack”,
“npc_metropolice”,
“npc_rollermine”,
“npc_strider”,
“npc_turret*”,
“npc_hunter”,
“antlion”
}

bullseyeRunners = {
headcrab
}

bullseyeLovers = {
“npc_citizen”,
“npc_eli”,
“npc_kleiner”,
“npc_magnusson”,
“npc_barney”,
“npc_alyx”,
“npc_dog”
}

function ENT:Initialize()

if (SERVER) then

            -- Snipped out unrelevent code

    self.Bullseye = ents.Create("npc_bullseye")   
    self.Bullseye:SetPos(self:GetPos() + (self:GetRight()) + (self:GetForward()) ) 
    self.Bullseye:SetParent( self )  
    self.Bullseye:SetKeyValue( "health","9999" )  
    self.Bullseye:SetKeyValue( "spawnflags","256" ) 
    self.Bullseye:SetNotSolid( true )  
    self.Bullseye:Spawn()  
    self.Bullseye:Activate()         
    
    for k,v in pairs(ents.FindByClass("npc_*")) do

                    -- Doesn't work, no idea why.
            for k,v in pairs(ents.FindByClass("npc_*")) do
            if ( table.HasValue( bullseyeHaters ) ) then
                v:Fire( "setrelationship", "npc_bullseye D_HT 99" )
                v:AddRelationship( "self D_HT 99" )
                v:AddEntityRelationship( self, D_HT, 15 )
                MsgN("NPC Relations - Hate!")
            elseif ( table.HasValue( bullseyeLovers ) ) then
                v:Fire( "setrelationship", "npc_bullseye D_LI 15" )
                v:AddEntityRelationship( self, D_LI, 15 )
                MsgN("NPC Relations - Like!")
            elseif ( table.HasValue( bullseyeRunners ) ) then
                v:Fire( "setrelationship", "npc_bullseye D_FR 15" )
                v:AddEntityRelationship( self, D_FR, 15 )
                MsgN("NPC Relations - Fear!")
            else
                v:Fire( "setrelationship", "npc_bullseye D_NU 15" )
                v:AddEntityRelationship( self, D_NU, 15 )
            end
                end    
            end    
    end    

end

end

[/lua]

You are not using table.HasValue correctly, You must pass to it the value you trying to test as second argument.

So if I want to pass the whole table, I just put the same entry again?

Like: table.HasValue( bullseyeHaters, bullseyeHaters )

Here, look at this page: http://wiki.garrysmod.com/page/table/HasValue

If it doesn’t help, first argument is a table you are trying to check the value in, the second argument is the value you search in those tables

I already looked at that page. I’m trying to make any of the things in the table follow it.

I tried using “*” as the second argument, but it didn’t work.

Edit: I tried this:

[lua]
for k,v in pairs(ents.FindByClass(“npc_*”)) do
v:AddEntityRelationship( self, D_HT, 15 )
end
[/lua]

Now it says AddEntityRelationship is a nil value.

table.HasValue( bullseyeHaters, v )

Also, you have two loops, one in other, for no reason.

What loops? I also don’t know what a loop is.

[lua]
for k,v in pairs(ents.FindByClass(“npc_*”)) do

        if ( table.HasValue( bullseyeHaters, v ) ) then
            v:AddEntityRelationship( self, D_HT, 15 )
            MsgN("NPC Relations - Hate!")
        elseif ( table.HasValue( bullseyeLovers, v ) ) then
            v:AddEntityRelationship( self, D_LI, 15 )
            MsgN("NPC Relations - Like!")
        elseif ( table.HasValue( bullseyeRunners, v ) ) then
            v:AddEntityRelationship( self, D_FR, 15 )
            MsgN("NPC Relations - Fear!")
        else
            v:AddEntityRelationship( self, D_HT, 15 )
            MsgN( "NPC Relations - Not Found!" )
        end
        
    end    

[/lua]

It still says AddEntityRelationship is a nil value each time it spawns. No idea why.

Each time you call table.HasValue, you’re running a loop. Your logic, instead of being O( n ) = one loop, it’s O( n^2 ) = nested loops, but you’re doing it up to 3 times = 3 nested loops inside one main-loop meaning worst-case scenario it is very expensive. I’d recommend restructuring your data-structure to use less processing power.

I have no idea what any of that means; I’m a mapper trying to make code work.

I don’t know any other way, and it also doesn’t work to begin with so I have no idea what to do.

Sakarias told me to copy it from one of his SEnts, but he had a long line of “if X or Y or …” etc using the same amount of elses I have, so I thought it would be better to use a table instead of 10-20 or statements.

Example I modified:
[lua]
if ( string.find(v:GetClass(), “npc_antlionguard”)) or
( string.find(v:GetClass(), “npc_combine*”)) or
( string.find(v:GetClass(), “zombie”)) or
( string.find(v:GetClass(), “npc_helicopter”)) or
( string.find(v:GetClass(), “npc_manhack”)) or
( string.find(v:GetClass(), “npc_metropolice”)) or
( string.find(v:GetClass(), “npc_rollermine”)) or
( string.find(v:GetClass(), “npc_strider”)) or
( string.find(v:GetClass(), “npc_turret*”)) or
( string.find(v:GetClass(), “npc_hunter”)) or
( string.find(v:GetClass(), “antlion”)) then
[/lua]