ID:1988639
 
(See the best response by As334.)
Code:
atom
proc/Flood(var/atom/a)
for(var/atom/A in world) if(A.z==src.z) if(A.icon_state==a.icon_state)
spawn() for(var/obj/tile/T in usr.Selected)
A.icon=T.icon
A.icon_state=T.icon_state


Problem description:
Hi i try to add flood brush for my map editor, every is ok but this core replace all obj/turf in world. I need to replace only all atoms touch clicked atom. I thinking about getstep or view() procs... but i have no idea...

Best response
The issue is that you're using A instead of a, since A refers to the for atom in world. It might be easier to use more descriptive variable names to avoid these problems in the future.
Thanks, but i know what i wrote :D And how this code work. I only need replace atom's eith the same icon_state, close clicked tile.


I need replace tiles only in the square.
As with most things in life, I'm sure there's a better way to do this.

However, here is a script that appears to work.

mob/var/atom/Selected

mob/proc/Flood(var/atom/ref)
if(!Selected)
src << "You must select a tile to place before flooding."
return
var/list/open = list(ref)
var/list/closed = new/list
while(open.len > 0)
for(var/atom/o in open)
for(var/turf/t in list(locate(o.x+1,o.y,o.z),locate(o.x-1,o.y,o.z),locate(o.x,o.y+1,o.z),locate(o.x,o.y-1,o.z)))
set background = 1//prevent locking
for(var/atom/a in (t.contents+t)-(open+closed))
if(a.type == o.type)
a.icon = Selected.icon
a.icon_state = Selected.icon_state
open += a
else
closed += a
open -= o
closed += o
ref.icon = Selected.icon
ref.icon_state = Selected.icon_state


A couple of critiques with your current code. You've made the procedure atom/proc, but really it serves no purpose since you are using arguments to pass which tile you are trying to flood. With that said, instead of making it atom/proc, I'd suggest making it mob/proc. The reason for this, is it allows you to use src instead of usr (which you should try to do as often as possible, because usr can be something unpredictable).

Also, I'm not sure if you're using a list for your Selected variable, and if so why. I'd recommend just creating a reference instead of a list, that way you won't have to cycle through a list just to access it (in this case, even using a list would be pointless, because only the last one would effect anything. If you're dead set on using a list, I'd suggest using indexing to select the last element of the list. Something like if(Select.len>0){currentItem = Selected[Selected.len]} ).

So, in a nutshell; try to avoid using usr instead of src, because src is more reliable, and don't use lists when you don't need them.
@"Also, I'm not sure if you're using a list for your Selected variable, and if so why."-
I use list couse we can select many liles from tileset. After i add items with icons, icon_states and x,y locs.
I was thinking it might be something like that, but wasn't entirely sure.

If so, then in order to use the above code, you'd want to do something like:

var/atom/current
if(Selected.len > 0)
current = Selected[1]//If you'd rather select the last, make it Selected[Selected.len]
else
src << "You need to select a tile in order to flood."
return


And then:
a.icon = current.icon
a.icon_state = current.icon_state

///and

ref.icon = current.icon
ref.icon_state = current.icon_state