Handmade Hero » Forums » Code » Hash table entity polymorphism
NelsonMandella
41 posts
#16326 Hash table entity polymorphism
3 months ago Edited by NelsonMandella on Sept. 14, 2018, 1:46 a.m.

Is the idea of using a hash-table for each entity to achieve polymorphism reasonable (haven't found much on the topic)? Like if you have a general object type and wanted to be able to do hundreds of permutations (and sub-permutations) where each has its own unique set of attributes (including potential nested hash tables) for example. Are there any really obvious drawbacks aside from the search/access cost difference and wasted memory of unused buckets.
Delix
Ryan Fleury
139 posts / 1 project

Handmade Network lead and developer of The Melodist

#16328 Hash table entity polymorphism
3 months ago

I'd say that it depends on what your game is spending most of its time doing. Are you updating a large number of entities at a time? Do the properties of these entities overlap? How much? How many entities will the game have to be concerned about? What is the problem you are trying to solve with this system? It doesn't make much sense to consider the quality of a solution in the void, severed from the problem it might solve.

Handmade Network lead and developer of The Melodist
NelsonMandella
41 posts
#16330 Hash table entity polymorphism
3 months ago Edited by NelsonMandella on Sept. 14, 2018, 1:21 p.m.

On the order of around 10,000 entities each with their own tables with full high-res updates being performed on maybe up to 200 entities per-frame with lower res updates running in the background. The core overlapping properties (animation, physics, etc.) that all entities would need will be struct members while the specific properties would belong to the hash-table. However, these specific properties would mainly be manipulated through a scripting language, where most/all of their high level logic gets done, and not on the game engine side. Each entity has a lua script, which gets called for updates/message handling - every time it gets called the dictionary is pushed onto the lua stack as a table (and popped off when it wraps up) in effect exposing and allowing the creation of unique entity properties from the script-side.

The problem would be needing a way to create hundreds of highly customized entity permutations whose properties can be referred to using strings. I can't think of any other way to do this other than to keep 10,000 lua states running to preserve their global variables. In a general sense, is there anything conceptually flawed about using hash tables for 10000+ entities in a game? Whenever I can't find much info on something like this I just assume it's for some good reason.