SCI1.1 Creating Inventory Items

Now that we have gotten this far, it is probably about time to add an inventory item or two. This chapter will teach you how to create inventory items, how to pick them up, put them down, and use them in your game.

First off, we will need to visit the messages that hold the inventory, number 15. We will need to add a new noun, and presumably a verb if you are planning to actually be able to use the item in the game. If you don't know how to do this, might I suggest that you take a moment and browse through the chapter on the message editor. For this test item, lets add the noun N_TESTER and the verb V_TEST. You can then add the look statement of your choice, but something along the lines of "This is a test inventory item." will more than suffice.

Now, we will need to edit script 15, InvItem in order to add the item to the game. Down at the bottom of the script, we will need to create a new instance of the inventory item. For this chapter, I am going to name it testItem, but you can name it anything that you would like.

Code: [Select]
(instance testItem of InvItem
    (properties
        view 900
        loop 1
        cel 1     
        cursor 900
        message V_TEST
        signal $0002
        noun N_TESTER
    )
)

Property Explanation:
  • view: defines the view that holds the images for the inventory item to be used in the  Print statement, the view inventory screen, as well as the icon that is placed in the menu bar area when the item has been selected.
  • loop: defines the loop that holds the image to be used in the Print statement as well as the view inventory screen. The loop value plus 1 holds the icon that will appear in your menu bar when the item is selected. So for example if you defined loop 4, then the menu bar icon will be in the same cel of loop 5.
  • cel: defines the cel that holds the images for the inventory items Print statement, inventory screen, and the menu bar icon.
  • cursor: defines the view that holds the cursor image for the item. Out of the box, This will default to loop 0, cel 0 and will therefore require a seperate view for each cursor. Check out the advanced chapter on inventory items to learn how to also set the loop and cel for the cursor so that you can place multiple cursors in the same view.
  • message: defines the verb to be used when the items cursor is used in the game.
  • signal: Your guess is as good as mine...
  • noun: defines the noun that represents the item, for use in the messages.

Now that you have the instance created, we will need to add the item to the game. Still in script 15, you will want to scroll up to the top of the page and find the templateInventory instance of ScrollableInventory. In the instances init method, locate:

Code: [Select]
        (self:
          // Add inventory items here.
          // add(Money AThing AnotherThing)
            eachElementDo(#lowlightColor 2)
            add(invLook invSelect invHelp invUp invDown ok)
        )

The call to add is where you will want to place your inventory items instance name. One thing to note about this list, the order that the items appear in is how the inventory items will be referenced. The first instance name listed in this section will be known to the game as item 0... the instance names are not used outside of this script but the numerical place that the item falls in this list is. Anyway, we'll address that in just a second. For now, we will want to add our item to the list. To make this section a little easier to find in the future, you'll notice that I added in a little comment section. Simply replace this whole self block with this.

Code: [Select]
        (self:     
            add(
                //******************************************************
                // Add Items Here
                //******************************************************
                testItem
               
               
               )
            eachElementDo(#lowlightColor 2)
            add(invLook invSelect invHelp invUp invDown ok)
        )

Alright now, as the number of items grows, you might find it a little difficult to keep track of which item is represented by which number. If you read the bit above, you'll know that the test item is known as item 0. In order to make this a bit easier when referencing in other scripts, lets make an entry or two in the game.sh file. We'll go ahead and define a variable name for our item and set that equal to 0. Then in the other scripts, we can use our variable name instead of the literal number. In game.sh add the following code and save.

Code: [Select]
//***********************************************
// Inventory Items
//***********************************************
(define ITEM_TESTITEM             0)

Now in the future, whenever you add a new item, add it to this list in game.sh and then throughout the rest of the scripts, you can use the more meaningful name instead of the number.

Ok, so now it is time to draw some pictures of the test item. You'll take note that in the instance for both the cursor view and the item images we declared view 900. Hopefully you remember that the cursor defaults to the views loop 0 and cel 0. As well, for the cursor images we declared loop 1 and cel 1 so therefore the print image will be loop 1 cel 1 and the menu bar icon will be loop 2 cel 1.

The initial size of the cursor cel based off of the template is 18 x 10
The initial size of the print cel based off of the template is 21 x 22
The initial size of the menu bar icon cel based off of the template is 21 x 16

Of course, you can draw the images however you like. Here's mine...


We are getting close now. We have our item added to the game, we have the views and we have the message that is displayed when the item is looked at. All that is left to do now, is give it to the ego and actually click on something with it.

Syntax to give the ego the inventory item

Code: [Select]
(send gEgo:get(ITEM_TESTITEM))

Syntax to take the inventory item away from the ego

Code: [Select]
(send gEgo:put(ITEM_TESTITEM))

So let's actually get the item in the playable game so that you can see it in action. For now, let's take a shortcut here and give it to the ego during the titlescreen so that you can see it as soon as the game starts up. Check out the Room Interactions chapter to learn how to give the ego items as part of the actual game play. Until then, open up script 100, the initial splash screen, and go to case 0 of the rmScript changeState method and add in the syntax for giving the ego the inventory item.

Now let's create a message that is displayed when the items cursor is clicked. Go ahead and go to the initial playable room's messages. Let's add a new message. Select the noun for a room click as well as the verb we set up to represent this cursor and enter a message. If you drew a rainbow like I did, maybe something like "Pot of gold at the end of the rainbow my ass."

Now as long as everything has been saved and compiled, go ahead and run your game now, you should have the test item in your inventory when the game starts up, you should be able to view and select it, and with any luck when you click somewhere in the room with it then your newly added message will be displayed.

And so ends the initial chapter on inventory items... If you want to learn how to do more with them, then it might just be time to check out the  Advanced chapter on Inventory items.