Advertisement

How do you use int handles instead of string handles?

Started by May 08, 2024 03:10 AM
2 comments, last by Juliean 6 months, 3 weeks ago

I'm working on a small game engine and for my assets I use the filepath as an id

void loadAssets() {
loadMesh("path/to/test.fbx");
}

And using strings it's very easy to reuse it or to access it

void loadScene() {
GameObject player;
player.modelId = "path/to/test.fbx";
}

// During rendering in a for loop:
getMesh(object.modelId);

But using an int/unsigned int seems to be more common and better practice then strings and although I don't care about optimization or performance I am curious how do you work if something is a number? If my loadMesh() function generates some random id (uuid?) like 347856 or even just simple increments 1,2,3,… I wouldn't be able to do what I did in the above code since how would I know what id is for test.fbx? The only ideas popping into my head is using the string id to access the handle so the loadMesh() function would take the filepath and if it already exists return a handle so my loadScene() function would look like this

void loadScene() {
GameObject player;
player.modelId = loadMesh("path/to/test.fbx"); // Since this was already loaded it would return a handle
}

But the entire point of using ints seems to be to avoid strings so I think this defeats the purpose? Unless there's something I'm missing or not getting?

ShapeSpheres said:
although I don't care about optimization or performance I am curious how do you work if something is a number?

I guess performance always is the motivation, since a variable length string causes unique memory allocation per string.

To work with numbers, i see two options:
Generate a hash from the string, but requires to handle hash collisions eventually.
Pack all your strings into one allocation, using the number for the memory offset or have an offset LUT to index the string.

Personally i sometimes use fixed size strings for an alternative. 16 characters seem good enough to give things a name.

Advertisement

ShapeSpheres said:
But the entire point of using ints seems to be to avoid strings so I think this defeats the purpose? Unless there's something I'm missing or not getting?

Yes, the point of using ints is not to entirely not use any string whatsoever. Unless you use an engine with an inspector or a build-system, where you can assign references with an UI, you will generally reference assets with a name. The difference is that your second solution converts the id to an integer on load, instead of requiring string-accesses in the update/render-loop. That already is a benefit. Common engines have similar functionality - in Unitys Animator, you can access an integer via the name of a property, which you can then use to set the properties value later. I was going to post a link, but the shitty forum editor doesn't like that, so just google “Animator.GetInt”.

JoeJ said:

ShapeSpheres said:
And using strings it's very easy to reuse it or to access it


I guess performance always is the motivation, since a variable length string causes unique memory allocation per string.

Allocation isn't really the issue usually, those can be avoided. std::string can store longer strings, of up to 14 or even 16 bytes w/o allocating. And string-literals can be handled without allocating with std::string_view. It's more about processing the values - a string, whether fixed or not, requires comparison of the bytes to see if two strings are equal. An integer is just a single "cmp".

This topic is closed to new replies.

Advertisement