This is the system I'm currently using, plus the added functionality of objects having a list of palettes (ordered by desirability) so that if all four palette slots are taken, it will settle for second, third, fourth, fifth, etc, choice. Obviously if there are zero matches the object despawns instantly.tokumaru wrote:It is possible to make mid-level palette changes completely dynamic if you load/unload palettes as objects are activated/deactivated. Each object's initialization code can check if its palette is already loaded, use the index where the palette is, and increment a counter indicating how many objects are using that palette. If the palette isn't available, load it in the next available slot and set its use count to 1. When deactivating an object, decrement the number of uses of its palette accordingly, and if that becomes 0, free the palette slot.
That way you don't have to worry about hardcoding palette changes to scroll positions, you can place enemies freely when designing your maps, you just have to mind the distance between areas patrolled by differently colored enemies, so that a palette slot is guaranteed to be available when an enemy using new colors shows up.
Furthermore, I recently added so that each palette choice on the list can point to different sprite CHR data, meaning that there can be specially adapted patterns for dealing with tight color situations. For instance, my fireball sprite is usually red and white, but it can "downgrade" to just white if it finds a palette with the colors "white, blue, green" and there is nothing better available. When it does this, it changes it's CHR data to be some patterns which has the red trimming part removed in favor of a 100% single color version.
I don't do this for everything, most sprites look terrible with too few colors, but it's a great alternative for objects that tend to randomly spawn everywhere (so you can't tactically plan it's placement out in the level editor) and objects which would seriously annoy the player if they just failed to spawn (Mario's fireballs!)