ID:158839
 
Hi, i have this shop system where i use the stat panel. i have two questions:

1. as of now, when someone buys something, it removes it from the shop and adds it to the player. this is fine, but how do i make it so that it "respawns" if you like, in the shopkeeper inventory after a certain time, and if thats not possible just gives the player a new one without removing the one in the shop.

2. is there a more efficient way to do this?.:

heres the code:

mob/NPC
Store_Keeper
icon= 'NPC.dmi'
icon_state = "Shop"
npc = 1
density = 1
New()
var/list/adding = list()
var/obj/raft/o1 = new/obj/raft
o1.suffix = "[o1.buyprice]"
o1.inshop = 1
adding+= o1
var/obj/raft/o2 = new/obj/raft
o2.suffix = "[o2.buyprice]"
o2.inshop = 1
adding+= o2
var/obj/raft/o3 = new/obj/raft
o3.suffix = "[o3.buyprice]"
o3.inshop = 1
adding+= o3
var/obj/Meat/o4 = new/obj/Meat
o4.suffix = "[o4.buyprice]"
o4.inshop = 1
adding+= o4
var/obj/Meat/o5 = new/obj/Meat
o5.suffix = "[o5.buyprice]"
o5.inshop = 1
adding+= o5
var/obj/Meat/o6 = new/obj/Meat
o6.suffix = "[o6.buyprice]"
o6.inshop = 1
adding+= o6
spawn(2)
contents+=adding
Narutostory wrote:
Hi, i have this shop system where i use the stat panel. i have two questions:

1. as of now, when someone buys something, it removes it from the shop and adds it to the player. this is fine, but how do i make it so that it "respawns" if you like, in the shopkeeper inventory after a certain time, and if thats not possible just gives the player a new one without removing the one in the shop.

2. is there a more efficient way to do this?.:

heres the code:

> mob/NPC
> Store_Keeper
> icon= 'NPC.dmi'
> icon_state = "Shop"
> npc = 1
> density = 1
> New()
> var/list/adding = list()
> var/obj/raft/o1 = new/obj/raft
> o1.suffix = "[o1.buyprice]"
> o1.inshop = 1
> adding+= o1
> var/obj/raft/o2 = new/obj/raft
> o2.suffix = "[o2.buyprice]"
> o2.inshop = 1
> adding+= o2
> var/obj/raft/o3 = new/obj/raft
> o3.suffix = "[o3.buyprice]"
> o3.inshop = 1
> adding+= o3
> var/obj/Meat/o4 = new/obj/Meat
> o4.suffix = "[o4.buyprice]"
> o4.inshop = 1
> adding+= o4
> var/obj/Meat/o5 = new/obj/Meat
> o5.suffix = "[o5.buyprice]"
> o5.inshop = 1
> adding+= o5
> var/obj/Meat/o6 = new/obj/Meat
> o6.suffix = "[o6.buyprice]"
> o6.inshop = 1
> adding+= o6
> spawn(2)
> contents+=adding
>


What you could do is make differing types of these objects, since I noticed you have two objects with three different prices:

obj
shop_items
meat1
suffix = "$5"
meat2
suffix = "$10"
meat3
suffix = "$15"
raft1
suffix = "$20"
raft2
suffix = "$40"
raft3
suffix = "$60"


Then you could populate the shopkeep's inventory loads easier:

mob/npc/shopkeep
New()
..()
src.contents = newlist(typesof(/obj/shop_items)-/obj/shop_items)


Causing respawning objects can be done by using the bought object's type:

//assuming usr is the player buying stuff, current_item is the current item they're buying
var/obj/shop_items/bought_obj = new current_item.type //dont give the player the item the shopkeep has; instead, make a new one to give out
bought_obj.suffix = null //don't forget the price suffix
usr.contents += bought_obj


And you could also make some objects infinitely spawning while others can only exist once or spawn a limited amount of times doing something like this:

obj
shop_items
var/quantity = -1 //we'll assume for the purposes of our code that -1 is "infinitely spawning"
meat
suffix = "$5"
raft
suffix = "$10"
quantity = 3 //only three rafts available!
declaration_of_independence
suffix = "$100" //what a deal!
quantity = 1 //there is only one of these

//assuming we're inside the proc where items are bought again
var/obj/shop_items/bought_obj = new current_item.type
bought_obj.suffix = null //don't forget the price suffix
usr.contents += bought_obj
if(current_item.quantity > -1)
current_item.quantity--
if(!current_item.quantity) del(current_item)


Though you have to be careful if you allow varying quantities of items to be bought, since what I provided for example above only covers buying items one at a time.
In response to Mobius Evalon
What makes the most sense to me is to simply make the shopkeeper pick everything up under him in New(), and then assume everything in his contents is something he's selling:

obj/shopkeeper
New()
..()
//give time for the world to finish creating things
sleep(0)
for(var/obj/O in oview(src,0))
contents += O

verb/buy(var/obj/O as obj in contents)
set src in oview(1)
new O.type(usr)
Narutostory wrote:
1. as of now, when someone buys something, it removes it from the shop and adds it to the player. this is fine, but how do i make it so that it "respawns" if you like, in the shopkeeper inventory after a certain time, and if thats not possible just gives the player a new one without removing the one in the shop.

I think I'd need to know more about the overall scenario you're trying to do here.

If all you're trying to do is make sure the shopkeeper's inventory is never deplayed, then don't bother moving anything from the shop to the player, the shopkeeper doesn't even need an inventory. Instead, just create a new item every time the player buys something.

However, if you're trying to do something fancier than that, like maybe make it so the shop can only sell so many of an item at a time before it respawns, then that would involve keeping track of the items to respawn and when to respawn them.

2. is there a more efficient way to do this?.

You could probably rig up a for (each in list) loop - assuming that's the kind of efficiency you're referring to. The key is in swapping out the individual variables for members of a list. You might even be able to roll it all into a pre-existing list.
well when I started to program with byond I think it took me all of 10 mins to figure this out, it wasn't all to hard but I guess my store system could use some improvement
        weapon_shop
icon_state = "BLAH"
name = "Weapon Shop"
Click()
set src in oview(2)
switch(input("What would you like to buy?", text) in list("Basic Sword ----- 500 gold", "Katana ----- 10,000 gold", "Broad Sword ----- 100,000 gold", "Cancel"))
if ("Basic Sword ----- 500 gold")
if (usr.money >= 500)
usr.money -= 500
usr.contents += new /obj/weapons/Bsword
else
if (usr.money < 500)
alert("You do not have enough gold to buy the Basic Sword!")
return
if ("Katana ----- 10,000 gold")
if (usr.money >= 10000)
usr.money -= 10000
usr.contents += new /obj/weapons/Katana
else
if (usr.money < 10000)
alert("You do not have enough gold to buy the Katana!")
return
if ("Broad Sword ----- 100,000 gold")
if (usr.money >= 100000)
usr.money -= 100000
usr.contents += new /obj/weapons/Broad Sword
else
if (usr.money < 100000)
alert("You do not have enough gold to buy the Broad Sword!")
return
if ("Cancel")
alert("Ok. Come again soon!")


with this way, what it does is makes a completely new object and sticks it in their inventory, all though the whole input way of listing the items is crappy (no accutal image of the item) it works, and fairly well to because it checks if they have the money to buy the item, if they dont it disallows them to get it, and if you have enough money it will give you the object by directly creating a new thing into the invitory.