AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Entity Index for fast ADT Array access (https://forums.alliedmods.net/showthread.php?t=311350)

HorseTower 10-14-2018 02:40

Entity Index for fast ADT Array access
 
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.

eyal282 10-14-2018 02:49

Re: Entity Index for fast ADT Array access
 
Quote:

Originally Posted by HorseTower (Post 2619641)
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

HorseTower 10-14-2018 03:03

Re: Entity Index for fast ADT Array access
 
Quote:

Originally Posted by eyal282 (Post 2619643)
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,...};


eyal282 10-14-2018 03:45

Re: Entity Index for fast ADT Array access
 
Quote:

Originally Posted by HorseTower (Post 2619645)
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

hmmmmm 10-14-2018 06:28

Re: Entity Index for fast ADT Array access
 
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.

asherkin 10-14-2018 06:41

Re: Entity Index for fast ADT Array access
 
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.

HorseTower 10-14-2018 23:01

Re: Entity Index for fast ADT Array access
 
Thank you all for the fast responses. :wink:

Quote:

Originally Posted by hmmmmm (Post 2619661)
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 :D. 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 :)


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

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.