Eng:How to Save an Inventory

Inventories can be a crucial part in any game (A game which has an inventory, of course) and saving/loading scripts are a bit tricky for new scratchers with limited knowledge. Here you will see a way you can implement inventories into your games.

Getting Started
First, you need a list to save a player's inventory data. You can create a list called 'Inventory' or 'Inv' or anything you like. This list will store the 'Item Id's' that are used to identify certain items.

Eg:
switch costume to (item (current_itemId_looking_at_in_the_list) of [inventory v]) stamp

You will also need a variable that contains a certain code that a script will read and divide into separate item id's to add to your inventory. Name this variable something like 'InventorySaveCode' or just 'InvSav'. Finally, create a variable called 'ItemCount' or anything similar.

Saving Your Inventory
In this step you will learn how to take all the item id's from the list and make it into a single varible. First, we want to know how many items we have in the inventory list to determine how many time we will take a certain number from that list and add it to the inventory save variable. To do this, simply add the code: repeat (length of [inventory v]) This will repeat the action of adding the item id's from the list to the inventory save variable (we will put that code in later) the amount of items there is in the inventory list. Now lets add the code that add the item id from the list to the inventory save variable. To do so add this code in the repeat block we've just added. set [inventory_save_variable v] to (item (1 v) of [inventory v])

So all the code should look like:
repeat (length of [inventory v]) set [inventory_save_variable v] to (item (1 v) of [inventory v]) If you had not noticed, something's wrong with this code. The repeat block repeats adding item 1 of the inventory, and none of the other items. To fix this, we need to use the 'ItemCount' variable we created earlier. The first code we will put in is setting the item count to 1 before we start repeating to add the items from the inventory list. This simply makes it so the code will review the first item in the inventory list first.

Eg:
set [item_count v] to [1] repeat (length of [inventory v]) set [inventory_save_variable v] to (item (1 v) of [inventory v]) We also need to make it so it actully reviews the value of 'item count', so change the 'set inventory save variable to item 1 of inventory' code to 'set inventory save variable to item item_count of inventory'. Then, add a join block after the 'set inventory save variable to' so its 'set inventory save variable to join [] ['item item_count of inventory']

Example 1:
set [inventory_save_variable v] to (join [] (item (item_count) of [inventory v]) Finally, put the inventory save varible in the empty space of the join block

Example 2:
set [inventory_save_variable v] to (join (inventory_save_variable) (item (item_count) of [inventory v]) Next, to make the piece of code review the next item, add this piece of code after the 'set inventory save variable to item item_count of inventory' block.

Example 3:
change [item_count v] by [1]

Overall Code:
set [item_count v] to [1] repeat (length of [inventory v]) set [inventory_save_variable v] to (join (inevntory_save_variable) (item (item_count) of [inventory v]) change [item_count v] by [1] If you've gotton this far, good, there's only a little bit to go. As you can see, the script adds all item id's to the inventory save variable, but one problem is that the id's are all connected together so its just one big string of numbers and there is nothing to identify any single id. to stop this, simply place this block in above the 'change item count' block. Also, add a block underneath the 'set item count' block to clear the save data before it loads (so you do not have a huge string of different saves)

Example 4:
set [item_count v] to [1] set [inventory_save_variable v] to [] repeat (length of [inventory v]) set [inventory_save_variable v] to (join (inevntory_save_variable) (item (item_count) of [inventory v]) set [inventory_save_variable v] to (join (inventory_save_variable) [:] change [item_count v] by [1] This piece of code separates each item id with a colon so you can load the save data. That's it! you have now successfully created an inventory saivng system.

Final Saving Code:
set [item_count v] to [1] set [inventory_save_variable v] to [] repeat (length of [inventory v]) set [inventory_save_variable v] to (join (inevntory_save_variable) (item (item_count) of [inventory v]) set [inventory_save_variable v] to (join (inventory_save_variable) [:] change [item_count v] by [1]

Loading Your Inventory
This bit will be a bit more tricky, but eventually we will have a working saving and loading system! First we will start out by clearing the inventory and setting the item count to one. Simply put this code in: delete (all v) of [inv_ v] set [inv_scan/read_count v] to [1] Now lets add the repeat block. This time we will repeat the length of the save because that's where we're getting out data from.

Example 1:
repeat (length of (inv_datasave)) Next, Lets add a chunk of code that will determine what the item before the one were viewing is. This will determine whether the item id has two digits or one.

Example 2:
if <(letter (inv_scan/read_count) of (inv_datasave)) = [:]> then change [inv_scan/read_count v] by (1) else change [inv_scan/read_count v] by (1) if <(letter (inv_scan/read_count) of (inv_datasave)) = [:]> then change [inv_scan/read_count v] by (-1) else change [inv_scan/read_count v] by (-1) This code checks if the id is two digits or one amd whether the digit its looking at is a colon or number. Next, lets add the pieces of code which add the item id's to the inventory list and the move on to the next digit after.

Example 3:
if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (1) else change [item_count v] by (1) if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (-1) add (letter (item_count) of (inventory_save_variable)) to [inv_ v]                   change [item_count v] by (1) else change [item_count v] by (-1) add (join (letter (item_count) of (inventory_save_variable)) (letter ((item_count) + (1)) of (inventory_save_variable))) to [inventory v]                   change [item_count v] by (2) Finally, add this code which cleans up all errors after the loading of the inventory if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (1) else change [item_count v] by (1) if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (-1) add (letter (item_count) of (inventory_save_variable)) to [inv_ v]                   change [item_count v] by (1) else change [item_count v] by (-1) add (join (letter (item_count) of (inventory_save_variable)) (letter ((item_count) + (1)) of (inventory_save_variable))) to [inventory v]                   change [item_count v] by (2) if <(length of [inv_ v]) > [10]> then repeat ((length of [inv_ v]) - (10)) delete (last v) of [inv_ v] Great! You have just learned how to make a saving and loading inventory! Here's what the final two pieces of code should look like!

Saving Final Code
set [item_count v] to [1] set [inventory_save_variable v] to [] repeat (length of [inventory v]) set [inventory_save_variable v] to (join (inevntory_save_variable) (item (item_count) of [inventory v]) set [inventory_save_variable v] to (join (inventory_save_variable) [:] change [item_count v] by [1]

Loading Final Code
repeat (length of (inventory_save_variable)) if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (1) else change [item_count v] by (1) if <(letter (item_count) of (inventory_save_variable)) = [:]> then change [item_count v] by (-1) add (letter (item_count) of (inventory_save_variable)) to [inv_ v]                   change [item_count v] by (1) else change [item_count v] by (-1) add (join (letter (item_count) of (inventory_save_variable)) (letter ((item_count) + (1)) of (inventory_save_variable))) to [inventory v]                   change [item_count v] by (2) if <(length of [inv_ v]) > [10]> then repeat ((length of [inv_ v]) - (10)) delete (last v) of [inv_ v]