ID:2428609
 
Hello, I am trying to figure out how to spawn an item in a users inventory using
a verb. Would anyone have an example of how this would be possible?

Thank you!

What do you mean by saying "spawn"? Do you want add item to inventory?
Right, Instead of having an item on the ground and the player picks it up I want to add an item to the players inventory after an action. For example if they kill something an item automatically appears in the users inventory.

list += obj
@Kozuma3 Thanks for the reply but that is pretty vague, I'm not sure how I would implement that.

I've been working with something like this.
Code:
Ore = 0

mob/verb/Mining()
usr << "Mining Laser Engaged!"
var yield = rand(1,10)
usr.Ore += yield





So I can give the user ore with a variable but I'd rather give them an item as I want all verbs and encounters to give items they can buy and sell for more of a user run economy.

And storing all items gained as variables seems like a bad way to do it.

Hopefully this explains my question a bit more if you need further details I will try to help I appreciate any theories or ideas you or anyone can provide.

It depends on if you use an item stacking system or not, if you do, you'd just check for the existing item and increase the amount variable. If not you'd want to do something like:

var/yield = rand(1,10)
while(--yield > 0)
new/obj/ore(usr)


I don't really recommend that approach, introducing some form of stacking system would be much better.
As Nadrew said, stacking items more effective in this case.
obj/item
icon='icons.dmi'
var
stackable=0
amt=1

New(list/params)
..()
icon_state=name
for(var/v in params)
vars[v] = params[v]

mob
list/inventory = list()

proc/addItem(obj/item/thisItem, amt=1)
if(!thisItem) return
if(thisItem.stackable)//if thisItem is stackable
var/obj/item/curItem
for(var/i in inventory) //im looking for the same item in my inventory
if(i:type == thisItem.type) //if i find it
curItem = i // set my helped var as founded item
if(curItem)
curItem.amt += amt // increase item amount
else
inventory += thisItem // if not found item in my inventory add it.
else
inventory += thisItem // Not stackable item, just add it.

verb/Test()
addItem(new/obj/item(list(name="Apple", stackable=1)))// add one apple in to the inventory
addItem(new/obj/item(list(name="Bananna", stackable=1)), amt=5)// add five bannanas in to the inventory
addItem(new/obj/item(list(name="Armor", def=6)))// add one armor in to the inventory
You can use locate() to determine if an item of a specific type is in the inventory, without looping.
In response to Nadrew
Nadrew wrote:
You can use locate() to determine if an item of a specific type is in the inventory, without looping.


You're right, i forget about it, could you remind me how its work?

@Edit:
Somethink like this?
 var/curItem = locate(myItem) in Inventory
In response to Marekssj3
locate(Type) in Container - is the format you're trying to remember, Marek. Pressing F1 in Dream Maker and going to the locate proc revealed this info.
var/obj/curItem = locate(myItem.type) in Inventory
if(curItem)
// Has one already
else
// Doesn't
Here's a look on how I handle it.
inventory
var list/slots, max_slots
New(MAX_SLOTS){slots = new; max_slots = MAX_SLOTS}
proc
GetItem(mob/m, obj/item/i, a = 1)
if(!m || !i || !slots){return FALSE}
if(i.can_stack == TRUE)
i.stack = a ? a : 1; i.suffix = "x [i.stack]"
var obj/item/s = LocateItem(m,i.name)
if(s)
s.stack += i.stack; s.suffix = "x [s.stack]";
UpdateItemStack(s); return TRUE
else i.stack = 1
if(max_slots <= slots.len){ground += i; return FALSE}
slots |= i
m.contents |= i
UpdateItemStack(i)
return TRUE
DropItem(mob/m,i)
if(slots && (i in slots)){slots -= i; m.contents -= i; return TRUE}
return FALSE
HasItem(mob/m,x,a=1)
var list/l, t, c
if(m && m.equipment.slots && m.equipment.slots["Mainhand"] != null){if(findtext(m.equipment.slots["Mainhand"]:name,x)){return TRUE}}
for(var/obj/item/i in m.inventory.slots)
if(i.name == x)
if(i.can_stack){if(i.stack >= a){return TRUE}}
else
c = 0
for(var/obj/item/u in m.inventory.slots){if(u.name == a){c++}}
if(c >= a){return TRUE}
l = splittext(i.name," ")
for(t in l){if(t == x){return TRUE}}
return FALSE
LocateItem(mob/m,x)
for(var/obj/item/i in m.inventory.slots){if(i.name == x){return i}}
return null