Creating an Inventory System


#1

For my game, I’m trying to make a system that adds or removes an item if you gain/lose that item.
Currently, I’ve been doing *set inventory &", Name" to add new items to the player’s inventory.

The problem is, the line of text will go on and won’t indent, as it is a stat and things like *line_break won’t work.

Also, I want players to be able to sell things, normally it would be accomplished by something like *if (stickofwonders = true), true meaning they have it, and then making the option to sell it available.

This can be accomplished, but the problem is, how do I remove the name of the item from the player’s inventory without rewriting the entire inventory? And if I do, the item orders won’t be correct and the whole thing would just be a big mess.

So is it possible to make both of these things work?


#2

I have no experience but i am pretty sure @CJW can give you a definite awnser or was it @CWJ …?


#3

I use this code, and it works really well and is fairly simple. It won’t indent as long as you put all of the if statements in a separate line- no *line_breaks needed!



Inventory: 
*line_break
*if Chemical_instilled_staff
	Chemical instilled staff,
*if Poisonous_darts=6 
	6 poisonous darts,
*if Poisonous_darts=5
	5 poisonous darts
*if Poisonous_darts=4
	4 poisonous darts
*if Poisonous_darts=3
	3 poisonous darts
*if Poisonous_darts=2
	2 poisonous darts
*if Poisonous_darts=1
	1 poisonous dart
*if Silver_chain 
	Silver chain,
*if Iron_shavings=8
	Iron shavings 8/8,
*if Iron_shavings=7
	Iron shavings 7/8,
*if Iron_shavings=6
	Iron shavings 6/8,
*if Iron_shavings=5
	Iron shavings 5/8,
*if Iron_shavings=4
	Iron shavings 4/8,
*if Iron_shavings=3
	Iron shavings 3/8,
*if Iron_shavings=2
	Iron shavings 2/8,
*if Iron_shavings=1
	Iron shavings 1/8,
*if Salt=2
	Salt 2/2,
*if Salt=1
	Salt 1/2,
*if Knife
	Knife,
*if Food_and_water
	Food and water,
*if Demon_Book_of_Names
	Demon Book of Names,
*if Candles
	Candles,
*if Flint_and_metal
	Flint and metal,
*if Razor_shield 
	Razor shield,
*if Marble_grenades 
	Marble grenades,
*if Mythril_armor 
	Mythril armor,
*if Healing_ointment=4
	Healing ointment 4/4,
*if Healing_ointment=2
	Healing ointment 2/4,
*if Flame_accelerate
	Flame accelerate,
*if Six_daggers 
	Witch's daggers,
*if Blood_charm 
	Blood charm,
*if Vials_of_poison
	Vials of poison,
*if Spiked_steel_knuckles 
	Spiked steel knuckles,
*if Staff 
	Staff,
*if Wind_Scyth 
	Wind Scythe,
*if Flame_Devil 
	Flame Devil,
*if Constricting_Vines 
	Constricting Vines,
*if Ice_Statue 
	Ice Statue,
*if Earth_Abyss 
	Earth Abyss,
*if Destruction_grenade=1
	Destruction grenade,
*if Flash_grenades=3
	3 flash grenades,
*if Flash_grenades=2
	2 flash grenades,
*if Flash_grenades=1
	1 flash grenade
*if Smoke_grenades=5
	5 smoke grenades, 
*if Smoke_grenades=4
	4 smoke grenades, 
*if Smoke_grenades=3
	3 smoke grenades,
*if Smoke_grenades=2
	2 smoke grenades,
*if Smoke_grenades=1
	1 smoke grenade, 
*if Strange_amulet
	Strange amulet,


#4

There are several solutions to creating an inventory, however the method I am using for Blackraven might be what you are looking for.

For my inventory, I have the same toggle, true / false for having the item or not. I also have a ‘placeholder’ variable for when you don’t have the item. This is simply a variable called no_sword (for example) that is set as text like this:
Set no_sword " "
between the " I use enough spaces to create the gap where the word “sword” would go.

I can then display the inventory in a grid with gaps where items are missing / not yet acquired.

After a while, you can simplify this more as some words (ie, shield / sword) have the same number of letters. The same placeholder can be used for both.
Rename the placeholder variable to X_spaces (eg, 8_spaces / 10_spaces). Trial and error will give you the correct length (it’s not as simple as 1:1 as most letters are wider than spaces).

Your code will look something like this:

[pre]*create 10_spaces " "
*create 9_spaces " "
*create 8_spaces " "
*create 7_spaces " "
*create sword false
*create shield false[/pre]

Then, in your stats page tell it to display the sword if true or the place holder if false.
The order of all items will be retained and you can arrange the items into a grid.


#5

@Samuel_H_Young & @andymwhy

Thanks for the help!
Both ideas seem great, but @Samuel_H_Young’s post seems simpler. I do like your style @andymwhy, but it seems a little over complicated for someone such as myself, I seem to be really, really bad when it comes to coding in Choicescript, and it seems that @Samuel_H_Young’s version does not require a *stat_chart.

Again, thanks for replying, and thank you @Prototype, and I think it’s @CJW.


#6

You do actually need a stat chart, but all that means is ypu have to put a
*stat_chart
above the inventory. It’s still really simple


#7

@Samuel_H_Young
Sorry, my mistake XD


#8

No problem at all. I also should tell you that it has to be in a choicescript_stats scene


#9

Sorry im new where do you add the inventory? in the startup page . . . im new to programming too


#10

In choicescript_stats, which is the file governing the ‘Show Stats’ page in a game. More info on that file (including some example inventory code) here -