ID:146845
 
I've got a runtime error in a proc. The proc is supposed to return a obj/bigpic. The proc is supposed to load a picture of 'variable size', and set the icon_state and overlays of the obj/bigpic appropriatly.

Here's the proc:
proc
Load_Pic(mob/M, x, y, filename)
var/
icon/I
obj/bigpic/
P
PO
is = ""
is_coord

I = new(filename)
P = new()
P.icon = I
P.icon_state = "0,0"
P.screen_loc = "[x],[y]"

for(is in icon_states(I))
if(is == "0,0" || !is)
continue
is_coord = dd_text2list(is, ",")
PO = new()
PO.icon = I
PO.icon_state = is
PO.pixel_x = 32 * is_coord[1] /* Runtime error */
PO.pixel_y = 32 * is_coord[2]
P.overlays += PO

if(M && M.client)
M.client.screen += P
return P

And the runtime error:
runtime error: Undefined operation
proc name: Load Pic (/proc/Load_Pic)
  source file: loadpic.dm,30
  usr: Thanatos (/mob/live/player)
  src: null
  call stack:
Load Pic(null, 4, 4, "Data/Pics/016.bmp")

A couple of notes:
1) Yes, the image is a .bmp. I plan to change this to .png later when I've set everything up.
2) The path to the image is relative to the .dmb/.dme, and is correct. (It gives different runtime errors if it's wrong. :P)

[EDIT]
Line thrity is "PO.pixel_x = 32 * is_coord[1]"
The output of is_coord with
for(var/i in is_coord)
world << i

is:
1 0
[/EDIT]
Thanks for any help!
it would be much easier to figure out if you turned debugging on so it would state which line the error occurs on.

-Lute
In response to zackla21
zackla21 wrote:
it would be much easier to figure out if you turned debugging on so it would state which line the error occurs on.

-Lute

Sorry, good idea. Idea forgotten about that. The line has been marked in the topmost post of this thread. (Thanks)
Well, first, we'll open our runtime error reference (Thanks to crispy for this one!), then, we look for Undefined Operation. Now, there, we find:

"You tried to do a mathematical operation on something that isn't a number. For example, maybe you multiplied an object by another object, or tried to calculate the value of src divided by a number, or subtracted a text string from usr, or divided something by null."

Now, if memory serves, dd_text2list() doesn't use any math. Therefore, the problem would be:

PO.pixel_x = 32 * is_coord[1]
PO.pixel_y = 32 * is_coord[2]


Since 32 is obviously valid, that means that is_coord[1] and is_coord[2] are not. Logically, dd_text2list(is, ",") would text into a list of strings.

Even if is_coord=="1", you're still trying to multiply 32 by a text value.

Instead, try

PO.pixel_x = 32 * text2num(is_coord[1])
PO.pixel_y = 32 * text2num(is_coord[2])


That should solve the runtime error issue, but I still have some problems with the program:

First of all, After an atom's pixel_y or pixel_x reaches 127 or -127, it stops going further. Any value above 127 or below -127 stays 127 or -127, respectively. You need to account for that.

Second, you don't need all the trailing slashes! It should look like this:

proc
Load_Pic(mob/M, x, y, filename)
var
icon/I
obj/bigpic
P
PO
is = ""
is_coord


Notice that now, neither "var", nor "obj/bigpic" has a trailing slash.

Third, your variables are named badly. This, in truth, is more of an opinion, but I think that it would help everybody if you could give your variables more descriptive names than "P", "PO", and "I".

Finally, I don't recommend naming your variables after pre-existing variables such as x and y. In general, it just causes too much trouble and confusion.
In response to Wizkidd0123
Wizkidd0123 wrote:
Well, first, we'll open our runtime error reference (Thanks to crispy for this one!),
Oh wow. I didn't know that existed. /That/ has been bookmarked! Thanks!

PO.pixel_x = 32 * text2num(is_coord[1])
PO.pixel_y = 32 * text2num(is_coord[2])

Thanks! That worked!

First of all, After an atom's pixel_y or pixel_x reaches 127 or -127, it stops going further. Any value above 127 or below -127 stays 127 or -127, respectively. You need to account for that.
Hmm... that could be a problem. The images are either going to come real close or wack that limit... but I could probably offset where the 'center', is, and fix that. (The images, as I said, 'vary', (there are three different sizes), and I do know the smallest of the three loads.)

Second, you don't need all the trailing slashes!
Hehe---very bad habit of mine. I need to fix that, I know.

Third, your variables are named badly. This, in truth, is more of an opinion, but I think that it would help everybody if you could give your variables more descriptive names than "P", "PO", and "I".
(Another bad habit.) In this instance, "Picture", "Picture Overlay", and "Icon", respectively, but of course, only I'll ever know that. :-)

Thanks for all the help Wizkidd0123, on all the points! The problem is corrected, and it works nicely now. Hooray! (I'll try to not use anymore of those trailing slashes.)

-Nova