Naming entities inside C-lite (not WED)

Posted By: mmelo

Naming entities inside C-lite (not WED) - 01/22/08 22:23

Hi,

I would love to "mark" an entity I created via script with a name, so I could later retrieve it with ent_for_name() (or something equally simple). The reason for me needing this is so I don't have to keep arrays of pointers/handlers knocking about.

Is this in any way possible?

Thanks,

Miguel
Posted By: HeelX

Re: Naming entities inside C-lite (not WED) - 01/22/08 23:31

Of course. Make a linked list which holds instances of structs. In these structs, you save a char array with the name (or a STRING, if you like) and an entity pointer. Then you write these functions:

  • putEnt(ENTITY* ent, char* name) //register an entity
  • getEnt (char* name) //returns an entity pointer to the entity
  • delEnt(char* name) // deletes the entity with the name
  • delEnt(ENTITY* ent) //deletes the entity just from the list, but not from memory (the entity is still available)
  • renEnt (char* name, char* newName) //renames the entity with given name into the new name
  • renName(ENTITY* ent, char* newName) //renames the entity with the given new name


If you don't know how to do this, contact me via pm/email.

Cheers,
Christian
Posted By: mmelo

Re: Naming entities inside C-lite (not WED) - 01/23/08 00:22

Hi Christian,

Thanks for your answer!

I appreciate that you can do that, but that way you are handling the entity cache yourself rather than letting the engine do it. I was trying to find if there was any way out of the box to do it.

Furthermore, with a linked list like that, getEnt() is bound to need to scan through the list to find what you want which could be costly. If 3dgs did it internally, they would probably implement it as an Hashmap that would make it a lot more performant.


Miguel
Posted By: HeelX

Re: Naming entities inside C-lite (not WED) - 01/23/08 06:42

Well, if you are not handling 1 million entities, this is quiet fast, even if you scan through the list with an average duration of O(n).

You can also create a binary tree and search in it - which is approx O(log (m) to base 2)... which is super fast if you have LOTS of entities. Even, in the worst case, a binary tree could have a in the worst case a O(n), when your first Item is an entity with the name "a" and then you always sort-in names with a higher rank. You can circumvent that even by implementing an AVL tree.

And if even this is not enough, you can try to make a hash list or hash tree, but this will give you only very little performance boost.

The point is: even if the developers of GS would implement this, they would take a similar approach. And since the fact that you can do this on yourself, I doubt there will be a faster implementation soon by their side.

I'm sorry. You have to write yourself these 100 lines.
Posted By: mmelo

Re: Naming entities inside C-lite (not WED) - 01/23/08 09:05

Quote:


I'm sorry. You have to write yourself these 100 lines.




Don't be...

I was really just wondering if it was in there somewhere but I was just missing it. Writing it myself is no problem whatsoever.

Once again, many thanks for your help.
© 2024 lite-C Forums