So you want an inventory for your game eh?
What this article will cover:
-how a python script can create a scrollable inventory
-how to implement the scripts for your own game
Download the example file here
|
The following is only for those who want to know how the script works. For those who just want to implement the scripts into your game, skip ahead to the MUCH smaller section at the end.
Understanding the script
How the script works:
When you pass over weapons, it appends each one into your inventory. In your inventory you can scroll up and down through your acquired objects and on the left of the screen, it says their name as you scroll. When you press enter on a weapon, it gets equipped so you are able to use it. How does it all work?
Download and have a look at the example blend file. Take a look in the script window and press the minus sign and youll see a number of different scripts. Theyre all very short and come together to make an inventory possible. Ill go through explain them now.
First, know that properties are very important. Look at all the guns and youll see they all have string property 'gun', with the name of the gun object as the value.. So the object named "machine gun" would have a string property named gun with value=machine gun. Anyway they all need to be in sync so the right gun is equipped or added to the inventory.
|
Select the script named "startup".
-What this basically does is sets beginning values to use so the python scripts dont get errors.
GameLogic.inventory = []
This line makes our inventory empty at the start of the game
GameLogic.name = "Empty"
The name variable is displayed in the inventory, telling you the
name of the object you have highlighted. Since the inventory is empty to start with, the name that appears in the display box is "Empty".
GameLogic.equip = ""
The equip variable is the weapon you are using at the moment. You start the game with no weapons so this value is blank
|
Now, select the "append" script.
-What this does is appends each weapon into the inventory when you acquire them in the level
cont=GameLogic.getCurrentController( )
This gets the controller logic brick so we can then access the object that owns that controller, and also the sensors and actuators connected to it.
own = cont.getOwner( )
This gets the object that owns the python controller
sen = cont.getSensor('col')
This line gets the sensor named "col", which is a collision sensor.
if sen.isPositive( ):
This states that if the sensor is true, do the following.
obj = sen.getHitObject( ).gun
This gets the information of the object that the main character collides with. Each gun has property "gun" with a different value, this line accesses the properties of the object so we know what gun we just collided with..
if GameLogic.inventory.count(obj) == 0:
This is to prevent doubles.. it simply states, if the object count is 0, then append the gun into the inventory. This way there will never be an accidental double of the same gun in your inventory.
GameLogic.inventory.append(obj)
So if the object count is 0, append the object name into the inventory list. Note the obj variable. Remember that equals the name of the gun we collided with
|
Now select the "addin" script.
-There are four empties in the inventory scene. Each empty loads an object so you can view it. This is the script that controls which objects to load, and where.
-Each empty has the property pos. The value of this property is the order of the empties. i put it from top to bottom, 1-4. Each empty also has property "go". When you hold up/down, the go value goes from 0 to 1. The script will only run when the property equals 0, so this will elimate the threat of having double or even triple meshes in your inventory.
-All the regular junk at the top of this script was explained above
if len(GameLogic.inventory) < own.pos and own.go == 0:
This means if the number of items in inventory list is less than that of the position of the empty, and the 'go' property is equal to 0, do the following. In short, i have one banana and two hands, one hand will hold the banana, one hand will be empty ;)
act.setObject("e")
I have an empty with the name of "e" in the overlay scene so instead of adding objects, blank spots in the inventory will add be filled be an empty
GameLogic.addActiveActuator(act,1)
This turns on the actuator and adds in the object
GameLogic.addActiveActuator(act,0)
This turns off the actuator so it doesnt keep adding(i think its necessary to turn it off, im not real sure)
elif len(GameLogic.inventory) >= own.pos and own.go == 0
Else if the number of items in the inventory is greater or equal to the position of the empty, do the following. In short, i have 2 bananas now, i can hold one in each hand =)
ind = own.pos-1
This is for the index. its the position of the object in the inventory that we want displayed by a particular empty.
name = GameLogic.inventory[ind]
This gives us the name of the object in the inventory we want displayed.
act.setObject(name)
Now we set the name in the "add object" actuator so we can add it in.
GameLogic.addActiveActuator(act,1)
This turns on the actuator and adds in the object.
GameLogic.addActiveActuator(act,0)
This turns off the actuator so it doesnt keep adding(i think its necessary to turn it off, im not real sure)
|
Now select the "info" script.
-This script gets the name from the object in the inventory so it can be displayed.
-Select the 'info gatherer' object in the inventory scene. What this simply does is it collides with the objects in the inventory and returns the property value each one has. Each gun in the inventory scene has property called "note" with the value being the name of the gun(example: note = Sub-Machine gun)..
if sen.isPositive( ):
If the object is colliding with a gun, do the following
GameLogic.name = sen.getHitObject( ).note
This gets the value of the note property from the gun it collided with. It then sets the note value as the global variable "GameLogic.name" so we can access it from other scripts.
if sen2.isPositive( ) and sen.isPositive( ):
If its colliding with a gun and user presses enter, do the following
GameLogic.equip = sen.getHitObject().gun
This gets the value of the gun property on the collided object and sets it as the global variable "GameLogic.equip". This variable will then be used in the equip script to equip the right weapon.
|
Now select the equip script =)
-This gets the global variable "GameLogic.equip" and uses it to make visible the right gun and allow it to be used.
if own.gun == GameLogic.equip:
The if name of the gun running the script matches the equipped gun name, then do the following(in short,if the property values match each other, do the following)
own.vis = 1
Set the vis property to 1. This is so you can shooot bullets or anything now.(example, if vis=1 and user presses space bar, add object halo and play gun sound)
own.setVisible(1)
This literally makes the gun visible for you to see. They all start off invisible until you equip one
else:
If the properties dont match, do the following
own.vis = 2
Make the vis property 2, so it will not be used
own.setVisible(0)
Make/or keep the gun invisible
|
Select the "name" script. this one is short short short
The text object in the inventory runs this script. It displays the name of the current highlighted object
own.Text = GameLogic.name
The Text variable will equal that of GameLogic.name(which was set in the info script)
|
And lastly, the best part of the script, the scrolling.. Open the "scroll" script.
-This allows the user to scroll up and down. Since the empties display a certain position of the objects in the list, to get them to scroll, all we have to do is rearrange the list.
This took me a couple tries to get right, but i finally saw the logic =) if youre having trouble following, after staring at it for a little, you should start to see the logic too.
The first 'if' statement removes the first entry and inserts it in the end of the list.
The second 'if' statement removes the last entry and inserts it in the beginning of the list, and vwala, scrolling.
Note that the empties that diplay the inventory objects run when up and down is pressed too. That is so they update when you scroll. And the objects are destoryed when you press up and down as well. Thats so they dont remain forever and just pile on top of each other.
Also note that the scroller object must be created AFTER all the emptys are created. It has to do with what order the scripts are executed in. so if it was created before, simply duplicate it and delete the original.
If youre still having trouble understanding, go learn more python, it does not take long.
|
|
|
Implementation:
Append all of the scripts in your game with shift+f1. Also append the whole inventory scene but you can delete my design and my gun objects. If you dont want to append the inventory scene, the important objects are the 4 "add_#" emtpies, 1 "scroller" empty, and the "name display" plane.
1. for each weapon, there are 3 copies.
a)the copy in the level you can pick up
-object name does not matter
-2 properties, gun(string) and prop(int or float)
-gun's value should be the name of the gun
-prop value = 1
-look at the logic bricks and copy them to your own weapon(spin=optional)
b)the copy the character holds
-object name does not matter
-2 properties, gun(string) and vis(int)
-gun's value should be the EXACT name of the first guns value
-vis value = 0
-look at the logic bricks. the first line of bricks is the only necessary line, the others are optional
c)the copy in the overlay inventory scene in a hidden layer
-object name DOES matter! it must be the same as the value of the above gun property
-2 properties, note(string), and gun(string)
-gun's value should be exactly like the name
-note's value should be the desired name of the gun that gets displayed
-copy the logic bricks
2. the main actor
a)just copy the last line of logic bricks(collision >> python), and property main=1
b)also make sure all the holding weapons are somehow parented to main actor
3. the camera in scene 1
a)copy the first two lines of logic bricks, the last few are just my animation bricks that you dont need
4. hmm thats pretty much it for implementing it. append scripts, append inventory scene(edit to your liking, or just append objects mentioned) and copy settings here and there.
hope this was helpful, i only had a day to scratch it up =),
chris plush |