Discover the addresses that tell you which sprites exist at the moment. In some games, the address for each sprite is clustered in a continuous region that we might cal sprite table. The first address is slot 0, the second address is slot one and so on... In Super Mario World, such a function would look like this (for lsnes: it draws the total number in the top left)
Language: lua
-- constants of the game
-- this, you should discover with the RAM search
local sprite_status = 0x14c8
local sprite_x_offscreen = 0x15a0
local sprite_y_offscreen = 0x186c
local total_sprites = 12
local function sprite_exits(slot)
return memory.readbyte('WRAM', sprite_status + slot) ~= 0
end
local function is_onscreen(slot)
local xoff = memory.readbyte('WRAM', sprite_x_offscreen + slot) == 0
local yoff = memory.readbyte('WRAM', sprite_y_offscreen + slot) == 0
return xoff and yoff
end
local function main()
local count = 0
for slot = 0, total_sprites - 1 do
if sprite_exits(slot) and is_onscreen(slot) then
count = count + 1
end
end
gui.text(0, 0, count, 'white', 'black')
end
-- call 'main' each time the emulator repaints the screen
callback.register('paint', main)
-- repaint now
gui.repaint()