ID:141913
 
Okay, I've been searching for three hours now, for something that might be so simple. Basically, the problem is that when I buy new items, after the first item is bought it begins to restock the item. However, as soon as I select the item to equip, my ammo switches back the the amount the weapon would normally contain if you only had one of it. I've had up to 60 ammo for a weapon and as soon as I actually equip it, the ammo goes back down.

This seems strange to me, considering I've went over the code so many times (even adding desperate DEBUG messages) and nothing's adding up... Everything seems perfectly in order.

**This only happens when it's my first weapon being bought. If I already have more than one weapon, there's no problem.


Here's the 'buy' procedure.
mob
proc
buy(obj/gun/o)
var/obj/gun/w=new o
world<<"[src.lastgun] switches to:"
src.lastgun=w
world<<"[src.lastgun]"
src<<"\red [w] costs [w.value] coins, are you sure you want to buy?"
var/yes=input("[w] costs [w.value] coins, are you sure you want to buy? (You have [src.coins] coins.)") in list("Yes","No")
if(yes=="Yes")
if(w.value>src.coins)
src<<"<b>You don't have enough money."
return
for(var/obj/gun/a in src.weapons)
if(a.name==w.name)
world<<"[a] has [a.ammo] ammo." //desperate DEBUG testing..
a.ammo+=a.maxammo
world<<"[a] has [a.ammo] ammo."
world<<"[src] has [src.ammo] ammo"
src.ammo=a.ammo
world<<"[src] has [src.ammo] ammo"
src.coins-=a.value
src<<"\red Weapon restocked!"
//usr.selected_weapon.update()
src.selection_in_progress=0


I've been trying to figure out what's wrong with it. Is my for loop checking through all the weapons I have wrong? Like is it not 'really' adding the ammo to 'a'? That was one of my many many many suspicions, but I'm not sure. Here's the part of the 'Click' procedure attached to the on-screen weapons: As you can see, I have a large amount of DEBUG tests because I've been stuck on this for so long.

            if(usr.selected_weapon) //This is part of a click procedure.
usr.overlays-=usr.selected_weapon
usr.selected_weapon=src.owner
world<<"[usr] has [src.owner.ammo] ammo, as usr.owner.ammo."
world<<"[usr] has [usr.selected_weapon] selected and it has [usr.selected_weapon.ammo] ammo."
usr.selected_weapon.dir=usr.dir
usr.overlays+=usr.selected_weapon
usr.ammo=src.owner.ammo
world<<"[usr] has [usr.selected_weapon] selected and it has [usr.selected_weapon.ammo] ammo."
usr<<"<b>You have equipped <font size=5> \icon [src] </font> ([src])"
usr.selection_in_progress=0


If I'm leaving any pieces of code out, I'll add them in.

Thanks for reading.

EDIT: I'm not sure why the indentation is wrong, just ignore it.
I think you forgot to check if the player has any weapons before giving them more ammo. The first time you buy gun, it skips your loop because you never added the gun to the weapons list. You probably do that after, but you should do it before.
In response to Xooxer
Yes, I found the problem. The 'usr.lastgun' wasn't in the right place, therefore giving the user the ammo of the gun created just for the purpose of a few options. Was that what you were talking about?

--It's been fixed :)