Scenes

Hello, I’m new here and kind of new to blender, but I am making a game where you control a ball through a certain course. Currently when you fall off the course you hit a plane which resets you to a try again screen on a new scene. That means for each level I have to create a new try again scene, which makes for a lot of scenes on a 30 level game, so is there any coding that can be done to make it where I don’t need so many scenes?

Also:
Can you mix two .blend files? If so, how?
How can you switch between two diff. cameras during game play?
How can you make a camera that loosely follows an object so when it turns the camera stays behind it?

Sorry I know this is a lot of questions, but i’m trying to get this game finished soon. Thank you.

Okay, I’ll see what I can do to answer your questions.

You didn’t give all of the information needed in your question about the scenes. Most people will not realize that each “Try Again” scene only links to one level (for example, the “try again” button in “TryAgain1” only restarts level 1, and not level 2 or 3 or etc.).

To answer your question, anyway, you need to find a way of letting the “Try Again” scene know which level the player is on, then dynamically change the scene pointed to with the “Scene >> Add Scene” or whichever Actuator your using. The easiest way to do this is with Python. I’m going to assume that you’ve never used a Python script with in the Blender Game Engine before (though I know that may not be the case), so I will explain all of the steps.

First, you need to set a global variable to keep track of what level the player is currently on. Global vairiables are values that are accessible to any object from any point in the game. They are created by assigning variables to the GameLogic module with a Python script. I like to put the Logic Bricks to trigger this sort of script on the main camera, but you can put it where ever you like.

Select the object where you’ll be putting the logic, and press F4 over the buttons window to bring up the Logic buttons panel. Click the “Add Property” button and add a new “String” type property named “levelName”. Type the name of the scene that the level is in into the value field (the scene name is case sensitive, so make sure you type it exactly as it appears in the scene selection menu). Now add the following logic bricks:

Always --> Python

The script does not need to run every frame (which is what an Always sensor does by default), so turn off the “TRUE level triggering (pulse mode)” button. This will stop the pulsing behavior of the Always, causing it to run once and never again (I call this a “Once” sensor, and I usually name a sensor like that “Initialize,” since it is good for triggering all of the settings that must be initialized when a game or level begins). Now all that’s left is to copy the value of the object’s “levelName” property to the global variable “GameLogic.currentLevel.” You’ll need to write a quick script to do this.

Open a Text Editor window in Blender. (If you don’t know about Blender’s window system, I highly suggest you read the manual, especially the sections about the window system and window types.) In the Text Editor, click the arrows between the “AB” button and the “Screen 12” drop-down list. Choose “ADD NEW” from the menu that pops up to add a new text buffer (you’ll type your Python script here). Click the new bar that will say something like “Text.001” in it to rename your script. Name it something like “SetLevel.py” Type this script in the Text Editor:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

GameLogic.currentLevel = own.levelName

That’s all the code you need to copy the value. Now go back to the object’s Logic Bricks and type the name “SetLevel.py” in the “Script:” field of the Python controller. In case you’re wondering what the script means, “GameLogic.getCurrentController()” is referring to the Python controller Logic Brick that called the script. We’re calling that “cont” in our script. “cont.getOwner()” is referring to the object that owns the Python controller, and “own.levelName” is referring to the “levelName” property assigned to that object. Variables do not need to be declared ahead of time in Python, so even if “GameLogic.currentLevel” does not yet exist, it will be created the first time we assign a value to it.

Now go to each level and add in the same Logic Bricks and the “levelName” property, set to the correct scene. The only task remaining is to set the “TryAgain” scene to read the “GameLogic.currentLevel” variable and set the scene accordingly.

There are a few different ways to go about making the scene transitions in this case, but I’m going to assume that when the player fails to accomplish the level’s goal, you use “Scene >> Add Overlay” to add the “TryAgain” scene, and then you use “Scene >> Remove” to remove the level. Also, I will assume that you are using a plane with the text “Try Again” as a button that triggers the level’s reloading when it is clicked. Based on those assumptions, set up the Logic on the button like this:

Mouse: Left Click —
Mouse: Over ---------------> Python: TryAgain.py -----> Scene: Add Overlay
----- Scene: Remove

Name the two Mouse Sensors “LeftClick” and “OverMe” respectively, and name the Scene Actuators “AddLevel” and “RemoveMe”. You won’t be able to type a Script value into the Python controller until you’ve created a new text buffer with the same name. The “TryAgain.py” script should look like this:

cont = GameLogic.getCurrentController()
leftClick = cont.getSensor("LeftClick")
mouseOver = cont.getSensor("OverMe")
levelAdder = cont.getActuator("AddLevel")
removeMe = cont.getActuator("RemoveMe")

if leftClick.isPositive() and mouseOver.isPositive():
     levelAdder.setScene(GameLogic.currentLevel)
     GameLogic.addActiveActuator(levelAdder, 1)
     GameLogic.addActiveActuator(removeMe, 1)

If all is well, your “TryAgain” scene should now work for restarting the current level.

A couple of Python pointers: Python is case sensitive, so “removeMe” is not the same as “ReMoVeMe” or “removeme”. Whitespace matters in Python, so make sure you indent every line that is part of an “if” block, just as I did in the script above.

(This is a long post, so instead of accidentally exceeding the max length, I’m going to post a second time with the answers to your other, much simpler questions.)

Yes and no. In a game, you can only use what is in the current .blend. You cannot call models or animations from an external .blend. However, if you made some of your models in another file, and you need to add them to your main game file, you can “append” them from another .blend using the “File >> Append or Link” option in the menu.

Also, if you would like to switch between different .blend files during the game, you can use the Game Actuator.

(Global variables do not carry over between separate .blend files, so if you need to share values between them you will either need to keep them together in the same .blend, or write to and read from an external file using Python.)

Use the “Scene >> Set Camera” Actuator. Type the object name of the camera into the field. (To see the object name of the camera, select the camera and press the “N key” over the 3D viewport window. The properites box will pop up, and the object name is listed in the field that begins with “OB:” You can also change the object name to whatever you want from here.)

Tip: While you are modeling and setting up your game, you can see the view through the main camera using the NumPad 0 key. However, if you want to see the view from another camera, you need to set it as the main camera first. To do this, select the camera and press Ctrl + NumPad 0, then press NumPad 0 to see the view through it.

Well, the easiest way would be with the Camera Actuator, but I’m not a big fan of how that works. It seems to me that it doesn’t ever follow the object quite the way I want it to. However, if your object is a rolling ball, it will be reasonably complicated to do what you want to do any other way (I have not successfully accomplished this yet).

If your object is not a rolling ball, then I can suggest a very nice system that will give you the good solid path following that the Camera Actuator lacks, but without the stiff rigidity of simple parenting.

Select the camera, then select your object. This will make the object the active object. The active object is the last object that was selected when multiple objects were selected, and it will be the object around which all operations performed on the group center. For example, when you press Ctrl + P (make parent), the active object will become the parent of all other selected objects. Press Ctrl + P to make the object the parent of the camera. If you play your game now, you will see that the camera follows the movements and rotations of the object with exact precision. Like I said, this can be a very rigid setup, so I will suggest a solution to “loosen up” the way the camera follows the object.

Select the camera, then go to the Object panel in the buttons window. You should see a large group of tracking and animation related buttons in the left hand panel. Click the “SlowPa” button. This activates “Slow Parenting”, causing the children to become “lazy” in the way they follow their parent. You have to set an offset to specify just how lazy they are, though. Enter a value in the “TimeOffset:” field. The offset is in frames, and Blender games run at 60 frames per second (unless you change that with Python)

If the parent object will be moving up and down on the Z axis, you will probably want to cause the camera to always be pointing at the parent. Do this by using the “Edit Object >> Track To” Actuator. The TrackTo Actuator is sticky (set it once and it sticks until you change it), so activate it with an Always sensor with its pulse mode turned off (the “Once” sensor I described in the last post). Make sure you turn on 3D tracking on the TrackTo Actuator.

That should help with all the questions you presented.

BTW, I forgot in the last post, but if you already know Python and you would like to know more about the Blender Game Engine specific functions, there is a complete and up-to-date listing on cthames’ website.

Alright thank you so much, a really big help (though your answer to my first question is kind of hard to understand, but whose wouldn’t be). Thank you!