ID:147505
 
I have this code, which compiles fine, but gives run-time errors :

mob
BoatShop
icon = 'Turfs.dmi'
icon_state = "BoatShop"
verb
Buy_Boat()
set src in oview(1)
var/turf/PORTS/T = locate(src.x,src.y-2,src.z)
var/mob/Boats/A = new/mob/Boats/Sloop1(T)
var/E = 1
while(E<T.MaxSpaces)
if(T.Spaces[E])
E++
if(E>=T.MaxSpaces&&T.Spaces[E])
world<<"Sorry, the dock is already filled!"
else
break
if(!T.Spaces[E])
T.Spaces[E] = A
A.pixel_x = (19*T.Spaces[E])-19
A.icon_state = "2"
A.Owner = usr
BoatName(usr,A)


Now it appears obvious to me that because T.Spaces has no length it doesn't work (although T.Spaces is defined as a list) - but I don't know how to correct my code so that it will do the bit:

if(!T.Spaces[E])
T.Spaces[E] = A
A.pixel_x = (19*T.Spaces[E])-19
A.icon_state = "2"
A.Owner = usr
BoatName(usr,A)


even if the list has no length. Thank you!

~Ease~
You cannot do that, if you tell it to access a certain index of a list the list has to be at least of length that it has that index. If you want to check and see if the list has no length and do something appropriately, use if(!Spaces.len).
In response to Loduwijk
Hmm. I changed the code, so that it now checks for no length, and works out the next procedure (not proc) accordingly. So the code now looks like this:

        verb
Buy_Boat()
set src in oview(1)
var/turf/PORTS/T = locate(src.x,src.y-2,src.z)
var/mob/Boats/A = new/mob/Boats/Sloop1(T)
var/E = 1
world<<"1"
if(T.Spaces.len)
while(E<T.MaxSpaces)
if(T.Spaces[E])
E++
if(E>=T.MaxSpaces&&T.Spaces[E])
world<<"Sorry, the dock is already filled!"
else
break
if(T.Spaces.len&&!T.Spaces[E])
T.Spaces[E] = A
A.pixel_x = (19*T.Spaces[E])-19
A.Owner = usr
BoatName(usr,A)
else if(!T.Spaces.len)
T.Spaces.Add(A)
A.pixel_x = (19*T.Spaces[E])-19
A.icon_state = "2"
A.Owner = usr
BoatName(usr,A)


But now I get the runtime error : "runtime error: Undefined operation
proc name: Buy Boat (/mob/BoatShop/verb/Buy_Boat)
usr: Ease (/mob/MAN)
src: BoatShop (/mob/BoatShop)
call stack:
BoatShop (/mob/BoatShop): Buy Boat()"

Which I have never heard of before! :S

~Ease~
In response to Ease
I started getting that recently - try taking the brackets around equations out.
In response to Hazman
Didn't work, I still get the error. Thanks though!

~Ease~
How are you defining your list?

I usually use length(L) instead of L.len; it's probably very slightly slower, but it doesn't rely on the list actually existing. =)
In response to Ease
Turn on debug mode so the runtime error output tells you what line the problem is on.