Hello,
I see this is your first mod, and it seems like a simple thing that I would like in the game too.
I was a little alarmed when you said you sort all spidertrons every second in the description.
I gave it a quick look over to see what can be improved.
Here's what I found;
-
Your init() function only checks nauvis. It should be checking all surfaces. This is a major concern for mod compatability like with warptorio and space exploration. i.e.
for k,v in pairs(game.surfaces)do local spiders=v.find_entities_filtered() etc() end
-
You are not hooking all the build and creation events, namely on_entity_cloned, on_robot_built_entity, and script_raised_built. I may have missed one.
-
I haven't used script.register_on_entity_destroyed before, it seems like a good place to use it here. There's two useful things to know here; spider.unit_number is also a unique identifier. And where you defined on_player_mined_entity event, I assume this is because on_entity_destroyed does not fire when the spidertron is picked up - I am unsure how robot_mined and script_raised_destroy interacts with this event, it may be worth checking.
-
With your current setup, you are only catching the default spidertrons. If a mod creates a new spidertron prototype, your mod will not sort its inventory. It may be better to check created_entity.type instead of .name, if you want to sort mod-created spidertrons.
-
You could use a step-iterator instead of sorting ALL spidertrons every second to save lag frames. This means sorting one spidertron every second and "step" to the next one, so every second only one is sorted. This spreads the computational load over time which can stop lag frames if a save has lots of spidertrons. You can do this with the lua next() function, which pairs() uses internally. e.g. "for k,v in pairs(table)" is the same as "for k,v in next,table".
7.5/10.
Great idea, points for effort.
Some room for optimization, a few niche cases missed, but not bad for a first script.
Well done.