Ability to sell any item in inventory and buy back

Hello all! First post, loving the series. I saw a couple episodes back somebody was asking about how to sell items and buy them back, and I have implemented that with a couple simple changes. I wanted to wait until the end to make sure it was going to work with Sam’s code. It’s actually just a change to the GetAvailableConfigs function, and a small tweak to the StockItemConfig class to accommodate.

Also, apologies in advance if my code style looks different, I am not a fan of having the curly braces all spread out, I find it harder to track, just the way I learned I guess

First, we need to add a simple constructor to StockItemConfig:

public StockItemConfig(InventoryItem item){
	this.item = item;
	this.buyingDiscountPercentage = 0;
	this.initialStock = 0;
}

Second, adjust the GetAvailableConfigs function as follows:

private IEnumerable<StockItemConfig> GetAvailableConfigs() {
	int shopperLevel = GetShopperLevel();
	List<InventoryItem> itemsCovered = new List<InventoryItem>();
	foreach (var config in stockConfig) {
		if (config.levelToUnlock > shopperLevel) continue;
		if(!itemsCovered.Contains(config.item)) itemsCovered.Add(config.item);
		yield return config;
	}
	if(IsBuyingMode()){
		foreach(InventoryItem item in stockSold.Keys){
			if(itemsCovered.Contains(item)) continue;
			yield return new StockItemConfig(item);
		}	
	} else {
		Inventory inventory = currentShopper.GetComponent<Inventory>();
		if(inventory == null) yield break;
		for(int i = 0; i < inventory.GetSize(); i++){
			if(inventory.GetNumberInSlot(i) == 0) continue;
			if(itemsCovered.Contains(inventory.GetItemInSlot(i))) continue;
			yield return new StockItemConfig(inventory.GetItemInSlot(i));
		}
	}
}
7 Likes

My eyes! My eyes! (Just kidding, I can read it fine, but I once saw a flame war on Reddit that went on forever over the position of the opening curly brace – I argue for on their own line)

This is a good solution, and actually a bit less verbose than my solution. Well done!

1 Like

Thank you for this. I tried it and tested it in my game and this is exactly what I needed.
to be able to sell new looted items from the world. it also has the ability to have a buyback when the player sells an item unintentionally.

I don’t know if this will have conflicts down the line when continuing the course. but It’s working great right now.

one question I have is how do you deal with the shop in time will be filled with junk sold by the player? how do you reset the library to not include what the player sold next time he opens the shop or maybe some time for example 2 minutes to delete the newly added items not included in the original shopkeeper’s inventory?

No, there’s nothing about adding the ability to sell your non-stock inventory that will break the rest of the course. :slight_smile:

1 Like

This worked fine being able to sell items originally not included in the shopkeeper’s stock.

but in time the shopkeeper will be filled with junk my player sold. I want to clear that list List<InventoryItem> itemsCovered = new List<InventoryItem>(); whenever my player goes out of town maybe, or after some time. but I am just being lost about where or when to clear it.

1 Like

It’s entirely possible that the shops could fill up, but the other thing to consider is will the player be around that shopkeeper long enough for it to matter? If so, maybe you could store the players level, then the next time they access the shopkeeper, if the level is different, then remove any items not in the default stock? There are definitely ways to handle it, I just made the assumption for my implementation that players won’t be around a shopkeeper long enough for it to really matter.

1 Like

You’re right. I’m worried about the wrong thing. :raised_hands:

I took a very simple approach to this… I simply didn’t save the items that didn’t have a stock config attached. Buy it back now, because if you go to the next scene, it’s gone forever. :slight_smile:

I am the Forum Necromancer, sorry :zombie:
Just to say thank you all for the help, also @Ayeng_AE, about the stock refresh, I’m using Enviro for the day-night cycle and I will using it as reference for refreshing the vendor stocks so, every week you’ll get the shoppers with a bunch of new stuff.

Indeed I’ve planned to expose in someway the StockConfig, so I will be able to randomize it in many ways for example if the town is in famine, or in conflict against other towns and so on. :slight_smile:
Also 'because I want to link that kind of situations with Quest system too.

Indeed thanks to the whole RPG Courses we’ve got a really great system, it’s very easy to extend. :smiley:

Hi community,

Re-opening this thread. I recently changed to this way of selling all inventory items, but now nothing is showing on my sell screen.

What would be the cause of this? I followed this example exactly.
I am using the dual-wielding and auto-equip functionality also found on these forums, but I dont see anything in those that would affect shop selling. I can successfully buy from the vendor’s, just no list appears when i switch to selling (even with items I just bought from the vendor).

Cheers

Hi community,

Nevermind, I got it working again. I just copied the code from the RPG repo, then re-added in the code from here. I must have had some test code somewhere that I wasnt seeing.

Cheers

Privacy & Terms