Working on property system, i made it very simple by EntIndex() but i noticed that door id change sometimes, why it happens?
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()
I like to use Entity.GetCreationID and ents.GetMapCreatedEntity for saving information about map entities
[code]local creationID = entity:GetCreationID()
-- and
local entity = ents.GetMapCreatedEntity(creationID)[/code]
[QUOTE=Handsome Matt;43299323]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][/QUOTE]
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]
[QUOTE=Willox;43299368]I like to use Entity.GetCreationID and ents.GetMapCreatedEntity for saving information about map entities
[code]local creationID = entity:GetCreationID()
-- and
local entity = ents.GetMapCreatedEntity(creationID)[/code][/QUOTE]
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.
[QUOTE=Willox;43299368]I like to use Entity.GetCreationID and ents.GetMapCreatedEntity for saving information about map entities
[code]local creationID = entity:GetCreationID()
-- and
local entity = ents.GetMapCreatedEntity(creationID)[/code][/QUOTE]
Fantastic information for the op!
[QUOTE=Acecool;43307331]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![/QUOTE]
Except that entity indexes of all map entities change when the maxplayers of the server is modified.
[QUOTE=Willox;43307923]Except that entity indexes of all map entities change when the maxplayers of the server is modified.[/QUOTE]
Not true. The first 128 are reserved for players at all times according to the wiki and Garry.
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.
[QUOTE=EvacX;43313453]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().[/QUOTE]
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.
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.
[QUOTE=Handsome Matt;43318864]is 110 your favorite number, what the fuck is that even needed for rofl.[/QUOTE]
If you just subtract as mentioned above, your number is too far below the amount. 110 is the max number I've been able to fit in the maxplayers var. I've set it higher, and 110 is still the offset that it's been to get it back to "normal".
This will work, I just tried it for ALL scenarios, try it!
[QUOTE=Acecool;43318880]If you just subtract as mentioned above, your number is too far below the amount. 110 is the max number I've been able to fit in the maxplayers var. I've set it higher, and 110 is still the offset that it's been to get it back to "normal".
This will work, I just tried it for ALL scenarios, try it![/QUOTE]
What the hell is the point of adding 110 or any other number in the first place?
[editline]27th December 2013[/editline]
I mean shit, I know you're trying to "normalize" the value, but it's still a completely arbitrary number so why does it matter?
[QUOTE=EvacX;43318925]What the hell is the point of adding 110 or any other number in the first place?
[editline]27th December 2013[/editline]
I mean shit, I know you're trying to "normalize" the value, but it's still a completely arbitrary number so why does it matter?[/QUOTE]
It's the maximum number of players that can be in a server, it's not arbitrary ( Arbitrary meaning randomly chosen, in case you didn't know what that means because throwing around "big" words without knowing the meaning isn't a good idea, especially if I've already proven why the number 110 was selected. ). It's required to calculate the proper EntIndex with MaxPlayers added to the equation.
The two bank doors on v33 by Sgt. Sickness, are 678 and 679 with 110 max_players.
Here's where the issue comes in, if you removed that number, it would show a completely different number each time you rejoined the server with a different/lower max_player setting instead of maintaining the same id each time; uniformity is what we're after.
So, 568 - 38 max players = 530. you have a range on that door id from anywhere from 568 to 458. If you're building a clientside system which wants to utilize the same number, each and every map-restart without relying on server-connectivity / net-traffic each time a player connects, you need to normalize it. Add the max that it can be, then subtract what we added. The result is the same number each and every time.
I use 678 and 679 to ID those two front doors so that a pretty outlined text can show on the door saying "Bank". Additionally, I reference those IDs to lock the doors, unlock the doors, etc depending on property owner. I'd rather store a number that will never change and make the proper calculation to ensure that, instead of giving it a range of up to 110.
I've provided the calculation to ensure the number will never change so long as no entities are spawned before the map entities. Spawn additional map-entities on InitPostEntity or after. Use the calculation if you want to avoid unnecessary net-traffic dealing with a server-side only function of GetCreationID. It will work properly, try it.
[QUOTE=Handsome Matt;43319122]Let me try and interpret the shit you just spewed... 110 isn't the maximum number of players possible, it's documented as 128, I've experienced 128, hell you even admitted it was 128 at the start of this thread.
The rest of your post.. Doesn't even answer the question you were asked; why add a constant 110.
My friend put our relationship quite nicely Ace:[/QUOTE]
I just tried 128, for me it wouldn't change to 128. I may have found the reason. I'll double check it again. I did say 128 at the beginning because that's what I thought; it seems my script was calibrated for 110. Change the number to 128, I apologize for the mix-up.
[lua]ENTITY = FindMetaTable( "Entity" );
function ENTITY:GetID( )
local _index = ( ( self:EntIndex( ) + 128 ) - game.MaxPlayers( ) );
return _index or -1;
end[/lua]
You have no idea of my one on one teaching methods, you have no say on them. I may make a mistake from time to time, but I own up to them.
[QUOTE=Acecool;43319053]in case you didn't know what that means because throwing around "big" words without knowing the meaning isn't a good idea, especially if I've already proven why the number 110 was selected[/QUOTE]
Getting personal now are we?
Also, I love how you seem to add useless parentheses everywhere in your code. Addition and subtraction are associative. And why are you putting that "or -1" at the end of your return statement? That would only ever run if _index was nil or false and it can be neither because it'd error out when trying to do arithmetic operations on it before that line would ever be reached.
[QUOTE=EvacX;43322353]Getting personal now are we?
Also, I love how you seem to add useless parentheses everywhere in your code. Addition and subtraction are associative. And why are you putting that "or -1" at the end of your return statement? That would only ever run if _index was nil or false and it can be neither because it'd error out when trying to do arithmetic operations on it before that line would ever be reached.[/QUOTE]
Because I have a few additional lines of code. Some entities pretend to be worldspawn when they are not, so in those instances it would return -1.
I understand that addition and subtraction can happen without any need for parens but I like the look of them so I use them. It's not quite the same reason why I use semi-colons, etc, but I find it makes reading it more attractive. It's just personal preference.
[QUOTE=Acecool;43323892]Because I have a few additional lines of code. Some entities pretend to be worldspawn when they are not, so in those instances it would return -1.[/QUOTE]
No, no it wouldn't. Then the id would just be 0, "0 or -1" gives 0. 0 does not evaluate to false.
[code]
print( 0 and "true" or "false" )
[/code]
gives
[code]
true
[/code]
And back on your argument for adding some number (128 presently, 110 earlier). Why? It has [I][B]no[/B][/I] significance. It's no longer the entity index, so [I]why[/I] do you bother "normalizing" it? It is [B]arbitrary[/B] because you might as well just add 666 or 1337 and it'll be just as valid as if you hadn't.
[QUOTE=Handsome Matt;43323979]He just doesn't want to admit he's wrong so he'll continue to spout complete shit until we either give up or can't comprehend the level of shitness he's spewing.[/QUOTE]
I know, and I mean on the "or -1" issue he is just outright wrong, yet he defends it.
[QUOTE=Handsome Matt;43324032]Visual Basic interprets 0 as False and any other value as True, so I guess it's kind of fitting he would assume that.[/QUOTE]
Assumptions don't make for good arguments. Especially when they're wrong.
When some entities are removed, they pretend to be worldspawn. Heres' the other bit of the code:
I'm not wrong, there's a pending bug/issue on this.
[lua]//
// Prevents World Data from being removed by "physics_entity_solver" reporting 0.
//
if ( _index == 0 && self != Entity( 0 ) ) then
Log( self, self:GetClass( ) .. " is posing as world entity 0" );
_index = nil;
end[/lua]
Here are a few examples logged:
logic_timer, sky_camera, info_camera_link, logic_relay, logic_case, logic_compare, and more. While the first bits are typically, or always, compiled into the map, there are other underlying ones which pose as worldspawn. The and more are basically effects residual from HALO when a halo is removed on the client-side, has some relation to physics/bones with ragdolls, etc.
I'll try to grab some of them if you like, all of them have been outputted by the logger and it is a bug.
Before you bash someone for having a snippet of code a certain way, understand there may be an underlying reason.
Edit: Ah, and that's the one relating to the ragdolls in my comment: physics_entity_solver
Please Acecool, present to me a scenario where the "or -1" would be evaluated.
[QUOTE=EvacX;43324081]Please Acecool, present to me a scenario where the "or -1" would be evaluated.[/QUOTE]
[quote]_index = nil;[/quote]
ohmygod are you for real kid, I think I have to spell this crap out for you.
If self:EntIndex() or game.MaxPlayers() returns 0, "or -1" is not evaluated because then _index would be 0 and evaluated as true.
If self:EntIndex() or game.MaxPlayers() returns nil, the function would [I]error[/I] because you're performing arithmetic operations on a primitive datatype (you know, the ones without operator overloading?) and thus "or -1" would never be evaluated, because the bloody line isn't even reached.
[lua]local index = nil + 1
print( "Hey guys, this doesn't print!" )[/lua]
Gives
[code]attempt to perform arithmetic on a nil value[/code]
Both of you aren't understanding. The OR is for this snippet of code:
[lua]//
// Prevents World Data from being removed by "physics_entity_solver" reporting 0.
//
if ( _index == 0 && self != Entity( 0 ) ) then
Log( self, self:GetClass( ) .. " is posing as world entity 0" );
_index = nil;
end[/lua]
It prevents 0 being returned unless it truly is worldspawn. The reason being is because of the way I network; I store some map data on the 0 index, it's referenced by game.GetWorld( ):GetFlag( _flag, _default, _private );
There was an issue that function GM:EntityRemoved( _ent ) was trying to remove "0" when worldspawn isn't to be removed because of some entities or physics controllers would be EntIndexed as 0. The additional snippet I posted is the residual from the function.
If the or -1 bothers you, remove it.
And, in case you're wondering, the snippet was in the middle. It wouldn't go on the top, which would cause an error....
[lua]function ENTITY:GetID( )
local _index = ( ( self:EntIndex( ) + 128 ) - game.MaxPlayers( ) );
//
// Prevents World Data from being removed by "physics_entity_solver" reporting 0.
//
if ( _index == 0 && self != Entity( 0 ) ) then
Log( self, self:GetClass( ) .. " is posing as world entity 0" );
_index = nil;
end
return _index or -1;
end[/lua]
I believe we've already answered the OPs question; OP please mark this as solved in the upper left-hand corner because this is going nowhere any longer.
I don't want to join this discussion, but to prevent confusion, Entity(0) does return worldspawn when ran clientside.
It's also kind of stupid that game.GetWorld() does work on the server, but not on the client. So I always had to use game.GetWorld() on the server and Entity(0) on the client.
Sorry, you need to Log In to post a reply to this thread.