SDL_SetError(3)

πŸ’– SDL_SetError πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS manual, and today's main character is SDL_SetError(3) β€” she sets the SDL error message for the current thread, and honestly? iconic behavior.


✨ NAME ✨

SDL_SetError β€” set the SDL error message for the current thread. yes literally that's her whole job and she ate πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_error.h>

bool SDL_SetError(const char *fmt, ...);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the way this works: calling this function replaces any previous error message that was set. gone. deleted. no notes. the old one does not get to stay, bestie, there is only room for one error message per thread at a time and this one is hers now ✨

and here's the part that lowkey trips people up: this function always returns false. always. every single time. no cap. why?? because SDL very frequently uses false to signify a failing result, which leads to this absolute banger of an idiom:

if (error_code) {
    return SDL_SetError("This operation has failed: %d", error_code);
}

see what she did there? set the error AND return the fail signal in one gorgeous little line. efficiency queen πŸ‘‘


πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • fmt β€” a printf()-style message format string. yes like the printf you already know and love, same energy, same % tokens πŸ“
  • ... β€” additional parameters matching % tokens in the fmt string, if any. if there are no % tokens, you can just... not pass any. she's flexible like that.

πŸ”₯ RETURN VALUE πŸ”₯

Returns false.

that's it. that's the return value. she said "I will return failure and I will mean it" 😭


🌈 THREAD SAFETY 🌈

it is safe to call this function from any thread. yes, ANY thread. go wild. per-thread error messages, no fighting, no drama, we love a peaceful multithreaded household 🫢


⭐ AVAILABILITY ⭐

this function has been available since SDL 3.2.0. so if you're on something older... bestie. upgrade. πŸ’€


πŸ‘‘ SEE ALSO πŸ‘‘


reminder bestie: the error message is per-thread, and calling SDL_SetError overwrites whatever was there before. don't set it and then act surprised when the old one's gone. iykyk πŸ’…

mwah πŸ’– that's the whole page! brought to you by Simple Directmedia Layer, running SDL 3.4.18, and yes this SDL_SetError(3) page is exactly as cute as it sounds. bye bestie, go set some errors responsibly 🧚✨

πŸ“¦ where this page comes from