Math

I can’t figure out how to do something seemingly simple in BGE python, add and subtract variables I’ve started out with this:

import Blender

variablename = 10
variablename1 = 5

How do I go from there, or am I doing it all wrong to start with?

variablename2 = variablename + variablename1

and the very helpful next command:

print variablename2

that will tell you what variablename2 is in the console window.

your mathematical operators are

  • add
  • subtract
    / divide
  • mult
    ** to power of

and the unusual ones:
+= increase by (example, n += 2 will cause n to be 2 larger than it was a moment ago)
-= decrease by
I believe *= and /= work as well, although they aren’t usually needed.

pressing alt-p with mouse over the script window will play the script immediately.

I typed this into the text editor:

variablename = 5
variablename2 = 10

variablename3 = variablename2 - variablename

print variablename3

Then i pressed alt-p, and nothing happened, blender stayed the same, why dosen’t it do anything?

you must check the console for the print statement

All print statements and errors appear in the blender console, not in blender itself. If you’re running windows, that’s the little DOS box that comes up when you run Blender. Also, make sure that the mouse is over the script window when you hit alt-p.

-Sam

I have a Mac how do I get a console box?

Something that you should always do is import everything you want… Sorry i can’t help you with your console window problem, but your script should really say


Import math
 
then all the otherstuff

To get blender to open on Mac OS X with the terminal for output you need to be slightly tricky.
Click here to view a post on how to do it.

yes. Don’t import Blender in BGE :slight_smile:

actually, as of 2.49 and later, you can do some stuff with the Blender module whiel in the game engine. In particular, realtime loading of images with Blender.Image.Load()

although in this and most cases in the blender game engine, import Blender is useless (similarly, don’t bother to import Math unless you know you’ll need it for more complex things like Math.sin or Math.pi)

oh, that is cool. I’ll have to try it out some time.

Uhm… nobody mentioned this yet, and you might know it, but to get any script to play in the game engine you need to connect a Python controller pointing to your script to some kind of sensor (always is good for tests). Set this up in the logic panel for an object.

Sorry for stating the obvious if you already know this.

As has been mentioned, you can use alt+p to run a script directly (or you can right click and select run from the menu), but if you import GameLogic in a script then you’ll need to execute the script from a game.