Need help with EntIndex()

Working on property system, i made it very simple by EntIndex() but i noticed that door id change sometimes, why it happens?

You mean… On different doors? Because that’s kind of expected behaviour.

DarkRP save doorsdata by door id but why my doors change id?

I think he means that the ID changes through map restarts.

I found the reason it depends on game.Max Players()

That’s correct. The first non player allocated entity index starts at game.MaxPlayers() + 1 so if you wanted to use EntIndex() as a unique identifier for stored data across map you could easily write a function:

[lua]local function FirstEntityIndex() return game.MaxPlayers() + 1 end[/lua]

I like to use Entity.GetCreationID and ents.GetMapCreatedEntity for saving information about map entities


local creationID = entity:GetCreationID()

-- and

local entity = ents.GetMapCreatedEntity(creationID)

How to add game.MaxPlayer() to all door id?

For example:

Id 126 its clear id without cout players so we need to add maxplayers

[LUA]
AddProperty(“Test”, “”, 150, { 126 })

function P.Buy ( ply, ent )

for k, v in pairs(Property) do
	
	if table.HasValue(v.doors, ent:EntIndex()) then
		
		local cost = v.price
		
		local id = ent:EntIndex() - game.MaxPlayers()
		
		print(id)
		
		ply:AddMoney( -cost )
		
		for k,v in pairs( v.doors ) do
			ents.GetByIndex( index ):Own( ply )
		end
		
	end
end

end
[/LUA]

[editline]24th December 2013[/editline]

Sounds good, but its serverside

You could still use ents.GetMapCreatedEntity/Entity.GetCreationID serverside and when networking use the entity index as that’ll be the same between client and server.

Door ids on maps will NEVER change, UNLESS you spawn entities before InitPostEntity. Anywhere you spawn map entities, make sure it’s in that hook.

If someone says anything else, they’re wrong an misinformed. I’ve been using EntIndex for my door system and they won’t change unless you spawn something before that hook.

I found this out because my old game-mode loader loaded everything dynamically but left the maps directly for a different system which would run at the InitPostEntity hook. The new system recursively traverses all directories, including proper map directories before that hook. So, just make sure that when you try spawning an entity, it’s done after the map is set-up.

Fantastic information for the op!

Except that entity indexes of all map entities change when the maxplayers of the server is modified.

Not true. The first 128 are reserved for players at all times according to the wiki and Garry.

No they’re not, stop talking shit. I’ve seen the engine source code, I’ve tried EntIndex with different max players, the first map entity is always game.MaxPlayers() +1.

This should work.

[lua]
local meta = FindMetaTable(“Entity”)
function meta:getEntIndex()
return self:EntIndex() - game.MaxPlayers()
end
[/lua]

Hey guys, instead of arguing about this why don’t you just test it? Oh that’s right, I just did. Acecool is wrong, they do change based on game.MaxPlayers().

You could always just use ents.FindByClass(“prop_door_rotating”)

It should be ordered by their entindexes. If not, then you can table.sort them with a custom function.

I have never had mine change, and I’ve changed from 5 to 110 players. I’ll do a quick test to see if it has in fact changed with a recent update, but I’ve tried it all in the past and it has never changed map entities for me.

Edit: Just tested several configurations / CFG Overwrite protection I have coded in to my .bat prevented changes, testing again.

Edit 2: Well I’ll be. That’s a recent change…

I’d recommend setting your server.cfg to read this:

[lua]maxplayers 110
sv_visiblemaxplayers 5[/lua]

replace 5 with how many slots you want your server to have, and then control if a player can connect over the allocated slots ( ie, special slots always allowing you in ) in conjunction with using the map creation id system.

Edit 3: Or use this instead of EntIndex:
[lua]local _index = ( ( self:EntIndex( ) + 110 ) - game.MaxPlayers( ) );[/lua]

Add 110 as that’s the maximum number of players and max that it’ll allocate, then remove the number of max players.

It’s been this way forever, I know three people with access to Source engines versions HL2 (2007 update) -> Portal 2 (2012 update) who can confirm this as fact.

[editline]27th December 2013[/editline]

yeah don’t do this, just use the special creation ID functions as stated in the first few posts. Setting max players high and then not using them; just a waste of the limited entity indices.

The issue is that if he needs it clientside for door recognition, say for door text, the server has no business needing to know how to render that data. It’s also a poor idea to have to transfer all of that extended info over when it’s not needed.

Just use this:
[lua]local _index = ( ( self:EntIndex( ) + 110 ) - game.MaxPlayers( ) );[/lua]

I have used a GetID( ) function for some time, to replace EntIndex because I didn’t know about EntIndex to start with, and I used unconventional text parsing. Then incorporated that. Now, I just incorporated the +110 and - max change, and it works regardless of max players. I’d recommend using that setup and ignoring using the server only commands.

Entire thing:
[lua]ENTITY = FindMetaTable( “Entity” );
function ENTITY:GetID( )
local _index = ( ( self:EntIndex( ) + 110 ) - game.MaxPlayers( ) );
return _index or -1;
end[/lua]

GetID is also a “protected keyword/function name” within GMod so it makes sense for it to be used with entities to me.

is 110 your favorite number, what the fuck is that even needed for rofl.