Door help

How do i get all the doors in the map like darkrp and make them ownable as well as vehicles?

[lua]//
// Determines whether or not an entity is a door - Josh ‘Acecool’ Moser
//
META_ENTITY = FindMetaTable( “Entity” );
function META_ENTITY:IsDoor ( )
if ( !IsValid( self ) ) then return false; end
if ( !self:GetClass( ) || !self:GetModel( ) ) then return false; end
local _bSearchClass = string.find( self:GetClass( ), “door” );
local _bSearchModel = string.find( self:GetModel( ), “door” );

return ( _bSearchClass && _bSearchClass > 0 ) || ( _bSearchModel && _bSearchModel > 0 );

end

//
// List all doors in a map
//
for k, v in pairs( ents.GetAll( ) ) do
if ( v:IsDoor( ) ) then
print( "Door EntIndex: " … v:EntIndex( ) );
if ( SERVER ) then
print( "Door Creation ID: " … v:MapCreationID( ) );
print( "Door Entity: " … tostring( ents.GetMapCreatedEntity( v:MapCreationID( ) ) )
print( "Door Entity Index: " … tostring( ents.GetMapCreatedEntity( v:MapCreationID( ) ) ):EntIndex( ) );
end
end
end[/lua]

EntIndex will change based on number of max-players allowed; it’ll shift. If you use EntIndex for an id, you’ll need to map it.

To generate the ID to always reference with the map, use:
[lua]local _index = ( ( self:EntIndex( ) + 128 ) - game.MaxPlayers( ) );[/lua]

To map that id back to a real entity, put it through this:
[lua]_index - ( 128 - game.MaxPlayers( );[/lua]

What happens is when you generate the id, it generates an id which the server would have given the entity IF the maxplayers was set to 128. When you put it through the 128-maxplayers bit, it uses the generated id and converts it to a usable Entity( id ).

The first 1-maxplayers entity slots are reserved for players, so when max players is less than 128, it’ll subtract 128-maxplayers ( the difference ) from the newly created entity which is why that system works. It is what I use so I don’t need to network all of the ids to the client when they join for hundreds of doors.

The other option, is to use a SERVER only command to get the map creation id of an entity. That number will NEVER change. The downside, is that it is a SERVER function only meaning if you want to have text over a door, or anything on the clientside, you’ll need to network every single door and the EntIndex which can be received by the map creation id; I gave an example above.

As for DarkRP stuff for making them ownable, ask in the DarkRP Help thread, it is why it is there.

General Learning Lua Help Thread: http://forum.facepunch.com/showthread.php?t=1337945

Problems that don’t need their own thread ( All simple questions / quickies should go here ): http://forum.facepunch.com/showthread.php?t=1348923

DarkRP Help Thread ( ALL DarkRP Questions go here ): http://forum.facepunch.com/showthread.php?t=1249475

Looking to HIRE a coder? Read this: http://forum.facepunch.com/showthread.php?t=1257110

Coderhire Thread: http://forum.facepunch.com/showthread.php?t=1281665

Facepunch Style Fixer for GreaseMonkey: https://userstyles.org/styles/65192/facepunch-fixer

I don’t know why Entity:MapCreationID() and ents.GetMapCreatedEntity( creationID ) haven’t been made shared yet. I’ve sent in a request on Github and mentioned it several times on Facepunch to no avail. If they were shared then door systems using it could be made much easier.

Because it isn’t shared is why I don’t use it for my doors. My system works just as well; when adding doors to the property system, I generate the id it would be if max-slots are 128; then I just pass those ids through the mapper function which gives the real id which lets me easily map data to entities without needing to network each door index when a player connects.

hey well thanks so much guys i was trying to figure that out for a week now :slight_smile:

im confused on what im supposed to with local _index = ( ( self:EntIndex( ) + 128 ) - game.MaxPlayers( ) ); and _index - ( 128 - game.MaxPlayers( );

When you generate the ids to store in txt or Lua files you use the local _index = xxx method to calculateid. When you want to access the entity, pass index into the other and it’ll be the entIndex.