ID:266717
 
I've tried if(contents == null) but it didnt work. Any ideas?
Thief jack wrote:
I've tried if(contents == null) but it didnt work. Any ideas?

contents is a list, which is never null. What you want is this:
if(!contents.len)
...

Lummox JR
In response to Lummox JR
How do I fix that with this then?
obj
var
SwordX = 1
obj
sword
verb
Get()
set src in oview(1)
if(contents.len)
for(var/obj/O in usr.contents)
if(istype(O,/obj/sword))
usr << "You get another sword!"
O.SwordX += 1
O.suffix = "[O.SwordX] Swords"
del(src)
else
usr << "You get [src]!"
Move(usr)
else
usr << "You get [src]!"
src.suffix = "1 Sword"
src.SwordX = 1
Move(usr)
In response to Thief Jack
if(!contents.len)
return
else
...
In response to Malver
Nope, not what i was looking for
In response to Thief Jack
Thief jack wrote:
How do I fix that with this then?
obj/var/SwordX = 1
obj/sword/verb/Get()
set src in oview(1)
if(contents.len)
for(var/obj/O in usr.contents)
if(istype(O,/obj/sword))
usr << "You get another sword!"
O.SwordX += 1
O.suffix = "[O.SwordX] Swords"
del(src)
else
usr << "You get [src]!"
Move(usr)
else
usr << "You get [src]!"
src.suffix = "1 Sword"
src.SwordX = 1
Move(usr)

The problem you're having here is that you're mixing up which things to check, and when. You've got things put in loops that don't really belong there, and the contents.len check is actually irrelevant to your problem. As the code stands now, if you have a sword and other items in contents, it could go through several checks with other items saying "You get [src]!" until finally it finds and moves the sword. (And, because the first version of the move code doesn't match the second below, src.suffix and src.SwordX are never set.)

Let's look at the loop first:
for(var/obj/O in usr.contents)
if(istype(O,/obj/sword))
usr << "You get another sword!"
O.SwordX += 1
O.suffix = "[O.SwordX] Swords"
del(src)
else
usr << "You get [src]!"
Move(usr)

The code in the else block you really only want to execute if no swords are found; however, it will run for every item found that isn't another sword. Considering the way this is all set up, what you'd probably want to do is run through the entire loop, execute the code in the if() if it matches, and after the loop just let the code fall through to what you've got under the other else:
obj/sword/verb/Get()
set src in oview(1)
for(var/obj/O in usr.contents)
if(istype(O,/obj/sword))
usr << "You get another sword!"
O.SwordX += 1
O.suffix = "[O.SwordX] Swords"
del(src)
usr << "You get [src]!"
src.suffix = "1 Sword"
src.SwordX = 1
Move(usr)

That's already much shorter. One thing you should know, though, is that you don't even need to use a loop. You can use locate() here:
var/obj/sword/O = locate(/obj/sword) in usr
if(O)
...
else
...

Lummox JR