ID:699365
 
Keywords: all, delete, objects, set, src, view
(See the best response by Kaiochao.)
I have a noob question on what I think should be a fairly simple piece of code? Am trying to make a verb that deletes all objects within a specific range of the user's icon.

Here's the code I'm using...

Code:
obj/Rock
icon='space.dmi'
icon_state="rock"
name="big fat rock..."
verb
killrock1()
set src in view(1) //for rocks within 1 square of player
del src //delete rock


Problem description: I have to click the verb once for each rock I want to delete. What I really want is for all rocks to be deleted with a single click (could be as many as 9 rocks: one in the user's square and one in each surrounding square).

I've tried for loops, while loops, playing around with various versions of view(distance) and lists based on forums posts... nothing seems to work.

What am I doing wrong? I am just learning, so it's probably something obvious.

Well to start off:
I suggest you do something more easy and use the "for" command.

Example:
for(var/obj/Rock in view(1))


And when you got the obj located then you can proceed by:
verb
killrock()
for(var/obj/Rock/M in view(1,usr))
del(M)


Also for further notice. If you want to use the
set src in view(1)

Then type is as "set src in view(1,usr)" to make sure it takes the 1 tile AROUND the one using the verb. Aka usr.
Ah, well the problem here is that your verb is just deleting the first instance of a valid target it finds; try something like

verb
killrock1()
for(var/obj/Rock/A in range(1,usr)) //iterate through each rock around you
del(A) //delete the targets


As an aside, the use of src and usr are topics of much confusion, so take care when using them.
The difference between range and view is that view takes into account visibility and range does not; this may or may not affect your context.

Edit; ah, Master Spectra got there first!
Deathguard, I were just about to correct you for typing:
for(/obj/Rock/A in range(1,usr))

But then you changed it to "for(var/obj/Rock/A in range(1,usr))".

Welp, this is the first time I actually made it first.
In response to MasterSpectra
FYI, usr is the default center of all the view-like procs.

Also, range() gets things that aren't visible, but still within distance.

oview gets visible objects within the distance, excluding the center and the center's contents. That is, for things AROUND the player, you would use oview(usr.loc).
In response to Kaiochao
I know;
I'm just being Captain Obvious.

Lol, yeah I guess your right? As always.
The OP inferred that it should target the user's tile too, though, so you wouldn't want to use oview in this instance. And it sounds like a debugging tool, so I'm assuming range() and not view() is appropriate.
Welp, I'm not a good "programmer/coder", I just though that I would help someone since I knew the answer.
Thanks for both suggestions and the discussion. Very educational.

Both suggestions make perfect sense. However, having tried them both, neither works. I can get them both compiling w/o errors, but when I try and run either one, nothing happens. No rock objects are deleted, nor error messages. Nothing.

I added an output line to both solutions (usr << "Loop complete...") to see if the verb was running, but nothing showed up in the output box, so I guess it's not executing?

I also note that before I would get the verb command tab to populate with the verb when I got within range of a rock, whereas now the verb doesn't show up. So I'm just typing 'killrock1' into the input box to try and execute.

Did I implement this wrong? Why do I no longer get the verb list?
Also, I didn't know that about oview(). I'll keep that in mind for future reference. However, for this verb I'm trying to delete all rocks, including any that are in the user's square.
In response to OrionBPG
Best response
obj/Rock
verb/killrock1()
set src in view(1)
for(var/obj/Rock/rock in view(1)-src)
del rock
del src

The "-src" is there so the verb doesn't stop after deleting a few of the rocks. Since src is the container of the verb, deleting src will also stop the verb from processing.
That did the trick. I guess I needed to keep that first "set src in view(1)" line in there".

BTW, I tried it both with the "-src" and "del src" included and excluded. Code seems to work fine either way (i.e., it deletes all the target rocks). What's the benefit of keeping those in there?

EDIT: My initial test was bad. I can now see that if I take out those two parts and happen to be located on a rock when I execute the verb, I have to execute it twice... once to delete everything around the user, and once more to delete the one under the user.

Your suggested code does it all in one fell swoop. Nice!

Thanks to all for the prompt replies and quick education.
In response to OrionBPG
As far as I know, calling a proc on an object and deleting the object before the proc is finished will stop the proc at that point. I assume it works like that for verbs too.
I was thinking that, if the rock you activated killrock1() on was deleted before any other rocks, those other rocks wouldn't delete.