ID:278401
 
#THIS TUTORIAL DOES NO SHOW PROPER CODING PRACTICE AND IS BEING REDONE.#

Thank you for your patience.

========== Basic Items & Inventory ==========

==|Includes|==
1 : Creating a Sword and Shield
2 : Creating an Inventory
3 : Creating get and drop functions.
4 : How to use it.


Welcome to another tutorial, in this one I'll teach you how to create items and an inventory for you to put them in. The items could be anything from Swords and Shields to Lazer Rifles and Plasma Pistols. This assumes you have created a basic world and understand how to make icons, compile and run. Let's get started then shall we.


# Creating a Sword and Shield #

We're going to keep this pretty simple for now. Just a sword and shield. You'll need two icons for them so lets make some. Here's what I made :




--Now remember you can always make whatever you want but you just need to remember how they relate to mine for now--

So now we have the icons, we need to make a new code file that will hold object details. Just encase you can't remember, go to "File" in the top left hand corner then "New". Now chose code file and call it "Objects". So now you should have :



Inside "Objects.dmi" write or paste this code :

obj/Equipment // Define a category of objects called Equipment. Used for later reference.
icon = 'Icons.dmi' // Where the icons can be located.
Sword
icon_state = "Sword" // Icon inside Icons.dm

Shield
icon_state = "Shield" // Icon inside Icons.dm


So here we use obj, that ones easy to remember as coming from Object. Now why we define them as objects is that they can be added to an inventory when needed.

All the rest is pretty self explanatory. Defined where the icons are, what icons we want to use from that file and of course naming the objects. Remember it's like a tree, obj is the root, the names are the branches and the information under the branches are the leaves.

Now if you compile your environment and go to your map you'll see that they are now in the object tree under "obj". Feel free to add them to the map as wished. They automatically appear above the turf don't worry.



# Creating an Inventory #

If you decide to run the game they're there but you can't do anything with them so we're going to need to create an Inventory. This is surprisingly simple. So because the inventory is for the player we'll be defining it under "mob", for those of you that followed the basic world tutorial that is found in Player.dm

This is all code to add one so paste or write this under "mob".

mob
Stat() // States we are adding a stat panel.
statpanel("Inventory",contents) // Stat panel name and list to display.


So there's a lot going on for just two lines of code. First off we're editing a process of the mob called "Stat()". This process is automatic and displays stat panels and their information.

Next, what is a stat panel. Well a stat panel will by default appear in the top right hand corner of the screen and is rectangular. Inside it can display information of many kinds including "contents".

Now what is "contents", well it is a 'list' of objects that the player has. Think of it like a bag, it can contain a list of a objects. You don't need to know a lot about it right now so I'll move on.

When we combine all this information we can add this all to a process. The process is called "statpanel()" and it will automatically do it's best to display what you put in the brackets. "statpanel" accepts two bits of information : A name in "" quotation marks and a list or variable. In this case we put "Inventory" and contents in and separated them with a , comma so it knew they were different things.

These two lines, simple yet detailed come together to form an inventory. To check out what it looks like compile and run. It will be blank.. For now..


# Creating get and drop functions. #

What would be the point of having all this if we couldn't use them together. We need a way to add those items into the players new inventory. Well here's how to do it :

mob
verb // Define a function.
Get(obj/Equipment/O in oview(1)) // Look for Equipment with 1 tile in each direction.
O.Move(usr) // Move it to the src, this automatically adds it to the inventory.

Drop(obj/Equipment/O in usr) // Look for Equipment in the contents list.
O.Move(usr.loc) // Move it do where the player is standing.


So first thing, whats this verb? Well it's used to define a function. This is something that can be used basically. Verbs will automatically appear in a stat panel so don't worry about messing around with that.

Now like how we have defined objects, I've defined a functions with their names coming before brackets. Inside the brackets are how they work. Read the comments for an explanation. It works just like the English language. You write like you would a book or manual.

So what's oview(), well it's used to search for things around the player in a 360 degree range. Hence "O". What goes in the brackets is how far away from the player can they be. Well the game is made of tiles so we speak in tiles. That's why I choose "1".

Now what's this Move() thing? Well it's exactly as it sounds. You use it to move items whether to a location or into a contents list. This is why I said usr, so it would be added to the source's contents which is a valid location.

Amazing how much is going on with such a small amount of code again isn't it. Don't worry, eventually lines like this will just flow from the keyboard.


# How to use it #

Well you'll need to compile what you have and run it. In the game you'll find the sword and shield you added but now when you right click on them while your character is close enough it will give you options.

Do the same thing to drop the item but right click on it in your inventory.

Congratulations, you now have a working inventory and items to pick up and drop! More tutorials on what to do next are coming :)

Here's mine :













obj
item
verb
Get()
Drop()

They shouldn't be mob verbs, but verbs belonging to a sub-group of obj called items.
In response to Moonlight Memento
I'm redoing the tutorial. No sweat.
In response to Kyle_ZX
You really should post this on your blog, too, in case you wished to add it to Dream Makers.
In response to Moonlight Memento
While you're right in the general sense, there's nothing actually wrong with making Get() and Drop() mob verbs per se; I could see that being useful in some games. However in pretty much 95% or more of games, making a common obj type like /obj/item for anything that can go into inventory and adding Get() and Drop() as verbs under that is the better option. For a basic tutorial that's definitely the way I'd go.

Equipment, on the other hand, can be done wrong in any number of ways, so I'm interested to see what happens there. The naive way of doing equipment, setting an obj.equipped var (or just using the suffix var), is wrong in every setting.

Lummox JR