Raised This Month: $404 Target: $400
 101% 

Entity Index for fast ADT Array access


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HorseTower
New Member
Join Date: Oct 2018
Old 10-14-2018 , 02:40   Entity Index for fast ADT Array access
Reply With Quote #1

To save the adt_array index of entity related stuff, i wanted to use an array with all entity indices to get fast access to the adt_array:
Code:
Array[entity] = adt_index;
I read that "An entity index is not reliable between frames, as it can be freed from one entity and reallocated to another at any time."

Is it unsafe to use the entity index for that purpose, if i'm aware when an entity used that way gets destroyed/
Can an entity index change without the entity being destroyed("kill")?

If yes, can you recommend another way of doing this access in constant time?

If no, what is the maximum entity count in CS:GO(i found it to be 4096)...

This array would be calculated on every round_start/ So no saving of that entity index in a database.
HorseTower is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 10-14-2018 , 02:49   Re: Entity Index for fast ADT Array access
Reply With Quote #2

Quote:
Originally Posted by HorseTower View Post
To save the adt_array index of entity related stuff, i wanted to use an array with all entity indices to get fast access to the adt_array:
Code:
Array[entity] = adt_index;
I read that "An entity index is not reliable between frames, as it can be freed from one entity and reallocated to another at any time."

Is it unsafe to use the entity index for that purpose, if i'm aware when an entity used that way gets destroyed/
Can an entity index change without the entity being destroyed("kill")?

If yes, can you recommend another way of doing this access in constant time?

If no, what is the maximum entity count in CS:GO(i found it to be 4096)...

This array would be calculated on every round_start/ So no saving of that entity index in a database.
Google entindextoentref and entreftoentindex, the reference is the good one to use
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
HorseTower
New Member
Join Date: Oct 2018
Old 10-14-2018 , 03:03   Re: Entity Index for fast ADT Array access
Reply With Quote #3

Quote:
Originally Posted by eyal282 View Post
Google entindextoentref and entreftoentindex, the reference is the good one to use
Thought about that too.
My problem with this is, that i don't know what the maximum reference number will be(And i also did not find it anywhere).
Since i have to allocate the memory for it, i have to know it beforehand.
Can i assume that the maximum will be 4096(as the entity index)?
Code:
#define MAX_ENTITYREF 4096
int g_EntityRefToADTindex[MAX_ENTITYREF +1] = {-1,...};

Last edited by HorseTower; 10-14-2018 at 03:06.
HorseTower is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 10-14-2018 , 03:45   Re: Entity Index for fast ADT Array access
Reply With Quote #4

Quote:
Originally Posted by HorseTower View Post
Thought about that too.
My problem with this is, that i don't know what the maximum reference number will be(And i also did not find it anywhere).
Since i have to allocate the memory for it, i have to know it beforehand.
Can i assume that the maximum will be 4096(as the entity index)?
Code:
#define MAX_ENTITYREF 4096
int g_EntityRefToADTindex[MAX_ENTITYREF +1] = {-1,...};
If I'm not mistaken, ent reference increases by 1 for every entity created, allowing it to reach infinity on a technical level, so having two arrays is the best option. Send full code though because sometimes there are better storing methods such as targetname, if you created the entity
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
hmmmmm
Senior Member
Join Date: Mar 2017
Location: ...
Old 10-14-2018 , 06:28   Re: Entity Index for fast ADT Array access
Reply With Quote #5

In order for SourceMod to be able to distinguish between entity references and indexes, references have their highest bit set. This means that the actual value for references will be negative and can't easily be used as indexes.

What I would suggest you do instead is convert the entity reference into a string and use a StringMap. If you want an example of this being done check out my CustomKeyValues plugin: https://forums.alliedmods.net/showthread.php?t=306950

I'm sure you can reuse a lot of the code from that.
hmmmmm is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 10-14-2018 , 06:41   Re: Entity Index for fast ADT Array access
Reply With Quote #6

Using the entity index directly in your case should be fine (if you only need to handle networkable entities), if you want to be extra safe, store the entref as the first block for each array item and your data after it, then check the entref resolves to the same index on get.
__________________
asherkin is offline
HorseTower
New Member
Join Date: Oct 2018
Old 10-14-2018 , 23:01   Re: Entity Index for fast ADT Array access
Reply With Quote #7

Thank you all for the fast responses.

Quote:
Originally Posted by hmmmmm View Post
In order for SourceMod to be able to distinguish between entity references and indexes, references have their highest bit set. This means that the actual value for references will be negative and can't easily be used as indexes.

What I would suggest you do instead is convert the entity reference into a string and use a StringMap. If you want an example of this being done check out my CustomKeyValues plugin: https://forums.alliedmods.net/showthread.php?t=306950

I'm sure you can reuse a lot of the code from that.
Totally forgot about Hashmaps . I will try to go with that data structure since it will give me more flexibility and maybe even less memory usage, while still keeping the constant time access.
Btw. great plugin. I'm very happy to have it
HorseTower is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:33.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode