C-API: Adding a (void*) payload to a sprite?

I was wondering if there's an interface on the C API for Sprites to add a pointer to a payload somehow. I imagine something like:

void playdate->sprite->setUserData( LCDSprite *sprite, void *payload );
void *playdate->sprite->getUserData( LCDSprite *sprite );

So far I've been using setTag(), and giving it an index into an array of Payload, but obviously this is limited to 256 payloads because the tag-parameter is uint8_t. When the sprite update is called, I can then index-into my the sprite Payload array.

My current use-case is where the Payload structure stores things like an Entity's vital statistics: type, inventory, vim, vigour & muchness. I would like to directly access this structure inside the sprite update-function, where you don't really know which sprite it is... just "one of them".

Maybe there is already a best-case with some other implementation, but if-so, I was unable to find it.

Any thoughts?


PS> Obviously the user is responsible for the memory management of the Payloads. If this is undesirable, perhaps a "free()-on-destroy" flag could be added to setUserData().

You can already use playdate->sprite->setUserdata and playdate->sprite->getUserdata for that.

Sounds like UserData is perfect for what you want to do, I had a similar solution in an earlier alpha build

Set like

struct Location_t* location = getLocation(x, y); // Sub in your struct here
pd->sprite->setUserdata(mySprite, (void*) location);

And then get like

void myUpdateFn(LCDSprite* s) {
  struct Location_t* location = (struct Location_t*) pd->sprite->getUserdata(s);

Oh! It's there already!
I guess my knuckle-head was showing... sorry.