From BlenderWiki

Jump to: navigation, search
Image:LeftArrowHeader.png
[[{{{2}}}|      ]]>
Image:RightArrowHeader.png
[[{{{3}}}|      ]]>
Blender Summer of Documentation: Contents | Manual | Blender Version 2.42

[edit] Making the ball jump

We will make the ball jump when the Space key is pressed.

Add a new sensor, controller, and actuator to the scene and connect them up. Change the Always sensor to a keyboard sensor and set it up to use the Space key.

In the Force section of the Motion actuator, set the 3rd value (Z / up) to be 250. Also, click on the L to turn it off, so that the force is applied along the global direction, rather than the local Z direction.

This will give the ball quite a bit of a knock, making it look like it is jumping up in the air.

Image:BSoD-Introduction_to_the_Game_Engine-Bullet_Jump_A.gif

Press P and in the game, tap the Space bar. You will notice that the ball will jump directly up in the air. However, if you continually hold down the Space button, the ball will continually go up in the air.

We need to add in an additional rule - the ball can only jump when it is touching the ground. To do this, we will use the Touch sensor.

Add an additional sensor to the scene and change it to a Touch type. Now connect this sensor to the controller that the Space keyboard sensor is connected to.

Image:BSoD-Introduction_to_the_Game_Engine-Bullet_Jump_B.gif

The controller will only send a signal to the connected motion actuator (for the jump action) when BOTH Space is pressed AND the ball is colliding with another object, such as the ground.

[edit] Restarting the game when a goal is reached

When the ball touches a certain object, we will set it to restart the level.

  • Add a new object into the scene and place it somewhere near the main sphere (but not touching it).
  • Make sure the new object is selected (and not the main sphere).
  • Open the Game Logic panel, add a sensor, controller, and actuator and connect them together.
  • Change the type of sensor to Touch. If you have the sensor set to Always, game play will freeze when you press P.
  • Change the type of Actuator to Scene. The default setting of the Scene actuator is Restart.

Image:BSoD-Introduction_to_the_Game_Engine-Reset_Scene_A.gif

  • Now press P to play the game. It will look like nothing is happening. However, the Always sensor is actually continually triggering the Restart Scene sensor.
  • Press Esc to exit the game.
  • Change the sensor type to Keyboard and set it up to use the R key.

Image:BSoD-Introduction_to_the_Game_Engine-Reset_Scene_B.gif

  • Press P again to play the game and you will be able to move the sphere around.
  • Pressing R will restart the game and allow you to continue.
  • Press Esc to exit the game.
  • Finally, change the sensor type again to Touch.

Image:BSoD-Introduction_to_the_Game_Engine-Reset_Scene_C.gif

  • Press P again to play the game. The scene will restart now when you run into it with the sphere.
  • Press Esc to exit the game.

This illustrates some basic game engine scene management. You could have the game go to a different scene (not covered in this tutorial) that might contain a game win or game lose sequence, or the scene might be an additional level of the game.

[edit] Collecting pickups within the level

We will now add in an object that the player can collect when they move close to it.

  • Add a sphere to the scene and place it at a reasonable distance from the main sphere.
  • Add and connect a Sensor, Controller, and Actuator.
  • Change the Sensor type to Near
  • Change the Actuator type to Edit Object.
  • For Edit Object, change it from Add Object to End Object.
  • Also, activate both the Actor and the Ghost buttons. The Ghost object means that other objects (including the main player) will NOT be able to collide with the object.
NO COLLISION MAY BE A BETTER OPTION
According to Ch. 2 of gameBlender Documentation, "Ghost objects don't re-situate to collisions, but still trigger a collision sensor." If you select No Collision from the drop-down menu to the left of Actor, your game will be less laggy when objects that do not activate the "End Object" actuator pass through or rest in the object/item. This may be a better option than turning on Actor and Ghost, when you begin adjusting sensor properties.


Image:BSoD-Introduction_to_the_Game_Engine-Pickup_A.gif

  • Press P now to play the game. When you move near the object, it will now disappear (by ending itself).
  • Press Esc to return to Blender.

[edit] Counting the collected objects

When a pickup is collected, we will update a property value to reflect the total number of items that have been collected so far.

We will use the Message actuator to send a signal to another object in the scene, which will have a Message sensor. This sensor will trigger an actuator that will increase the value of a property.

  • Add another actuator to the pickup object and change its type to Message.
  • Connect this Message actuator to the existing Controller.
  • Set the subject name to be something like "pickup". It's important to remember this name, as it will be required for the Message Sensor later on.

Image:BSoD-Introduction_to_the_Game_Engine-Pickup_B.gif

When the near sensor is activated, both of these actuators will be triggered and the message will be sent to all of the objects in the scene.

We will use another object to store the pickup count information. For this task, an Empty object is useful - this is an object which exists in a scene, but has no geometry, making it invisible in the game engine.

Adding an Empty object.
  • Add an Empty object to the scene (Adding an Empty object.).


Add Property
  • When this object is selected, click on the Add Property button in the Game Logic panel (See Add Property image).
  • Change the name of the newly added object to "items" and change the type from Float to Int (Integer, or whole number, e.g. 0, 1, 2, 3). This is where we will store the number of items collected so far.


Activate Show Debug Properties
  • Finally, press the D (for "Debug") button to the right of the property. This will allow you to see the value of the property in-game. In order to see the debug properties in-game, select the Show Debug Properties menu option.


  • Now, press P to play the game and you will notice some text in the top left corner of the 3D screen, showing the value of the "items" property (currently set to 0).


  • Press Esc to return to Blender.
  • On the Empty object, add in a Sensor, Controller, and Actuator logic block sequence, and connect them together.
  • Change the Sensor type to Message and set the subject name to "pickup" (the same name that you placed in the Message sensor on the "pickup" object).
  • Change the Actuator type to Property and change Assign to Add.
  • Change the Prop name to "items" (the name of the property to add to) and set the value to 1.

Image:BSoD-Introduction to the Game Engine-Pickup D.gif

Now, press P to play the game and collect a pickup - you will notice that when you collect it, the value of the property increases. Press Esc to return to Blender.

You can use the result of this property to affect your game, such as restarting or going to a new scene when a certain number of pickups are collected.

[edit] Adding color to the levels using Materials

Up until now, the added objects have used the default gray color. In order to change the basic look of the scene, we will now change the colors of the models in the scene by creating new materials for the them.

  • Open the Materials panel by clicking on the gray sphere on the panel (Image:Manual-Part-I-Interface-Context-Shading.png) or by pressing F5, as shown below.
  • Select the main sphere model in the 3D view with a right click  RMB Image:Template-RMB.png.

Image:BSoD-Introduction to the Game Engine-Materials Panel.gif

  • Click on the Add New button. This will add a new material to the sphere model. A more complex set of panels will appear. For now, we will just change the color of the material.
  • Click on the area beside Col, which indicates the main material color.

Image:BSoD-Introduction_to_the_Game_Engine-Materials_Panel_Options.gif

  • A color picker will then appear. Use it to choose a red color, and then move the mouse cursor away from the picker. The sphere will appear red in the 3D viewport.

Image:BSoD-Introduction_to_the_Game_Engine-Materials_Panel_Options2.gif

  • Repeat this process for some of the other models in the scene until they are all different colors.

Image:BSoD-Introduction_to_the_Game_Engine-Materials_GE.gif

[edit] Making a stand-alone version of the game

Blender allows you to create a stand-alone version of your game to distribute to colleagues, without them having to have Blender installed. Your game will automatically run when the program is run.

In Blender, select File -> Save Runtime.


The image above is not up-to-date. In Blender 2.48, the option is: File -> Save Game As Runtime.
{{{2}}}


In the Save screen, enter a name for the game executable (for example ball_game). This will create a ball_game executable in that folder, which you can distribute to your friends.


NOTE
You may need to include a few other files along with that executable. Copy the file to a new folder and run it. If it gives an error that a file is missing, copy that file (probably a .dll file) into the same folder. Continue this process until the game runs. You will then be able to distribute those files to your colleagues. You may want to refer to this short document if you have dll errors: http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Making_exe .


--Mal_CanDo 17:00, 18 August 2006 (GMT)








Redirects to fix

  • BSoD/Introduction to the Game Engine → Doc:Tutorials/Game Engine/BSoD
  • BSoD/Introduction to the Game Engine/Overview of SensorsControllersActuators → Doc:Tutorials/Game Engine/BSoD/Sensors, Controllers, Actuators
  • Blender Summer of Documentation → BSoD
  • Manual/Manual → Manual