Conflicting information

I’ve asked for help and gotten some good replies (and a few not so nice - but let’s forget that).
But when I read or look at various tutorials, there is a lot of inconsistency. For example, in the current Blender release that I am using (2.74), do I need to import bge, or import bge.logic, or, as some say, it’s already imported so I don’t have to put that line in my script. Where do I find the correct answer? One VERY SIMPLE thing I wanted to try was to generate a random integer between 1 and 20, and just print it above the cube in the game. When I press “P”, the game starts but none of the methods in the tutorials works. I either get no text or an error. I’ve tried using a property and assigning it to a text object, using a message connected to a text, and a few other things. I can’t believe that something so simple would be so hard to do. I’m starting to think I’m just too stupid to use Blender anymore. It’s a shame because I love using it.

You can access everything in the bge module by calling “import bge”. It is not automatically imported for you.

Calling “import bge.logic” will raise an ImportError since bge is a module, not a python package. You probably mean “from bge import logic”. This gives you access to everything inside bge.logic, but you can just type “logic” in your script instead of having to type “bge.logic” as you would if you just used “import bge”.

If you want to get a random integer, you should use the random module.

import random

myRandomNumber = random.randint(1, 20)
print(myRandomNumber)

This code will print a random number between 1 and 20 to the console. All you need to do is setup a Python controller containing the script and connect a sensor to it. The script will be called every time the sensor is triggered.

Mobious I really appreciate your reply. But I’m already confused, because when I try to find information about the difference between a module and a python package, I couldn’t find anything. Before my first post, I had also watched or read some tutorials about the game engine, and at least one of them said that it was no longer necessary to use “import bge” because it is automatically imported, but you say it’s not. I believe you but I wonder why that one tutorial said it was automatic. And I would love to find these answers on my own, but how? For example, how did you find out that using “import bge” is necessary? I keep looking and searching, both here and in Google, etc. but all the examples, tutorials or other references just don’t agree on a lot of things. Sure I can do the basics of logic bricks and such, but anything more involved starts to be a problem, because if I get info from a tutorial, and I do what it says, it doesn’t work the same, or something unexpected happens that the tutorial doesn’t address.

The other thing I asked about in this post was how to get the random number to show (print) in the game itself, above the cube. I know how to see a number in the console, but I want it to appear in the game itself.

Thanks for your help. It’s appreciated.

Here’s an example (blender 2.74)

text_on_cube.blend

I hope the link works, it’s the first time i try that site.
There are some old examples and tutorials on the net, if they are old they probably won’t work. You can check the blender-python api documentation to see if some field or type has been removed from the game engine. You can access the documentation from blender, menu “Help”->“Python API Reference”.
Keep in mind that all software is a pain in the back to grasp at first. The more you stick with it, the easier it gets.

Don’t worry about packages right now. It’s not really necessary to know much about them unless you want to make one yourself.

It’s possible that at some point the bge module was automatically imported, but that’s not currently the case on version 2.74.

You can think of a module as just a file that contains Python code, functions, constants, and classes that you can use by importing them. When you call “import bge”, you get access to everything it contains. You can see what’s in it by reading the API. Note that it’s not “necessary” to import anything. You only need to import a module if you want to use something in it.

If you want to set visible text in the game, you’ll need to use a Text object. You can set the text it contains by setting the “Text” property on the object. Here’s an example script you can use if you attach it to a controller on the text object:

import bge
import random

controller = bge.logic.getCurrentController()
obj = controller.owner
obj['Text'] = random.randint(1, 20)

pgi & Mobious, thanks again. I will keep trying. I started writing programs in BASIC in 1980, and then I did machine code programming too. I coded a machine language graphics/drawing program that was pretty sophisticated for it’s time. (I even had a paint fill mode that could fill any 2D shape!). But I had reference books that listed each and every BASIC statement or function, with proper syntax and examples, and for the machine language code, all the codes and what each did. I can’t seem to find the same thing for the BGE. I can find a list but no real definition for each item. I would buy a book on Python, but it’s not really the same as the python scripting as used in the Blender game. I’ve seen that I would not use “import bpy” for a normal game because it’s not really the same as what Blender uses for game scripting.

The API I linked to contains information on just about everything you could use when interacting with the game engine. You can search for a specific function, or you can just browse through the different modules and classes available.

If you want to learn about other Python modules, a simple Google search will tell you just about everything you’ll need to know. The first link for the search “python random” is the documentation for the random module I used.

http://bgepython.tutorialsforblender3d.com/

Try this site, I used it a lot when learning blender Game engine. It gives good examples of each function showing exactly how to use it. It’s not updated as often as the API so some bits can become outdated but most of blender python hasn’t changed for a long time.

Thanks Monster. One thing, though, I downloaded the demo from your Healthbar tutorial, and when I play the game, the ball bounces ( seems like forever) but the text just remains as “Text”. Is it supposed to be changing every time the ball bounces?

[EDIT]

I looked at the demo for the random actuator (second link above) and that works how I expected. That’s pretty much what I want to learn. I didn’t realize that the “message” sensor could be used that way. I had written two scripts, one for my moving cube and one for a text object, but I couldn’t get them both to run. But using the logic bricks the way you did, I don’t even need to use a script. Brilliant! Thanks again.

Thanks for noticing me. I tried with 2.74 and it still works as I expect: the ball bounces 19x and the text is counting until 19 while the yellow bar grows to right.

I can’t tell why it is different on your PC. Nevertheless the principles are the same … notify the other objects about the change, that they can update themselves.

I don’t know why either. I downloaded your file again and ran it but the same thing happens. The ball bounces several times, but when it gets close to the “floor”, it just keeps moving up and down, just a little distance but it never stops. Also, the word “Text” never changes. I’m running 2.74 also. I couldn’t see any differences in the general default settings, like world gravity etc. so it’s a mystery to me. But for me a lot of Blender is a mystery. Thanks for all your help.