Adding custom stuff to SDL Global Properties.

Hi, good afternoon!

I would like to ask you if is it secure to add custom stuff (like custom pointers to data structures and things like that ) to the Global Properties?

For example, I have the next listing:

game_object_t *gobjects = /* whatever */;

SDL_SetPointerProperty(SDL_GetGlobalProperties(), "custom_game.game_objects", game_objects);

I ask you this question because in some cases I don’t have access to some variables and using something like this:

game_object_t *gobjects = SDL_GetPointerProperty(SDL_GetGlobalProperties(), "custom_game.game_objects",NULL);

is really useful when you don’t have access to that data structure in the context of the function and I’m not able to use global variables.

So my question is if is it secure to use that function to store stuff from my application.

Really, thank you so much in advance!
Regards

SDL3 uses it internally, so it’s stable. I’ve used it to store Tiled tilemap properties using LuaJIT so it should work fine. Just be sure to clean up any pointers you allocate.

The official SDL properties are prefixed with SDL, for example SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN is defined as "SDL.window.create.fullscreen", so it should be safe as long as your custom property names don’t start with SDL.

That said, I’m curious what advantage this has over a global variable…

1 Like

Thank you so much all of you for your answers!

I tried to show all properties before adding my own stuff because I don’t want to overwrite stuff from SDL but I was unable to do that. That’s why I added a pattern before the entry to the key.

Have a nice day!
Regards

I was questioning the same thing and just dive a bit deeper. If you check SDL_CreateProperties function, it groups each property group in an hash table, which I think has a very good implementation. You’d probably end up with a similar pattern for holding global variables for a complex app scenario

1 Like

Yeah, I agree with you, totally.

The thing is I haven’t had so much time to get into the source code of SDL3 and I have to admit I wouldn’t like to do that in this moment (maybe after I’ve finished with my projects). Right now I’m doing my job with the help of the documentation (and the wiki, examples, …) and with the binary packages installed with the package manager of the Operating System I’m using so, now, I don’t want to spend time looking for code into the source tree of SDL3.

Really thank you so much for your time and answer (all of you).