Python Scripting for the Game Engine

February 22nd, 2012 | Comments (99)

Learning Python!

In this beginner’s BGE Python tutorial you’ll learn how to use Python scripting in Blender 2.6 to make a car move, increase in speed, and stop. This tutorial will teach you the basics of Python scripting for the Blender game engine, including accessing and changing logic brick information through scripting. Before getting started, if you’re new to Python and for more general information on Python including formatting, statements, functions, blah blah, check out Beginner’s Guide To Python. None of the guides here take long to go through, and you’ll learn everything you need to know to get started in a day. The rest is learning through experience and necessity through your own projects. But even if you don’t know a single thing about Python, this tutorial is easy to follow.

Click here to download the blend file. This file is the finished product and is not necessary to download in order to do the tutorial. All models and textures packed in the blend file are yours to do with what you will.

Setting things up

I have 3 windows set up in Blender, top left is the 3d View, top right is the Text Editor, and spanning the bottom is Game Logic. Your blender should look the same for this tutorial. Once you have these windows in view we can start creating our little game. You should already have a basic understanding of how logic bricks work before reading this tutorial on using python, but it’s simple enough to follow along either way. The logic bricks are in the Game Logic window, with sensors on the left, controllers in the middle, and actuators on the right. Sensors act as triggers so that when something happens such as a key being pressed or a property value changes, an action can be performed. Controllers give you a set of options that determine how sensors are interpreted or used. Actuators make things happen when certain parameters are met.

Creating a new Python script

In the Text Editor, create a new text file by pressing the “New” button in the header, and rename it to “cubeMove”. Before we write anything we want to check out some of our visual options. There are 3 icons next to the script name field where you just renamed your script. The first is line numbers. Click on this to enable it. This simply shows you numbers next to every line so you know what line of the script you’re typing on. This is essential for debugging because when there’s an error in your script, the System Console will tell you what line number the error is on. The other two icons are for word wrap, and syntax highlighting(which highlights python key words and such, I would enable this). One last thing we need to enable is the System Console. In older versions of Blender this was visible by default, but it’s not anymore. This is where all the script data and script errors will print out. To enable this in Windows, go into the top Help menu and select “Toggle System Console” if you don’t already have that window visible. For instructions on opening the console on Linux and Mac, click here.

Now for some actual scripting. Copy and paste the code below into your new script file:

print (“hello”)

print () is a simple command that prints out whatever is in the parenthesis into the console, in this case it prints the word hello. For the script to actually run and do that, we’re going to have logic bricks run it. So select an object in your scene(the default cube will do just fine if you didn’t make a super awesome car like me) and in the Game Logic window add an always sensor and a python controller. Connect these two by clicking on the little circle next to the sensor and dragging it to the little circle next to the controller. In the script field of the python controller, type in or select your script cubeMove.

Now hover the mouse cursor over the 3D View and press P. This starts the game. Press Esc to end the game. Check the console and you’ll see the word hello. Your first successful script! **Make a note that if you were hovered over the script window and pressed P, you would’ve written P somewhere in the script and it would’ve caused an error. Whatever window the mouse is hovered over is the active window. This mistake will happen a lot so keep it in mind!**

Getting serious now

Now erase that print line. Paste the following line at the top of your script:

import bge

This line will be at the top of pretty much all of your game scripts. The bge module contains all of the Python functions for realtime Blender, so it’s necessary to import this module so we can access all of its functions to use in our scripts. Add in another line below that, which makes better use of the print command, so that your script looks like this:

import bge
print (dir(bge))

Start the game(press p while mouse is hovered over 3D View) then look at your console. dir() = directory if you haven’t made the connection. The console will print out all the functions in the bge module, most noteably for this tutorial, logic, which contains the functions we can use to access our logic bricks and object properties. So let’s print out the directory of bge.logic and see what our options are there. Make your script look like below:

import bge
print (dir(bge.logic))

Start the game then look at your console. The console will print out all the functions in the bge.logic module.

So, you should know that not only can you print out the dir of bge, but the dir of all of its functions as well such as bge.logic and all its functions too. Find the getCurrentController function listed in the console. This accesses the logic brick controller that runs the script. Once we access the controller we can access all of the sensors and actuators connected to it, and even access information about the object that owns the controller. This is a pretty damn important function. Now change the print line of your script to print out the dir of bge.logic.getCurrentController() now, making sure it has its own set of parenthesis at the end, so that the script looks like this:

import bge
print (dir(bge.logic.getCurrentController()))

Start the game and take a look at the console. You’ll see a new set of functions, including actuators, sensors, and owner, all of which have their own directories which can be printed out too. All the information you need can be found using dir(). If you’re ever unaware of what your options are when working with objects or logic bricks in Python, you can simply print out their directory which tells you. Make note of the capitalization, and make sure all parenthesis are closed or you’ll get errors.

Scripting for reals

So you know about the print command, printing directories, and you know about import bge. Let’s move on to some real game scripting. Erase the print line and add two lines so that your script looks like this:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

Take a look at these three lines. They’ll pretty much be at the top of every one of your game scripts and are probably the three most essential lines of code for any blender game script.

I’ll start by explaining the second line, cont = bge.logic.getCurrentController(). I told you before what the getCurrentController() function was all about. Well here we just added a line that accesses the controller and assigns that information to the variable cont. The variable name can be anything you like, but it’s typically an abbreviation like cont, and we use it simply so we don’t have to write out bge.logic.getCurrentController() every time we need it. We just write cont instead. Make sure you put the set of parenthesis at the end. You’ll get an error if you don’t.

Now onto the third line, own = cont.owner. Here we access the OWNer of the CONTroller and assign that information to a variable. Now we have access to the object that owns the python controller that runs the script, in this case your default cube(or my awesome car). This gives us access to info about the object, most notably its game properties.

Adding more logic bricks

Delete the always sensor and add two keyboard sensors, one for the up key, and one for the down key. Rename these sensors to “up” and “down”. The name matters because we’ll be calling these sensors by name in the python script. For the “up” sensor, also enable the “Tap” option so that this sensor only registers once when you press the key, else it also registers a second time when the key is released. Tap ensures a sensor only fires one time. Connect both of these sensors to the python controller, so that the python script runs when either of those buttons are pressed. Now add a motion actuator too and connect it to the controller. Rename the actuator to “move”. We’re going to use this actuator to move our car when the up button is pressed.

Now we’re going to add four more lines underneath own = cont.owner so we can access our new sensors and actuators in the script, and we’re creating another variable for speed. So make your script look like this by adding the bottom four lines:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
move = cont.actuators["move"]
pressup = cont.sensors["up"]
pressdown = cont.sensors["down"]
speed = move.dLoc[1]

Take a look at move = cont.actuators["move"]. This accesses the actuator named “move” from the list of actuators. If nothing was defined within the brackets(ex: cont.actuators[]) then all actuators connected to the python controller would be put into this list. But in this instance, we’re just calling the one named “move”.

Now look at the lines for pressup and pressdown. These access the sensors “up” and “down” so we can detect which keys are pressed and make the script do something different for each event.

Now check out the last line, speed = move.dLoc[1]. Here we dive into the move actuator. The move actuator is a motion actuator and has different fields we can access and change with python. In this instance we are accessing the dLoc value because that’s how we’re moving our vehicle. We want to move the vehicle on the Y axis so that’s why I’m calling the second value of the dLoc field. If you’re wondering why I typed [1] to call the second value in the list, make a note that list items start from 0, so calling move.dLoc[1] is calling the second item in that list. If I wanted to get the first value, the X value, I would write it as move.dLoc[0]. So in short, with this line of code our variable speed will now equal whatever the Y value is at the time the script is run.

Making stuff happen

Now that we’ve declared all of our variables we’re going to add a couple statements to determine if up or down is pressed, and then have both keys trigger different things. In brief, this is what we want to happen. Whenever the player presses up, 0.05 is added to the Y value in the motion actuator, increasing the speed more with each key press. Whenever the player presses down, the Y value is reset to 0, making the car stop.

We’ll start by defining what happens when we press up. So add in the lines beneath the speed variable so that your script looks like this:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
move = cont.actuators["move"]
pressup = cont.sensors["up"]
pressdown = cont.sensors["down"]
speed = move.dLoc[1]

if pressup.positive:
    speed = speed + 0.05
    move.dLoc = [0.0, speed, 0.0]
    cont.activate(move)

This if statement is used to detect whether or not the up button has been pressed. Make sure to keep things formatted just like I’ve formatted it. Your if statement needs a colon at the end of it, and any action part of this statement needs to be tabbed in beneath it. Now if the player presses up, 0.05 is added to whatever the speed value currently is, and this speed value is plugged into the move actuator. dLoc is the field in the motion actuator we want to change. If you want to change other fields in the actuator, to figure out what options are available you can use print dir(move). So now we simply plug the speed variable in as the Y value in the list there and it will change the value in the actuator. To make this actuator active now, we use the line cont.activate(move). Now our car moves faster.

Now we’re going to define what happens when we press down. So add in the lines at the bottom so your script looks like this:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
move = cont.actuators["move"]
pressup = cont.sensors["up"]
pressdown = cont.sensors["down"]
speed = move.dLoc[1]

if pressup.positive:
    speed = speed + 0.05
    move.dLoc = [0.0, speed, 0.0]
    cont.activate(move)

elif pressdown.positive:
    speed = 0
    cont.deactivate(move)
    move.dLoc = [0.0, 0.0, 0.0]

The statement elif is like saying “else if”. So if the player is pressing down instead of up, we set the speed value to 0, deactivate the move actuator which stops the car, and finally we reset the Y value in the dLoc field back to 0.

Play the game!

Your script is complete! Start the game! Now press up repeatedly to gain speed, and press the down key to stop.

We didn’t actually use the own variable in this tutorial. But as a quick example, if you created a property for your object called “health”, you would be able to read or change this value in Python by calling it like this: own["health"].

How to debug your scripts

There may be errors in your script that prevent if from running properly. These errors appear in the system console. So if you go to play this game and nothing is working, check the system console to see what the error might be. It will even tell you the line number of the error encountered. With line numbers enabled in the Text Editor you’ll be able to find the error with no problem.

-Chris (blengine)

99 Responses to “Python Scripting for the Game Engine”

  1. this is awesome, thanks

    • hiii…
      Very nice tutorial.Can u tell me how can i move my man character with the help of script like ur game.
      can u tell me the script or any tutorial based on scripting(which can move my character)
      Thanks. :)

    • hi..
      i ma using this script on my character which have armature bt it doesnt work.
      i had add an action actuators ant also add a walk action on the script.can u tel the script…?????????

  2. Aniruddha Hardikar

    Thank you so much!! I was looking for a starting point for BGE scripting, and this seems to be perfect!

  3. Wow! Even the code ‘reads like a book’. Thanks so much.

  4. thanks . i want the car should move reverse .. plz help me

  5. Wow very informative and well explained!

  6. Thanks for this great introduction !! Can’t wait for more !!!!!!!

  7. Cool intro, it’s making us wanting for more!

    Sometimes I think it is a good idea also to show “what if” you were to do this manually.

  8. why is it that even if i set the dLoc to 0 or 2, it still moves it on the y axis?

    • If you’re still changing the Y value in the dLoc field its going to keep moving on the Y axis. If you set it to 0 though it should have stopped. If you’re still having trouble you could paste your script so I could take a look.

      • import GameLogic
        cont = GameLogic.getCurrentController()
        own = cont.owner
        move = cont.actuators["move"]
        pressup = cont.sensors["up"]
        pressdown = cont.sensors["down"]
        speed = move.dLoc[0]

        if pressup.positive:
        speed = speed + 0.05
        move.dLoc = [0.0, speed, 0.0]
        cont.activate(move)

        elif pressdown.positive:
        speed = 0
        cont.deactivate(move)
        move.dLoc = [0.0, 0.0, 0.0]

        Here it is.

        Thanks for helping.

      • Ah, when you plugged speed into the dLoc value you still have it as the Y value.. You need to change that line to move.dLoc = [speed, 0.0, 0.0], so that speed is plugged in first(for the x value).

    • I think it has to do with where you put the speed variable in your move.dLoc line ie;

      if pressup.positive
      speed=speed +0.05
      move.dLoc = [0.0, speed, 0.0]
      if you see this as move.dLoc = [ X, Y, Z, ] then you will realize that is where you would change to directional component.
      To move the cube along the X axis the code would look something like move.dLoc = [speed, 0.0, 0.0]

      make sense?

      i think.

  9. Oh I get it thanks a lot man your the best.

  10. Just to add my thanks for a great tutorial. I hope you move on soon to more complex things.

    Any reference material on GameLogic module would be a really useful addition.

  11. Great Tutorial. One Question: Is there a way that my cube gains speed while holding a key instead of tapping it?

    • At the top left of the up-key sensor there’s two buttons with triple quotes on them. The one on the left is called True Pulse Mode, and when this is enabled the sensor will trigger constantly when the up key is held in. You can even change how frequently the sensor is triggered by enabling True Pulse Mode then increasing the Frequency value next to it.

  12. I had lots of trouble with this tut. since I was using mac I ahd to open the console diferently. also gamelogic does not import. but bge does. has it been changed since this tut?

    • I’m updating the tutorial tonight to use bge.logic and describing how to find the console on a mac. Thanks for letting me know about these things. GameLogic still works for me in 2.63 on windows but bge is more current.

  13. just updated the tutorial using bge.logic and added a link for instructions on opening the console in linux and mac

  14. great tutorial, i was searching for something like this for getting started. would be great if you could make somemore great stuff like that. =)

  15. Thomas H. Culhane

    Chris, this is the most helpful and well written tutorial I’ve yet seen and I will be using it with students around the world. Thanks for also posting your awesome car and racing environment and for taking the time to answer everyone’s questions. Your hard work is much appreciated! We look forward to more of your tutorials!

    • Thanks Thomas! That’s an amazing thing to hear, I appreciate it and am really glad you found this useful. If you have any suggestions for more tutorials let me know.

  16. Chris, I’m very interesting. This is a great tutorial. But, I want to ask something. Is there a way that the cube will move when I press a key and stop when I release that key? Thank you

    • Yes sir there is a way. Delete the down-key sensor, enable true pulse on the up-key sensor, and make your script look like the script below. With true pulse enabled, the script will run as long as the up key is pressed in. And when it’s released the script will stop running and the values will be reset.

      import bge
      cont = bge.logic.getCurrentController()
      own = cont.owner
      move = cont.actuators["move"]
      pressup = cont.sensors["up"]
      speed = move.dLoc[1]

      if pressup.positive:
      speed = speed + 0.05
      move.dLoc = [0.0, speed, 0.0]
      cont.activate(move)

      else:
      speed = 0
      cont.deactivate(move)
      move.dLoc = [0.0, 0.0, 0.0]

      • make sure you tab the right lines in, the tabbed lines didn’t appear correctly in the reply

      • Thanks. That’s very helpful. I look forward for another tutorial. Great job

      • Hi Guys, I’ve tried this script, but tried to add some more so I could move my character front and backwards.

        But I did not get it to work perfectly, i’ve tried as this:

        import bge
        cont = bge.logic.getCurrentController()
        own = cont.owner
        motion = cont.actuators["move"]

        downkey = cont.sensors["down"]
        pressup = cont.sensors["up"]
        speed = motion.dLoc[0]

        if pressup.positive:
        speed = speed + 0.05
        motion.dLoc = [0.0, speed, 0.0]
        cont.activate(motion)

        if downkey.positive:
        speed = speed – 0.05
        motion.dLoc = [0.0, speed, 0.0]
        cont.activate(motion)

        else:
        speed = 0
        cont.deactivate(motion)
        motion.dLoc = [0.0, 0.0, 0.0]

        once you add the downkey sensor, the pressup stops working, it only works when you add each sensor to a different script. Which is not that bad, but it is lame of me to not make it work in one script!
        Any suggestions?

        thanks!

        ps. Ive tried with elif for the downkey also, but with no success!

      • Problem solved! Turns out i did not realize that

        I was setting two “elses” for the same movement, one for up key and one for down key and both got were active at the same time and both stoping movement!

        The solution was just remove the else for up key, turn the down key to a else if and the magic is done!

        Thanks!

        here the updated code for moving foward and backwards:
        Character will move only when keys remain pressed and stop upon key release.

        import bge
        cont = bge.logic.getCurrentController()
        own = cont.owner
        motion = cont.actuators["move"]

        downkey = cont.sensors["down"]
        pressup = cont.sensors["up"]
        speed = motion.dLoc[0]

        if pressup.positive:
        speed = speed + 0.05
        motion.dLoc = [0.0, speed, 0.0]
        cont.activate(motion)

        elif downkey.positive:
        speed = speed – 0.05
        motion.dLoc = [0.0, speed, 0.0]
        cont.activate(motion)

        else:
        speed = 0
        cont.deactivate(motion)
        motion.dLoc = [0.0, 0.0, 0.0]

  17. I typed in the code correctly, but when i hit p to start the game and i hit up arrow, nothing happens.

  18. Thanks for the wonderful explanation of exactly how this all works together! It’s just what I needed to wrap my brain around it all. I had some pieces of information from various tutorials and user manual, but none of them showed how it all works together on a basic level like you show here. Great job!

  19. What is the name for left click in the text editor? Im making a minigun and I want to hole left mouse button down to make it spool up. Is there a way?

  20. Thanks Chris!

  21. Any hardcore tutorials or examples where multiple objects getting data from server or something like that :D ?

    • That’s not a topic I can cover, but I’m looking for some python gurus to join the site and cover really advanced topics like that, so in the future we’ll definitely have tutorials like that.

  22. I both downloaded the .blend file and also created my own file using this guide though I am unable to “print(dir())” anything. In console all I get is Blender Game Engine started and finished. Nothing in between. Same thing happens if I try to print “hello” for instance, nothing shows up. I am using Blender 2.63. What am I dping wrong?

    • Are you pressing the up or down keys when you start the game? The script is only run when the sensors connected to it are triggered.

      • Yes! If I run the script through the downloadable blend it all works great! The problem is I can’t print the directory of bge, which is what I want to do. Thanks for reply!

      • Can you copy and paste the script you’re using here so I can just double check it?

      • Sure!

        import bge
        print(dir(bge))

        The only thing I get printed out in the console is “Blender Game Engine has started” and “Blender Game Engine has finished”. Though if I try print(“Hello”) for instance, that works!

      • That’s really strange, that should work. Does print (dir(bge.logic)) work for you?

      • No, nothing prints out it does not matter what I try to print wether it is print(dir(bge)) or print(dir(bge.logic)) or even print(“Hello”).

      • I have no idea what’s wrong, maybe is a python version issue. Try this:

        print “hello”

        That’s how it used to be formatted, but now print is a function. You should’ve gotten an error though if that’s the case, but this is all I can think of.

        • I was having the same problem as Mr.Z printing the word, hello. I am running 2.63a out of Desperation I typed in print (‘hello’) and that solved the issue. I hope this helps someone.

  23. Hey Chris, just wanted to thank you for this excellent beginner python bge tutorial. It is extremely helpful for a beginner python user such as myself.

    Secondly, I have the same problem as Mr Z. I can’t get the dir function to work at all. I even copied and pasted the exact 2 lines of code you used:

    import bge
    print (dir(bge))

    I’m using blender version 2.63a on a windows 7 PC and i see that you used that version as well to do this, so I’m just lost as to why it won’t work. Any help will be appreciated. Thanks

    • Also forgot to add that all i get in the Console window is “Blender Game Engine has started” and “Blender Game Engine has finished”, just as Mr Z does.

  24. I downloaded your completed file, and toggled the system console, but I still go the error: “no module named bge” I have blender v.2.62 . Also when I pressed “p” on the workspace it just switched to textured view mode. However when I presse alt+”p” on text editor it did the same thing as the run script button.

    • Try the latest version of blender and see if that gets rid of the “no module named bge” error”. This was made with 263 so maybe the python changes that were made recently happened between versions. The only reason I can think of that the p key would enable texture view is if you changed your keyboard configuration. But you can play the game from the menus by changing the “Blender Render” option to “Blender Game” in the very top header, and then going to the Game menu and selecting Start Game Engine. Check the keyboard config too to see if that hotkey was changed.

      • Thanks for replying, Chris. I haven’t changed my keyboard configurations, so its probably something I did wrong because I misunderstood something along the way. I was set at blender render and reset it to blender game mode, but that didn’t change anything noticeable. I will try downloading the newest version of Blender, do you happen to know if its out in portable yet, because I haven’t found anything new on that yet.

  25. I downloaded v.2.6.3 and got all the same results as before.

    • Ah, are you running the script from the game, or by pressing alt+p? Alt+p is not for game scripts, and it will cause that error. Game scripts must be run from within the game by a python controller. So start the game and press up or down to run the script.

  26. Wow. That worked. That was a lot simpler than I thought. Thanks. But why would it give me those errors when I pressed alt+p or “run script”? It doesn’t make sense to me. Also, when I toggle the console open, it is completely blank, with nothing written in it.

    • You can create regular scripts for blender that you run with alt+p, like if you create scripts to aid in modeling or animation. Game scripts need to be run while the game is running. From what I understand the game engine is essentially a separate part of blender, just integrated in it, so game scripts need to be run by the game engine. Is the console blank after you run the game and/or try the print () function?

  27. Thanks for explaining that to me. The console is blank before I run the game and after, even if I use the print() function. It doesn’t even list any information I suspected it would on start up about the blender or python. Entirely blank.

    • I don’t know why that’s happening without peaking over your shoulder. You could try running blender from the command prompt(start menu > run > cmd.exe, then type in the path to the blender.exe file)

  28. I gave up with the console because even that wasn’t working for me (I’m sure I just didn’t type it in right) I’m going to use this that I found instead: http://www.blendernation.com/2010/07/21/blender-python-bindings-for-notepad/

  29. Sprite, I was having the same problem for a little while. Double check just to make sure an “always” actuator is attached to your script and that the python script is pointing to the proper text file.

  30. Yup, I got the script running fine, thanks for replying James. I found out though, that the console worked on another computer with same software same running program, while it didn’t still on the other computer I was using. After a bit of research on the subject, I have also seen no one have a similar problem to mine.

  31. scripts dont look impossible anymore safe

  32. will i get game engine in blender 2.64 itself ?thanks.

  33. hi , awesome tutorial on bge and python . I wana know if you can make a tutorial like this but with a car thats fully controlable , like wheels turning and having all the behaviour of a real car . I really wana make a racing game with blender . Such a tutorial will be perfect thanks

  34. Blender is very good! But what is if I want create simple 3d game with 15 minutes? Now you can try Game Builder based on pyWinGUI and pyIrrlicht.

  35. on running:

    import bge
    print (dir(bge.logic))

    I am getting the following error-”import bge
    print (dir(bge.logic))”

    i am using blender 2.65

  36. I great tutorial. easy to understand and not a video. too many of those lately (imho)

    Now to my problem:
    I followed the tut exactly yet, it wouldn’t play. I literally cut and pasted the text of the script from the tut and got the exact same error. So, so long as your syntax is good (and I’m not doubting that it is) then my Syntax is good. My error in the console shows:
    KeyError: ‘requested item “move” does not exist’ and has a traceback to line 4 which is the declaration of the move variable. I’m kinda stumped here. got anything? thanks

    • update: The sample blend worked fine on my machine (with the exact same script.) this is leading me to believe there must be something wrong with the setup of my logic blocks but I looked them over and they are the same too,,,

      …im stumped.

  37. ok, I am officially a moron LOL. I never changed the name of the actuator from “motion” to “move”

    works fine now,

    thanks again…sorry for all the needless posts.

    • haha, no problem, glad you figured it out! And I totally agree about video tutorials. I personally don’t care for them, but most people seem to love them. I’ll be doing more text tutorials in the future though, especially when it’s data related like python tutorials.

  38. Its really helpfull……………..thanks

  39. that was a great tut, I was wondering if you could explain getting the attribute list for the edit object actuator, i need to control the linear velocity but dont know the attribute name. I really like what you are doing and hope you can help me, Thx Paul

    • Hey Paul, name your Edit Object actuator “act1″ or something like that, then connect it to a python controller and a sensor to trigger it. Then use the following script. It’s the same as I use above to get the directory for bge, only instead of printing dir(bge) I’m printing dir(act1).

      import bge
      cont = bge.logic.getCurrentController()
      act1 = cont.actuators["act1"]
      print (dir(act1))

  40. Tochukwu Bedford

    Honestly, this tutori@l w@s indeed very helpful. Now I’ve got @ b@sic underst@nding of python.

  41. i know its been out awhile but i just finished that bow and arrow tut cant believe i got it to work . nice . thank for makin it

  42. Good introduction, thank you, ready for the next !

  43. get tutorial. I can seem to get objects to show textures in the Viewport Shading selection. I am using Blender 2.68a. Can you tell me how you were able to get your objects to show textures? Thanks!

    • To enable GLSL shading for the viewport, press the N key to open the right-side toolbar in the 3d View, scroll down to the Display panel, and change the view type to GLSL. If your graphics card supports it then you’ll be able to see your materials/textures in the 3D View. Press alt+z to switch to textured view afterward.

      • Thanks so much. Another question about textures – if i change the texture on the banner to something else, it shows when rendered. But in textured mode it still shows the original checkered texture.

        • With GLSL enabled textured view should show exactly what the render shows. If you have another display type enabled like mulittexture or singletexture then the objects in the 3d view will display the images they are uvmapped to. So if the banner is still uvmapped to the checker image, that will be shown in textured view. Simply go into edit mode, select all and change the image its uvmapped to.

  44. Hello! One question, if I want to rotate my cube (around each exis) how I shoul proceed?
    Thanks?

  45. Blender bpy module can be loaded to external python IDE,
    how about bge,
    it is posible to load bge module to external editor/IDE ?

  46. I set speed = move.dLoc[0] or speed = move.dLoc[1] or speed = move.dLoc[2] car can move but I think when I set move.dLoc[0] or move.dLoc[2] car must stop
    please explain to me

    if pressup.positive:
    speed = speed + 0.05
    move.dLoc = [0.0,speed, 0.0]
    cont.activate(move)

    elif pressdown.positive:
    speed = 0
    cont.deactivate(move)
    move.dLoc = [0.0, 0.0, 0.0]

    Thank you very much… :)

  47. If there just would be flash version of that game, I’d happily post it to my site.

  48. I just go straight up instead of on the y or x axis

    .import bge
    cont = bge.logic.getCurrentController()
    own = cont.owner
    move = cont.actuators["move"]
    pressup = cont.sensors["up"]
    pressdown = cont.sensors["down"]
    speed = move.dLoc[2]

    if pressup.positive:
    speed = speed + .05
    move.dLoc = [0.0, speed, 0.0]
    cont.activate(move)

    elif pressdown.positive:
    speed = 0
    cont.deactivate(move)
    move.dLoc = [0.0, 0.0, 0.0]

    • The vaues in the dLoc field represent X, Y, and Z. Try changing the value of a different axis in that field to go in a different direction. Your car object may have a different orientation than the car object I used for this tutorial.

  49. For some reason, the Console won’t print anything out for me, not even a simple.
    print (“Hello World”)

    I have absolutely no idea what is wrong, I’ve even uninstalled and reinstalled blender to try to fix it.

    • Hey Daniel, it sounds like the script isn’t running. Did you make sure to set up the logic bricks to run the script when the game plays? If you’re still having trouble, upload your blend file for me or you can even email it to me as well at contact@cgmasters.net and I’ll check it out.

  50. Brendon McDonough

    Hello Chris; thanks for the wonderful starter kit to scripting.

    I have been able to adapt the script quite successfully to a flight control model I am developing, using dynamic control instead of static movement, using force and torque values instead of location changes but one hiccup I have encountered is the need to repeatedly press a key to continually add to the value, rather than it adding while the key is held down and returning it to a default value when released.

    what part of the script is nullifying the input until the input key is pressed again?

    will continue trying but I am sure others would like the same functionality.

    • Brendon McDonough

      I think I have figured it out.

      because the key assignment uses if statements, the script only runs once per keystroke. Not only that, if multiple keystrokes are listed, (mine has up, down, pan left, pan right), pressing multiple keys results in the action of the last key pressed, rather than a combining of the two states.

      for the time being, I will run a separate script for each input key sensor brick, rather than multiple bricks running to one script.

      if there is a way of containing it all in one script I would love to know.