ID:141624
 
Code:
Make_Multitile()
set src in view(usr)
var/list/states = icon_states(src.icon)
for(var/i=1, i<=states.len, i++)
var/j = states[i]
var/list/j2 = dd_text2list(j,",") // This is a proc in Deadron's TextHandling library.
var/j3 = text2num(j2[1])
var/j4 = text2num(j2[2])
if(j3 && j4 == 0)
src.icon_state = "0,0"
else
var/turf/turf/T = new(locate(src.x+j3,src.y+j4,src.z))
T.icon = src.icon
T.icon_state = j


Problem description:
This is a verb on a turf. I'm trying to make it so if you choose a big non-DMI image for the turf's icon, you can automatically make a multitile turf out of it. When I use this verb on a turf, however, all it does is replace the turf with the blank default turf. Any help?

I recently made a proc for myself to break down multi-tile dmi/other image files and sort and display them. I will just post that, and let you sort it out from there using your variables. I have it all commented, and it is short.

mob/proc/Quick_Menus(var/start_x, var/start_y, var/icon/Ic)
var/list/states = icon_states(Ic) //makes a list of every icon_state within the icon.
// var/start_x starting x position of the menu, relative to the src
// var/start_y starting y position of the menu, relative to the src
var/next_x //next x position used for the menu
var/next_y //next y position used for the menu
for(var/M in states)
if(findtext(M,",",2,3))//check to see if the x coordinate will be under 10
next_x = text2num(copytext(M,1,2)) + start_x //figures out the x coord for the next piece of the menu
next_y = text2num(copytext(M,3)) + start_y //figures out the y coord for the next piece of the menu
images += image(Ic, locate(x+next_x, y+next_y, z), "[M]") //place the next piece
else
if(findtext(M,",",3,4))//if not under 10, check to see if it will be under 100
next_x = text2num(copytext(M,1,3)) + start_x //everything the same as first block of code
next_y = text2num(copytext(M,4)) + start_y
images += image(Ic, locate(x+next_x, y+next_y, z), "[M]")

//I didn't check for over 100, because the map doesn't ever extend that far

for(var/I in images)
src << I //the menu is no good if it can't be seen now is it


Where I am not using a "src." or "usr." command in front of variable, it is because DM automatically makes it into "src." when it compiles.

For example

images += image(Ic, locate(x+next_x, y+next_y, z), "[M]")


is the same as

src.images += image(Ic, locate(src.x+next_x, src.y+next_y, src.z), "[M]")


If this doesn't help you figure it out, just reply again, and I will help out in a more direct manner. I am just trying to help you figure it out on your own, and not tell you exactly how to fix it :D Good luck.
I believe what is happening here is that the first turf that it makes is for the state "0,0". This turf is place in the exact some spot as the turf which this verb is running from. Because only one turf can exist in any one spot, the new turf replaces the old one, and the old one is deleted. When an object is deleted, any running proc (and verbs are procs) that have that object as it's source are stopped. So, as soon as it creates that first turf, the entire proc stops, meaning that no more turfs are made, and that first new turf never gets its new icon.

You should be able to get around this by explicitly setting src = null before you enter the loop which creates the new turfs. If you do that, you won't be able to access the x, y, and z variables (or any other variable) belonging to the turf, so you should make your own variables to store that data first.

And just as a note, you should give your variables descriptive names. j may be easy to type, but it isn't going to mean a thing a week from now, and it certainly doesn't mean anything to someone trying to understand and debug your code. Even if you decide not to use descriptive names in your own code, it's just bad form to post code examples on the forums like that. A variable's name is a comment on how it should be used, and is your first line of defense from bugs.
In response to Satans Spawn
Satans Spawn wrote:
For example

> images += image(Ic, locate(x+next_x, y+next_y, z), "[M]")
>

is the same as

> src.images += image(Ic, locate(src.x+next_x, src.y+next_y, src.z), "[M]")
>


This doesn't seem to make sense in your (mob) proc, because src is a mob, but the built-in images var belongs to clients. The images var you're using in the proc seems to be a custom one (as you still appear to need to output the images after adding them to it), though we're not seeing where you've declared it, so it's either a global or a mob (or ancestor) var.

Satans Spawn programmed:
>   for(var/I in images)
> src << I //the menu is no good if it can't be seen now is it
>

Also, provided the above is at least partially correct,
Since images can also be output/displayed by an alternative method, you can do this instead:
src.client.images += images

This adds (effectively, outputs) all the images in your custom list at once, with no softcoded DM loop (it loops through it internally, which is faster and also would create less overhead than doing the action in 'parts').
In response to Kaioken
Kaioken wrote:
Satans Spawn wrote:
For example

> > images += image(Ic, locate(x+next_x, y+next_y, z), "[M]")
> >

is the same as

> > src.images += image(Ic, locate(src.x+next_x, src.y+next_y, src.z), "[M]")
> >

This doesn't seem to make sense in your (mob) proc, because src is a mob, but the built-in images var belongs to clients. The images var you're using in the proc seems to be a custom one (as you still appear to need to output the images after adding them to it), though we're not seeing where you've declared it, so it's either a global or a mob (or ancestor) var.


Yeah, I forgot to mention that [src.]images was a var I created. This was my first time working with images, and didn't know client already had an images var. I should probably get around to trying to do it a little more efficiently. Maybe adding it directly to the client/images instead of the extra list...


Satans Spawn programmed:
> >     for(var/I in images)
> > src << I //the menu is no good if it can't be seen now is it
> >

Also, provided the above is at least partially correct,
Since images can also be output/displayed by an alternative method, you can do this instead:
src.client.images += images

This adds (effectively, outputs) all the images in your custom list at once, with no softcoded DM loop (it loops through it internally, which is faster and also would create less overhead than doing the action in 'parts').

I fixed that up after I posted my message and read the same tip in my original thread I created to try and optimize my code.
In response to IainPeregrine
Thanks. I set src to null before running the loop, and it works now!