This lags the game?

Here is the code: (I know ents. related functions lag the game but is there another way to use to at least make them less laggy?)



	local friendslist = {"npc_vj_eye_deusex", "npc_vj_eye_deer", "npc_vj_eye_manduco", "npc_vj_eye_forma"} -- List
	for _,x in pairs( friendslist ) do
	local hl_friendlys = ents.FindByClass( x )
	for _,x in pairs( hl_friendlys ) do


Thanks!

Is that being run in HUDPaint or something?

No, it’s being ran in a SNPC


local friendslist = {"npc_vj_eye_deusex", "npc_vj_eye_deer", "npc_vj_eye_manduco", "npc_vj_eye_forma"} -- List
	for _,x in pairs( friendslist ) do
	local hl_friendlys = ents.FindByClass( x )
	for _,x in pairs( friendslist ) do

You’re running a loop twice, or is that within each other?

Anyways, ents.Find* functions can be resource intensive… FindByClass will look through all entities ( limit is 8000 or so now ); FindByClass isn’t as resource intensive as other finds; but since you have a known list, it can be done differently.

I’d suggest a managed list. Essentially, on Init of your SNPC do the initial FindByClass and build your list. Right after, do a


local Friendlies = {
	npc_vj_eye_deusex = true;
	npc_vj_eye_deer = true;
	npc_cj_eye_manduco = true;
	npc_vj_eye_forma = true;
};

//
// Local function to limit repetitive code
//
local function IsFriendly( _class )
	if ( !_class || _class == "" ) then return false; end

	// If the entity is a friendly
	if ( Friendlies[ _class ] ) then
		return true;
	end

	// default
	return false;
end


//
//
//
function ENT:Init( )
	// Initialize the list
	if ( !self.MyFriendlyList ) then
		self.MyFriendlyList = { };
		for k, v in pairs( ents.GetAll( ) ) do
			// No sense asking for invalid entity class
			if ( !IsValid( v ) ) then continue; end

			// Friendly? Add it...
			if ( IsFriendly( v:GetClass( ) ) ) then
				// Adding Ent Index as key
				self.MyFriendlyList[ v:EntIndex( ) ] = v;
			end
		end
	end

	// When an entity is created, check to see if it is the right one..
	hook.Add( "OnEntityCreated", "ManagingMyList" .. self:EntIndex( ), function( _ent )
		// Friendly? Add it...
		if ( IsFriendly( _ent:GetClass( ) ) ) then
			// Adding Ent Index as key... self will be seen as ENT because we defined this within the scope of the ent where self is accessible; defined outside of Init and it wouldn't work.. _ent is the argument passed in when entity is created
			self.MyFriendlyList[ _ent:EntIndex( ) ] = _ent;
		end
	end );

	// When an entity is deleted, check to see if it is the right one..
	hook.Add( "EntityRemoved", "ManagingMyList" .. self:EntIndex( ), function( _ent )
		// Friendly? Remove it...
		if ( IsFriendly( _ent:GetClass( ) ) ) then
			// Because we defined the key as the ent index, we can remove the table entry without searching
			self.MyFriendlyList[ _ent:EntIndex( ) ] = nil;
		end
	end );
end

Now; you can loop through self.MyFriendlyList in an area where you need to. k will be the EntIndex, v will be the entity. By using a managed list, you’re not searching for which entities you need to do x to each frame ( another option is to store a temporary list and have a timer update the list every few seconds… but managing will keep the resource usage to the lowest ).

Hopefully this helps.

It may help if you told us the end-result you’re aiming for to ensure that this is the quickest method ( because you’re still having to loop through the managed list if you want something to be done to them each frame / call )

Thanks I will check out the code you gave me and see how works. I am just aiming for less laggy system and I need to loop it almost every tick or else the SNPC will end up attacking its friend if let’s say I made it only do it every 1 second.