ID:2080969
 
(See the best response by GreatPirateEra.)
I'm trying to remove monsters from the Z level the player is on. I'm pretty sure the solution is something simple, but I'm drawing a blank. So far the closest I've got to success is with the code below, but I'm fairly sure block only works for turfs; though I don't know what else would work. Thanks for your time.

Code:
for(var/mob/Monster/M in block(locate(1,1,src.z), locate(50,50,src.z)))
del(M)
Need more information for this. How do you want to choose which mobs you want to delete? I highly doubt you just want to take an entire section of the map and delete all of it's mobs. And if so, WHY? also, you shouldn't call del() directly like that. I'd advice setting their loc to null and removing any reference to them, so that they can be garbage collected instead.
I'm wanting to delete the monsters on a specific z level that I'm using for a procedurally generated cave. By removing them I'm keeping out of walls the next time the layout is generated and I can properly place them.

Also, out of curiosity, why shouldn't I call del() like that? It is good to know about garbage collection, though. I didn't even know that was a thing.
In response to Gravity Sandwich
Best response
Why don't you just add those mobs currently in the cave to a list and when you need to get rid of them, check the list and set all their locs to null/remove all references to them? it's a lot more effective than searching for mobs you should already be keeping a reference to. About del(), it's a lot more taxing invoking it directly because the garbage collector has to look through every single reference to it and get rid of it and whatnot. It's better, performance wise, to do this yourself. I'm not too sure on how it actually works internally, but it's good practice to not call it directly. Maybe you'll be lucky and get a response from somebody more informed on the topic. You'll definitely notice the performance differences by doing a little test of your own, though.
block() returns a list of turfs, so your snippet won't work the way you intended.

Other than that, an alternative method would include using an associative list to keep track of each z-level's monsters. Then you would do something as simple as:

if(active_monsters && active_monsters["[z]"])
var mob/monster/m
for(m in active_monsters["[z]"])
m.dispose()


dispose() in this example has the responsibility of finalizing the monster so it can be deleted via garbage collection instead of being hard-deleted. This is what GreatPirateEra meant in reference to his del() usage comment.
In response to FKI
I would just add(and subtract when needed) the mobs created on an instanced map in general(the cave in this example) to a regular list. This is, of course, assuming that you'll only be needing to track the mobs in an instanced map. I honestly don't see a need to keep track of EVERY mob. That's just wasteful, as these lists could potentially be huge.
In response to GreatPirateEra
Actually memory is cheap. Calculating each mob's z level for all mobs in the game every time you want to delete some mobs is expensive and wasteful.

It used to be that memory was expensive but now most computers have 8gb or more. Keeping a list of all monsters is negligible memory usage.
In response to Red Hall Dev
I think you misunderstood me. What I meant was only keeping a list of mobs on an instanced map(not every mob) and setting that list to null whenever you are no longer using them. This doesn't involve any calculation. Keeping all mobs in a list without ever getting rid of them means they(along with anything referenced to them) will never get garbage collected.