ID:2151896
 
(See the best response by Ter13.)
Hey guys... I started making a game that is going to be a lot like real life.
I made a code for the Pack of Cigarettes to have an amount of 20, and the ability to take 1 out of the pack. If it gets to 0 the pack is empty and gets deleted. Here's the code I made for that.









    Pack_of_Cigarettes
icon = 'Smokes.dmi'
icon_state = "Pack of Cigarettes"
desc = "A full pack of smokes!"
Value = 12

Cigarette
icon = 'Smokes.dmi'
icon_state = "Cigarette"
desc = "A cigarette... "
Value = 1

obj/Pack_of_Cigarettes/verb
Take_Cigarette()
set src in view(1)
if (src.Cigarette_Amount >= 1)
spawn (/obj/Cigarette)
Move (/obj/Cigarette in usr.contents)
src.Cigarette_Amount-=1
usr << "You take a cigarette out of the pack."
else
usr << "The pack is empty."
del src


The code partly works... I have a full pack, I can take out 20 (sort of)and it becomes empty. but the smoke itself doesn't transfer over to my inventory...
I know where my problem is, I just don't know how to fix it, I'm still learning. ((The problem is the spawn(line) and the Move(line)

I was also wondering if you can help me with a code snippet to show the 20/20 smokes in my pack... my pack doesn't show how much is in it.

Thanks in advance.


Best response
obj/Pack_of_Cigarettes/New()
suffix = "[Cigarette_Amount] / 20"

obj/Pack_of_Cigarettes/verb
Take_Cigarette()
set src in view(1)
if(Cigarette_Amount>0)
Cigarette_Amount--
suffix = "[Cigarette_Amount] / 20"
new/obj/Cigarette(usr)
usr << "You take a cigarette out of the pack."
else
usr << "The pack is empty."
loc = null //always set loc to null before deleting an object. It speeds up deletion a bit.
del src


You were about right. spawn() seems like a bit of code to create an object, but actually the new keyword is the right instruction. The default argument of new is where the created object will go. In this case, it's going in the usr's contents, so we just use usr as the argument of the new instruction.

You can also show the number of cigarettes left in the pack in a statpanel by using the suffix variable, like I showed above.
Thanks a lot for helping me out... the code worked perfectly.