Blender Game Engine Tutorial #4; Properties - Part
Blender 3d tutorial
Hello there! Welcome back for yet another Blender Game Engine Tutorial!
This is the part where all the fun really begins, in my opinion. You see, in this part we get to learn how to:
- Open a 'Door' by using properties
- Expand a 'Hidden Bridge' by using properties
[Before you go on I suggest you read 'Blender Game Engine Tutorial #3; Properties - Part 1' as I will not go through the basics of properties in this one!]
To begin with we need our scene:
Once we have all these thing we can move on to the actual game-making stuff. In this case; The animations and the properties. We make a simple animation for the door and bridge to make them 'Open' and 'Close' when we press the button.
Once we have our scene setup it's time to make the animation for the door. Open up the 'Animation'-tab and select the door. When making our games we will visit the animation-tab quite a lot as all games have animations.. Well, at least our game(s) will.
1; Anyway, when you have the 'Animation'-tab open and the door is being selected - Make sure you're on Frame 1. Now, press ' i ' and a menu will pop up; The Keyframe menu. With this you can select which attribute(s) you'd like to key on the current frame. Since this is a door we want to key the location of the door - and nothing else. When that's done, move to another frame - In this case frame 25.
2; Move the door inwards, against the wall so that you can barely see the door-object. When that's done - press ' i ' again and key the location once more. Now we have our animation. To view the animation simply press Alt+A (and ESC to end it).
Repeat the steps 1 and 2 on the bridge as well.
This is as in-depth as I will go on animations for now. But it's also all we need as of this part of the series. So, once you're done with the animations head over to the 'Game Logic'-tab.
By now you should be familiar with the 'Game Logic'-tab as it will be the source for our games, as far as coding and such goes.
First off, let's create an 'Integer' property called; Open - on the Door Object.
Once that is done, create; 'Near' Sensor - 'And' Controller - 'Property' Actuator
Make the Near-Sensor Distance 2.000 and the Reset Distance 3.000. Also, make this react to the property Door. (We have not created this property, so just follow my lead).
The Near sensor will tell everything near a certain property that something will happen once *Object* is close enough. It will also tell the the certain property to forget the *object* if it's too far away. Perfect for radars, turrets, button etc.
Also, in the Property-Actuator, have the object 'Assign' a value to the property Open. In this case; 1.
Basically what we've made here is the open-action. Once the player is close enough to the door, the door will assign a value of 1 to the property Open. Now, to make the door open up once we're next to it we need to add; 'Keyboard' Sensor - 'Property' Sensor - 'And' Controller - 'Action' Actuator, as seen in the image below.
In the Property-sensor, make sure it says; 'Equal' - Open - 1. This will tell our door to do something, once the property of 'Open' equals 1.
For the open-key I use Enter (Also known as Return). You can use whatever you want.
The Action-actuator is where our animation comes in. You see, the animation we created is also referred to as an 'Action', which we can control by using an Action-Actuator. To make our animation work properly in this game we need to make it a 'Ping Pong' action, by selecting the drop down-menu which says play at first, and select the 'Ping Pong' option. what this does is simply make the door do the animation when you press enter - but when you press enter again the animation will be reversed, and the door will return to it's original state.
Once you've set the 'Ping Pong' option, change the start-frame to 1 and the end-frame to 25. This will tell the door to use the frames between 1 and 25 of this animation.
The Action-Actuator is a fun tool, and I suggest you play around with it by doing different animations on different objects. Once you get the hang of it you can do a lot of cool things with it; HP/Energy-bars, Platforming, Item-usage to name a few.\ What you see in the image above this might seem confusing, but in reality it's not. It's a network of connections between different objects - In this case the player and the door. Since you already know how to make the player move (Previous tutorials), I'm not going to explain it here! (Also, note that I have a jump property - that one is not necessary in this tutorial).
On your player, create two properties; Door & Bridge. These will act as our "keys" to open the door and the bridge.
On the player, create a Property actuator. While having the player selected - shift-select the door and connect the 'And'-controller at the bottom with the player's property actuator. Make sure you connect the right and-controller or else this won't work. When the connection has been made, make the player's property actuator 'Assign' the value of 1, to the property Bridge.
This will make the player's property; Bridge = 1, once you open the door.
Later on I will explain why we did it this way, just hang in there.
Now, we do the same thing with the bridge as we did with the door. However, skip the "open" property. The bridge is a tad bit different thanks to our funny way of doing this. Again, I will explain the reason at the end of this tutorial.
Once this is done, Go back to the player and add a property-sensor for the Bridge property; 'Equal - Bridge - 1'. This will tell the player that once the bridge-property has got the value of 1, the player can do *something* (in this case, open the bridge).
As you can see in the image below, there are 'Door' and 'Bridge' tabs. Don't worry, they are supposed to be there. They will tell us that these objects have been connected in our network of actions. If you're a programmer, this is what codes look like in an image-form.
Select the the bridge-object again and remove the 'and' controller, and only that.
Once the 'and'-controller has been removed, connect the 'Near'-sensor from your bridge to the 'and'-controller which is connected to the Bridge-property sensor on your player'-object, like the image shows.
When that is done, connect that 'and'-controller from your player with the Action-actuator on our bridge-object and you're done! Run your game and make sure you've done what I have told you to! If it's not working, search your logic-bricks and see if you can find the error yourself - Trial and error, the best way to learn! However, if this is too hard, you can always go through the tutorial again.
As promised I will now explain why we did this in such a funny matter. You see, when playing games you often stumble upon a puzzle. You need to do A to open B in order to get to C. What we made here is a simple puzzle. We made the door act as a button.
When we pressed it, we gained the "knowledge" to use the bridge. Like I said; 'You need to do A, to open B in order to get to C' - We opened the door "Do A", and then we were able to use the bridge "To open B" to get to our ending platform "In order to get to C".
There are easier way of opening up doors and bridges, without using them as puzzles. I just wanted to smack two flies in one shot; You learned how to open a door by using properties - but also how to make your own puzzle with it as well.
While using properties you will encounter a heap of trouble. But do not worry as game-making is all about trial and error!
With this said I hope you enjoyed this tutorial and also that you will enjoy the use of properties in your own games. As this was only a 'open & close, door puzzle' tutorial you can use these techniques with other stuff as well. Never make up your own boundaries from what you learn, because there is always more than what meats the eye when it comes to "simple" techniques and properties.
In the next part we'll go through something the Admin ,'IrascibleOne', of DarkScarab suggested; Visible HP-Bars and how they work.
Also, suggestions are more than welcome, just go to http://www.forum.darkscarab.com/viewtopic.php?f=6&t=775 and post your suggestions there! Thank you for reading! Until next time, take care and happy blending!