Tutorial : Basic Inventory & Shop System


#1

For this example, here’s what you’ll need to put into startup.txt;


*create money 5000
*create empty_inventory true
*create sword false
*create sword_name "Chainsaw-Sword of Brutalizing"
*create sword_dura 99
*create cape false
*create color ""
*create material ""
*create health_potion false
*create hp_number 0

This is what you put into choicescript_stats.txt;


*temp inventory ""
*temp comma false
*if (empty_inventory)
   *set comma true
   *set inventory &"You are carrying nothing"
*if (sword)
   *set comma true
   *set inventory &"$!{sword_name} (Durabillity: ${sword_dura})"
*if (cape)
   *if comma
      *set inventory &", "
   *set comma true
   *set inventory &"A $!{color} $!{material} Cape"
*if (health_potion)
   *if comma
      *set inventory &", "
   *set comma true
   *set inventory &"Health Potion(s) (x${hp_number})"
Inventory: ${inventory}
*line_break
Money: $${money}

What’s worth noting here is that the first item in the list doesn’t neccesarily need to insert a comma since it comes first, but since the code goes through it from top to bottom, there won’t be any problems either way. For advanced users, it is recommended to create a gosub to check whether or not the inventory is empty or not as this will save you a lot of trouble in the long run. This kind of list can be used for many other things as well, companions for example. The same principles could also be arranged into a more segmented inventory with slots for the head, torso etc.

And this is what you put somewhere in the game;


*label shop
*temp buy_price_s 800
*temp sales_price_s 0
*temp sales_price_s2 0
*temp sales_price_hp 0
*temp buy_price_hp 100
*temp buy_hp 0
Hello and welcome to Generic Dan's shop! 
*label top
*set sales_price_s round((buy_price_s /2) +sword_dura)
What can I help you with today?
*line_break
Money: $${money}
*choice
   *hide_reuse #Good morning sir, that's a very nice smile you got there!
      *set buy_price_s -200
      *set buy_price_hp -50
      Listen you, I'll give you a discount if you promise to never say something so creepy ever again.
      *page_break
      *goto top
   *if (sword = false)
      *selectable_if (money >= buy_price_s) #I'd like to buy the new Chainsaw-Sword 5000! [i]($${buy_price_s})[/i]
         *set empty_inventory false
         *set sword true
         *set money -buy_price_s
         *if (money < 0)
            *set money 0
         Thank you kindly, here's your new Chainsaw-Sword 5000!
         *page_break
         *goto top
   *if (sword)
      #I'd like to sell my $!{sword_name}. [i]($${sales_price_s})[/i]
         *set sword false
         *if not ((cape) or (health_potion))
            *set empty_inventory true
         *set money +sales_price_s
         Thank you kindly, I'll be taking your $!{sword_name} then.
         *page_break
         *goto top
   *if (sword)
      #(Bang the Chainsaw-Sword on the counter)
         *set sword_dura %-10
         Hey what are you doing, you bozo?! Stop that!
         *page_break
         *goto top
   #I'd like to buy some Health Potions. [i]($${buy_price_hp} x1)[/i]
      How many would you like?
      *input_number buy_hp 0 99
      *set sales_price_hp (100 * buy_hp)
      So you would like to buy ${buy_hp} Health Potions? That'll be $${sales_price_hp} then.
      *choice
         *selectable_if (money >= sales_price_hp) #Yep. Here you go.
            *set money -sales_price_hp
            *if (money < 0)
               *set money 0
            *set empty_inventory false
            *set health_potion true
            *set hp_number buy_hp
            A pleasure doing business with you!
            *page_break
            *goto top
         #I can't afford that.
            That's too bad, see anything else you like?
            *page_break
            *goto top
   *hide_reuse *if (cape = false) #Are those Bat-Boy capes?
      Yep, promotional item. We're giving them out free for the kids. Why, you want one? What color would you like?
      *fake_choice
         #Black
            *label always_black
            *set color "black"
         #Black...
            *set color "black"
         #Black!
            *goto always_black
         #Pink!
            *set color "pink"
      *set material "polyester"
      *set cape true
      *set empty_inventory false
      Here ya go... 
      *page_break
      *goto top
   *if (hp_number > 0) #(Drink a Health Potion)
      *set hp_number -1
      *if (hp_number = 0)
         *set health_potion false
      You chug down a cool refreshing health potion.
      *page_break
      *goto top
   #I'm done shopping.
      Okay, you come back again ya hear now? Have a nice day!
      *page_break
      *goto end

*label end
You walked out and immidiately got arrested.

So this is the shop. You can buy and sell, get a discount, get some free stuff and walk out. All with a nice and easily grasped user interface.

Everything here worked on the IDE Test page. The “bang your sword on the counter” option is only there to demonstrate an example on how the resale value will decrease the more damage is done to the weapon. As usual, I used a triple spacebar indent. See my Tutorial about genders for further information on indentation. As always, I’m happy to answer questions and take requests, but the easiest way to see what the code does is to copy paste it and see for yourself. Good luck!

https://dl.dropboxusercontent.com/u/7840892/CJW/choicescript/tools/IDE/main.html#

My tutorials and many others can also be found at http://www.choiceofbox.com by @Lordirish


How do I make an Inventory type of mechanic
Basic Store/Inventory
[Tool] Chronicler - ChoiceScript Visual Code Editor
#2

Cool tutorial - just a couple of things:

That IDE Test Page is actually a proof-of-concept page for embeddable/runnable code on the wiki, it’s not really meant for testing your own code (I mean feel free to do so, but just know that it is not what it was designed for and that the page in question will eventually be removed, breaking your link). If you want to recommend people test code online, please consider using this version of IDE: https://dl.dropboxusercontent.com/u/7840892/CJW/choicescript/tools/IDE/main.html

(If nothing else, at least that version supports the stats screen too.)

In regards to your “bang on the counter” error, that’s because you can’t fairmath values of 100 or 0, they’ve got to be between (or equal to) 1 and 99.


#3

Oooh, of course. D’oh, keep forgetting that. Thanks, will change that now.

Edit: Or I would if there was an edit button on that first post but there doesn’t seem to be one… Okay so simply change sword_dura to 99 then for it to work.


#4

I changed the IDE link and swapped the sword_dura value to 99 for you, is that OK?
Just @ (or PM) me if you want anything else changing :slight_smile:


#5

Ok, cool thanks. :slight_smile:


#6

Could I have some more feedback on this one? Was it too complex, too little explanations offered, or? Need to know this stuff if I’m gonna make any more.


#7

I don’t get why people use the entire comma part of the system. My system looks like this:


choicescript_stats.txt
*label top
What to check out?
*choice
 #Stats and stuff
  *goto stats
 #Inventory
  *goto inventory
*label stats
Stats and stuff
*goto top

*label inventory
*temp noitems
*set noitems true

*comment All variables declared in mygame.js
*if (food)
 *set noitems false
 Some food
 *line_break
 *goto slot2
*else
 *goto slot2
*label slot2
*if (mysterious_key)
 *set noitems false
 A key - you don't know what it opens
 *line_break
 *goto slot3
*else
 *goto slot3
*label slot3
*if (sheep != 0)
 *set noitems false
 ${sheep} sheep live back at your farm
 *line_break
 *goto slot4
*else
 *goto slot4
*label slot4
*if (bottles = 1)
 *set noitems false
 A bottle
 *line_break
 *goto slot5
*else
 *goto slot5
*label slot5
*if (bottles > 1)
 *set noitems false
 ${bottles} bottles
 *line_break
 *goto slot6
*else
 *goto slot6
*label slot6

*if (noitems)
 Your inventory is empty.
 *goto top
*else
 *goto top

It’s a little more code, but very simple, the only main difference being that it displays each item on a new line. I actually prefer it that way, and I couldn’t make sense of the code for the inventory system other people use, so I came up with this. You can see how it works - at each slot, it checks for a specific item that should fill that space. If the item is present, it tells you so (and in certain cases, how many of that item you have) - if not, it checks the next slot, repeating the cycle.


#8

Hmm, well it wasn’t exactly the input I was lookin for, but nice to have a second opinion/method.

But yeah I’m looking to know if my tutorial makes sense, or if I need to simplify or go through the steps more. Or maybe it’s just that not many people want/need shops in their stories? I dunno, but I would really appriciate some feedback, preferably from a few people who are new at coding, as you are my target audience.


#9

On that front, I’d say it was well written and clear. The shop system in particular seems fairly useful. If I had to give a suggestion, I’d say that maybe you could explain how the code works a bit more.