Failing at creating an inventory system


#1

Hey guys, i have been trying to create a working inventory system for a rpg game the last couple of days… and it still isn’t right.

I am just testing to see if i could buy and sell “potion_of_healing”.

I am able to buy successfully, even several and all end up on different slots.

Also works when buying the ring.

However when selling i get the correct price for it, but i cannot sell it.

At *label sellprice2 it goes to the *else sequence instead of matching it on the conditional.

Thanks in advance for the help.

Please have a look at the code:


*create slot1 "nothing"
*create slot2 "nothing"
*create slot3 "nothing"
*create slot4 "nothing"
*create slot5 "nothing"
*create slot6 "nothing"
*create slot7 "nothing"
*create slot8 "nothing"
*create buying "nothing"
*create price 0
*create money 100
*create potion_of_healing 0
*create expensive_ring 0
*create sellprice 0
*create selling "nothing"

 *start

 #Check inventory
  *gosub inv
  *goto start 
 #Buy potion of Healing
  *set buying "Potion_of_Healing"
  *set price 10
  *gosub buy 
  *goto start
 #Buy Expensive Ring
  *set buying "expensive_ring"
  *set price 100
  *gosub buy 
  *goto start 
 #Sell Item
  *gosub sell
  *goto start 

"Inventory System  

*label inv

Currently in Inventory:

Slot 1: Holding ${slot1}
*line_break
Slot 2: Holding ${slot2}
*line_break
Slot 3: Holding ${slot3}
*line_break
Slot 4: Holding ${slot4}
*line_break
Slot 5: Holding ${slot5}
*line_break
Slot 6: Holding ${slot6}
*line_break
Slot 7: Holding ${slot7}
*line_break
Slot 8: Holding ${slot8}
*line_break
*return

*label buy

*if slot1 = "nothing"
 *if money >= price
  *set slot1 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return.  
*elseif slot2 = "nothing"
 *if money >= price
  *set slot2 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return. 
*elseif slot3 = "nothing"
 *if money >= price
  *set slot3 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return. 
*elseif slot4 = "nothing"
 *if money >= price
  *set slot4 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return. 
*elseif slot5 = "nothing"
 *if money >= price
  *set slot5 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return.
*elseif slot6 = "nothing"
 *if money >= price
  *set slot6 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return. 
*elseif slot7 = "nothing"
 *if money >= price
  *set slot7 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return.  
*elseif slot8 = "nothing"
 *if money >= price
  *set slot8 buying
  *set money - price
  *return
 *else
  You don't have enough money for this.
  *return.   

*else
 You have no space in your inventory for this item.
 *return

  *label sell
  You can try to sell any of these itens:
  *line_break
  *if slot1 != "nothing"
   *line_break 
   ${slot1}
   *line_break
   *goto s2s
  *else
   *goto s2s  
  
  *label s2s 
  *line_break
  *if slot2 != "nothing"
   ${slot2} 
   *line_break
   *goto s3s
  *else
   *goto s3s
  *label s3s   
  *if slot3 != "nothing"
   *line_break
   ${slot3}
   *line_break
   *goto s4s
  *else
   *goto s4s
  *label s4s    
  *if slot4 != "nothing"
   *line_break
   ${slot4}
   *line_break
   *goto s5s
  *else
   *goto s5s
  *label s5s     
  *if slot5 != "nothing"
   *line_break
   ${slot5}
   *line_break
   *goto s6s
  *else
   *goto s6s
  *label s6s     
  *if slot6 != "nothing"
   *line_break
   ${slot6}
   *line_break
   *goto s7s
  *else
   *goto s7s
  *label s7s     
  *if slot7 != "nothing"
   *line_break
   ${slot7}
   *line_break
   *goto s8s 
  *else
   *goto s8s
  *label s8s  
  *if slot8 != "nothing"
   ${slot8} 
   *goto sell01
  *else
   *goto sell01
    
  *label sell01
  *choice
   *if (slot1 != "nothing") # ${slot1}
    *set selling slot1
    *gosub sellprice
   *if (slot2 != "nothing") # ${slot2}
     *set selling slot2
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot2}
   *if (slot3 != "nothing") # ${slot3}
     *set selling slot3
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot3}
   *if (slot4 != "nothing") # ${slot4}
     *set selling slot4
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot4}
   *if (slot5 != "nothing") # ${slot5}
     *set selling slot5
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot5}
   *if (slot6 != "nothing") # ${slot6}
     *set selling slot6
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot6}
   *if (slot7 != "nothing") # ${slot7}
     *set selling slot7
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot7}
   *if (slot8 != "nothing") # ${slot8}
     *set selling slot8
     *gosub sellprice
     You are offered ${sellprice} silvers for ${slot8}
   #Do not sell anything.
    *return
  
  *else
    You don't have anything to sell!
	*return
 
*label sellprice

*if selling = potion_of_healing
 *set sellprice 5
 *goto sellprice2
*elseif selling = expensive_ring
 *set sellprice 90
 *goto sellprice2
*else
 Error
 *finish 
 
*label sellprice2
You are offered ${sellprice} silvers for ${slot1}
*choice
 #Sell the item
  *set money + sellprice
  *if selling = slot1
   *set slot1 = "nothing"
   *goto sellprice3
  *elseif selling = slot2
   *set slot2 = "nothing"
   *return
  *elseif selling = slot3
   *set slot3 = "nothing"
   *return
  *elseif selling = slot4
   *set slot4 = "nothing"
   *return  
  *elseif selling = slot5
   *set slot5 = "nothing"
   *return
  *elseif selling = slot6
   *set slot6 = "nothing"
   *return
  *elseif selling = slot7
   *set slot7 = "nothing"
   *return
  *elseif selling = slot8
   *set slot8 = "nothing"
   *return
  *else
   ERROR
   *finish 
         
 #Do not sell the item
  *return

*label sellprice3
*return


#2

@Magma It’d make it easier to follow / see what it’s supposed to be doing if you could post it like this to retain indentation:

< pre>
code here
< /pre>

(but without those spaces)


#3

Instead of having slot1, slot2, etc. to represent capacity, you can *create a variable at 8, then decrease it when the players get an item and increase it when they lose an item. If it’s zero, forbid the players to get items. I think that’s much simpler and can achieve the same thing if you change the way inventory lists items.


#4

As Vendetta said, `

` will keep indentation.

1) One thing I'd recommend is not having labels and variables as the same thing. I don't think it will ever cause problems, but it can be very confusing to bug check.

2) On this line: `*set buying "Potion_of_Healing"` you're using a capital letter. Case matters.

3) As a general recommendation, as DSeg mentioned, I'd say it's far easier to track items as individuals, rather than having slots they can fit into.

4) I think the lines you're referring to (`*if selling = potion_of_healing`) you need `*if selling = "potion_of_healing"`.

#5

@Magma There were various problems with it (three main ones) but the following will at least work now.

However, there’s still a basic overall design flaw in that if allowing multiple items of the same name you will need to separately identify “slots” from “item name”, at least where the final part of the sale is concerned. At present, if you buy 3 potions and attempt to sell the third one, it will sell the first one, because you’re basing the ‘selling’ condition on item name, not actual slot#, and item 1 (potion) was the same as item 3 (potion), so it will always sell the first identically-named item it finds.

It’s not game-breaking, it will just look odd to the player, but it’s an easy fix if you think about (and treat) “slots” and “item name” as two different things.


*label start
*choice
  #Check inventory
    *gosub inv
    *goto start
  #Buy potion of Healing
    *set buying "Potion_of_Healing"
    *set price 10
    *gosub buy
    *goto start
  #Buy Expensive Ring
    *set buying "expensive_ring"
    *set price 100
    *gosub buy
    *goto start
  #Sell Item
    *gosub sell
    *goto start

*label inv

Currently in Inventory:

Slot 1: Holding ${slot1}
*line_break
Slot 2: Holding ${slot2}
*line_break
Slot 3: Holding ${slot3}
*line_break
Slot 4: Holding ${slot4}
*line_break
Slot 5: Holding ${slot5}
*line_break
Slot 6: Holding ${slot6}
*line_break
Slot 7: Holding ${slot7}
*line_break
Slot 8: Holding ${slot8}
*line_break
*return

*label buy

*if slot1 = "nothing"
  *if money >= price
    *set slot1 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot2 = "nothing"
  *if money >= price
    *set slot2 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot3 = "nothing"
  *if money >= price
    *set slot3 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot4 = "nothing"
  *if money >= price
    *set slot4 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot5 = "nothing"
  *if money >= price
    *set slot5 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot6 = "nothing"
  *if money >= price
    *set slot6 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot7 = "nothing"
  *if money >= price
    *set slot7 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*elseif slot8 = "nothing"
  *if money >= price
    *set slot8 buying
    *set money - price
    *return
  *else
    You don't have enough money for this.
    *return.
*else
  You have no space in your inventory for this item.
  *return

*label sell
You can try to sell any of these itens:
*line_break
*if slot1 != "nothing"
  *line_break
  ${slot1}
  *line_break
  *goto s2s
*else
  *goto s2s

*label s2s
*line_break
*if slot2 != "nothing"
  ${slot2}
  *line_break
  *goto s3s
*else
  *goto s3s
*label s3s
*if slot3 != "nothing"
  *line_break
  ${slot3}
  *line_break
  *goto s4s
*else
  *goto s4s
*label s4s
*if slot4 != "nothing"
  *line_break
  ${slot4}
  *line_break
  *goto s5s
*else
  *goto s5s
*label s5s
*if slot5 != "nothing"
  *line_break
  ${slot5}
  *line_break
  *goto s6s
*else
  *goto s6s
*label s6s
*if slot6 != "nothing"
  *line_break
  ${slot6}
  *line_break
  *goto s7s
*else
  *goto s7s
*label s7s
*if slot7 != "nothing"
  *line_break
  ${slot7}
  *line_break
  *goto s8s
*else
  *goto s8s
*label s8s
*if slot8 != "nothing"
  ${slot8}
  *goto sell01
*else
  *goto sell01

*label sell01
*choice
  *if (slot1 != "nothing")
    # ${slot1}
      *set selling slot1
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot1}
      *return
  *if (slot2 != "nothing")
    # ${slot2}
      *set selling slot2
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot2}
      *return
  *if (slot3 != "nothing")
    # ${slot3}
      *set selling slot3
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot3}
      *return
  *if (slot4 != "nothing")
    # ${slot4}
      *set selling slot4
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot4}
      *return
  *if (slot5 != "nothing")
    # ${slot5}
      *set selling slot5
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot5}
      *return
  *if (slot6 != "nothing")
    # ${slot6}
      *set selling slot6
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot6}
      *return
  *if (slot7 != "nothing")
    # ${slot7}
      *set selling slot7
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot7}
      *return
  *if (slot8 != "nothing")
    # ${slot8}
      *set selling slot8
      *gosub sellprice
      You are offered ${sellprice} silvers for ${slot8}
      *return
  #Do not sell anything.
    *return

*label sellprice

*if selling = "Potion_of_Healing"
  *set sellprice 5
  *goto sellprice2
*elseif selling = "expensive_ring"
  *set sellprice 90
  *goto sellprice2
*else
  Error
  *finish

*label sellprice2
You are offered ${sellprice} silvers for ${slot1}
*choice
  #Sell the item
    *set money + sellprice
    *if selling = slot1
      *set slot1 "nothing"
      *goto sellprice3
    *elseif selling = slot2
      *set slot2 "nothing"
      *return
    *elseif selling = slot3
      *set slot3 "nothing"
      *return
    *elseif selling = slot4
      *set slot4 "nothing"
      *return
    *elseif selling = slot5
      *set slot5 "nothing"
      *return
    *elseif selling = slot6
      *set slot6 "nothing"
      *return
    *elseif selling = slot7
      *set slot7 "nothing"
      *return
    *elseif selling = slot8
      *set slot8 "nothing"
      *return
    *else
      ERROR
      *finish

  #Do not sell the item
    *return

*label sellprice3
*return


#6

You need much less variables than you have:

*create money 2500
*create inv_potion 0
*create inv_ring false
*create capacity 8

*label start
*choice
  #Check the inventory.
    You are carrying
    *if (capacity = 8)
      nothing.
    *if (capacity < 8)
      :
      *if (inv_potion > 0)
        *line_break
        x${inv_potion} Healing Potion
      *if (inv_ring)
        The Expensive Ring
    *goto start
  #Go to the shop
    *label shop
    *choice
      #Buy something.
        *label buy_shop
        *choice
          *selectable_if (money >= 25) #Buy a healing potion. [Price: 25 Money]
            *if (capacity > 0)
              *set money -25
              *set inv_potion +1
              *set capacity -1
              *goto buy_shop
            *else
              You don't have enough space in the inventory.
              *goto buy_shop
          *selectable_if ((money >= 1000) and (inv_ring = false)) #Buy the expensive ring. [Price = 1000]
            *if (capacity > 0)
              *set money -1000
              *set inv_ring true
              *set capacity -1
              *goto buy_shop
            *else
              You don't have enough space in the inventory.
              *goto buy_shop
          #Back
            *goto shop
      #Sell something.
        *label sell_shop
        *choice
          *selectable_if (inv_potion > 0) #Sell a healing potion. [Price: 25 Money]
            *set money +25
            *set inv_potion -1
            *set capacity +1
            *goto sell_shop
          *selectable_if (inv_ring) #Sell the expensive ring. [Price = 1000]
            *if (capacity > 0)
            *set money +1000
            *set inv_ring false
            *set capacity +1
            *goto sell_shop
          #Back
            *goto shop
      #Back
        *goto start

I haven’t tested this and don’t use this kind of code, but I think this block should work and do what you want, just in a different way.

And it would be much more practical if you created the inventory list in the stat screen.


#7

Thanks for the assistance. Your coding seems much more simpler… I just don’t know if its scalable when you have many items, but i will give it a try.


#8

I just noticed a mistake. You should add a

*line_break

between

*if (inv_ring)

and

The Expensive Ring

in the inventory listing part. It’s not a breaking bug, but it makes the list look differently.

Sure it’s scalable. Just add more item variables and implement them into the inventory and buy/sell lists.


#9

Also, when selling the ring

*if (capacity > 0)

is completely unneccessary. I forgot to delete it when I copied the buying list to speed up the process. And I believe that is a breaking bug. Might give you an error about indentation and even if you corrected the indentation, it would stop you from selling the ring if your inventory was full.