Need a little assistance with a BGE python script

So in a physics game I am working on, I need the user to be able to modify the force of an object. What I have setup so far is:

In a HUD, I have a setup of planes that when the user clicks either the plus or minus sign, the counter in the middle of the planes will go up or down accordingly. I set a real-time text plane to be modified when certain areas are clicked.

I also have a simple script that will essentially do what I need it to do, if I can link the property being modified in the HUD, to a variable that will be applied to force in the script.

from GameLogic import * 

cont = getCurrentController() 
ow = cont.getOwner 

senslist = cont.getSensors() 
actlist = cont.getActuators 

mousesensor = senslist[0] 
force = actlist[0] 

forcex = 0
forcey = a
forcez = 0 
forcel = 0

a = 

force.setForce(torqx,torqy,torqz,torql) 

I have forcey set to a variable (a) and want that variable to be linked to the property in the HUD. Im also assuming that because I have the movement of the player set to local axis instead of global, the script wont overwrite that.

Im new to blender python, so Im sure that this will be a simple answer, to a very complex question.

Thanks in advance :smiley:

supposing you’ve the a property configured in the HUD object,
insert in the header :

sc = g.getCurrentScene()
hud= sc.objects['OBhud'] # or 'hud' if using 2.5

the variable hud is the HUD object itself retrieved from the current scene. you can now read its properties :

a=hud['a']

or write in it

hud['a']=smtg

http://www.tutorialsforblender3d.com/

in a next step you might want to look at the dictionnary feature to share variable, if your project requires a lot of variables.
http://www.tutorialsforblender3d.com/Python/Reference/Python_Reference_10.html
hope this will help.

Ok, so If I am understanding you correctly, my script should now look like:

from GameLogic import * 

cont = getCurrentController() 
ow = cont.getOwner 

senslist = cont.getSensors() 
actlist = cont.getActuators 

mousesensor = senslist[0] 
force = actlist[0] 

sc = g.getCurrentScene()
hud= sc.objects['grav'] # 

forcex = 0
forcey = a
forcez = 0 
forcel = 0

a = grav['a']



force.setForce(torqx,torqy,torqz,torql) 

grav is the name of the object with the property, so I replaced ‘hud’ with ‘grav’

I tested it and it doesnt seem to be working, I probably didn’t write it in correctly…

It seems to be on the right track though, I’m glad you could understand what I was trying to ask. :smiley:


forcex = 0
forcey = a
forcez = 0 
forcel = 0

a = grav['a']

You need to set the value of a before you assign it to the forcey variable:


a = grav['a']

forcex = 0
forcey = a
forcez = 0 
forcel = 0

or


forcex = 0
 forcey = grav['a']
 forcez = 0 
 forcel = 0
 

almost :slight_smile:

hud= sc.objects['grav']
a=hud['a'] # not grav

grav is not the variable. hud is the variable representing the grav object.

and also replace

sc = g.getCurrentScene()

by

sc = GameLogic.getCurrentScene()

I wrote g because I often use an alias for the GameLogic module :

import GameLogic as <b>g</b>

Ok, so if I get this straight, it should be like this:

from GameLogic import * 

import GameLogic as g

cont = getCurrentController() 
ow = cont.getOwner 

senslist = cont.getSensors() 
actlist = cont.getActuators 

mousesensor = senslist[0] 
force = actlist[0] 

sc = g.getCurrentScene()
hud = sc.objects['grav'] 

a = hud['a']

forcex = 0
forcey = a
forcez = 0 
forcel = 0





force.setForce(forcex,forcey,forcez,forcel) 

or should
a = hud[‘a’] || be || a = hud[‘name of property that I want the force of the object to sync with’] ??

also, because this is in the game engine, I feel as though I need to have the controller (the W key) somehow be integrated, or should i just set it to run the script when the W key is pressed?


import GameLogic as g
<b>from g import *</b>

cont = getCurrentController() 
<b>ow = cont.owner # new syntax</b>
sc = g.getCurrentScene()
hud = sc.objects['grav'] 

<b>senslist = cont.sensors # new syntax 
actlist = cont.actuators # new syntax</b>
mousesensor = senslist[0] 
force = actlist[0]

# or replace the last block by
#mousesensor = cont.sensors['yoursensorname']
#force = cont.actuators['youractuatorname']

forcex = 0
<b>forcey = hud['propertyNameInGrav']</b>
forcez = 0 
forcel = 0

force.setForce(forcex,forcey,forcez,forcel)

errr for the W thing I don’t understand exactly what you mean. try first with an always sensor connected to the python controller brick (and of course connect it to the motion actuator). if propertyNameInGrav is updated something should move :wink:

http://www.tutorialsforblender3d.com/Game_Engine/Tutorials_index.html

Ok, so this is what I have:

import GameLogic as g
from g import *

cont = getCurrentController() 
ow = cont.owner # new syntax
sc = g.getCurrentScene()
hud = sc.objects['grav'] 

senslist = cont.sensors # new syntax 
actlist = cont.actuators # new syntax
mousesensor = senslist[0] 
force = actlist[0]

# or replace the last block by
#mousesensor = cont.sensors['W']
#force = cont.actuators['Forward']

forcex = 0
forcey = hud['Text']
forcez = 0 
forcel = 0

force.setForce(forcex,forcey,forcez,forcel)

I have the sensor set to always, and attached to my cube, but nothing happens… Im sure its something silly, but I cant for the life of my find out what is wrong…

Would using 2.53 (which I am) affect the syntax, what with the new API?

post a link to your blend, me or someone will point out the issue. look at the demo files meanwhile, on the tutorials for blender website & in the game engine section here.

Well I uploaded it to rapidshare.com, I hope that isn’t an issue. I thought there was a way to upload it on this site, but I guess I was wrong.

http://rapidshare.com/files/412371444/Maze.blend

dont go crazy, I dont have a premium account so it can only be downloaded 10 TIMES!

Hoep this helps!