The console gives me this error
“Python module can’t be imported - controller…”
but in my code i just have
import bge
And, as expected, every keyword of this module isn’t recognized…
The strange thing is that if i load another blend file, with many scripts that import bge, everything works fine…
So, where am i wrong?
In the working scripts i start with the same line…
(all the scripts are in the same directory, and all the .blends too…)
I’m guessing you’re running the script. Put the text file in a Python controller, and attach that to a sensor, like an Always sensor, for example. The ‘bge’ module is an internal module that is present when the game runs, so you can’t (and aren’t supposed to) run it like an external module.
Quite a noob problem the one you suggested
No, it’s a module controller, connected to 5 keyboard sensors, that runs a module.py in a diroctory. In fact, if i just press p, and i don’t activate any of the sensors connected with the controller, the console doesn’t raise any error(because the module is not run).
The problem is here: the “structure” is equal to the one of the other blend, but this one doesn’t work…
i like to place a doc string in front of all my modules. Sometimes I get similar problems as you have, that Python says it can’t find something. Placing imports at the first line usually helps.
in your case I would try to import something else, like GameLogic, Rasterizer (yes they are still present ;)) , mathutils or os. You do not need to use it, but you can see if the import is possible or if this is a problem to bge.
Eidit: You could post a demo blend, so we could try as well.
import bge
def movement():
g = bge.logic
s = g.getCurrentScene()
c = g.getCurrentController()
o = c.owner
down = o.sensors["up"]
left = o.sensors["left"]
right = o.sensors["right"]
up = o.sensors["down"]
select = o.sensors["Q"]
if o["stato"]>0 and left.positive:
#add movement of the selector
#note: study how to use matrix rotation, and apply that to worldRotation and localRotation(maybe with matrix multiplication
o["stato"]=o["stato"]-1
elif o["stato"]<3 and right.positive:
#add movement of the selector
o["stato"]=o["stato"]+1
if select.positive:
if o["stato"]==3:
g.endGame()
if o["stato"]==2:
bge.NewSceneInvoker = s
s.suspend()
g.addScene("Options",1)
if o["stato"]==1:
g.addScene("Generale") #scena vuota dalla quale si carica tutto con LibLoad()
#load game
pass
if o["stato"]==0:
g.addScene("Generale")
#start new game
pass
def optionmovement():
g = bge.logic
s = g.getCurrentScene()
c = g.getCurrentController()
o = c.owner
down = o.sensors["up"]
left = o.sensors["left"]
right = o.sensors["right"]
up = o.sensors["down"]
select = o.sensors["Q"]
exit = o.sensors["W"]
tick = s.objects["tick"]
width = s.objects["width"]
height = s.objects["height"]
if not hasattr(bge,"screenresolution"):
bge.screenresolution=[1,1366,768]
if o["stato"]>0 and up.positive:
o.wolrdPosition[1]+=0.500
o["stato"]=o["stato"]-1
elif o["stato"]<2 and down.positive:
o.worldPosition[1]-=0.500
o["stato"]=o["stato"]+1
if exit.positive:
bge.NewSceneInvoker.resume()
s.end()
if select.positive:
if o["stato"]==2:
#write the changes to the rungame file
###
#add control for os, and do something similar for unix
###
with open("Run Game.bat", mode="w", endoding="utf-8") as f_w:
f_w.write("blenderplayer.exe ")
if bge.screenresolution[0]==1:
f_w.write("-f ")
else:
f_w.write("-w ")
f_w.write(str(bge.screenresolution[1]))
f_w.write(str(bge.screenresolution[2]))
f_w.write("Game.blend")
bge.NewSceneInvoker.resume()
s.end()
if o["stato"]==0:
if bge.screenresolution[0]==1:
bge.screenresolution[0]=0
tick.setVisible(0)
else:
bge.screenresolution[0]=1
tick.setVisible(1)
if o["stato"]==1:
resolutions=[[640,480],[800,600],[1024,600],[1024,768],[1280,720],[1280,768],[1360,768],[1366,768],[1440,900],[1600,900],[1600,1200],[1920,1080]]
#set the resolution || in bge.screenresolution, alla quale si accede quando si scrive il nuovo file
if left.positive:
o["resolution"]+= 1
elif right.positive:
o["resolution"]-= 1
if o["resolution"]>11:
o["resolution"]=0
elif o["resolution"]<0:
o["resolution"]=11
bge.screenresolution[1]=resolutions[o["stato"]][0]
bge.screenresolution[2]=resolutions[o["stat"]][1]
widht["Text"]=bge.screenresolution[1]
height["Text"]=bge.screenresolution[2]
And put in that directory an init.py too. (i can’t upload rar,zip,7z files…)
I tryed loading other module, but i didn’t managed to… (tryed with GameLogic and os)
I tried with 2.54 and it seems to be fine, except that the above code is a bit misaligned after cut&paste.
That means… it does not help you :(.
If you want to transfer python code within a blend file, you could use following method. You could have a copy of the file internal. The recipient could (but don’t have to) place it on disc.
I tried at home. Yes, 2.55 acts really weired it does not even find the module Start_Module within the folder. But another modeule “test”. But this is not executed.
Should i open a new bug in the traker?
Damn, it’s the second in a week, and till the last week i haven’t found any… Unlucky development for me, 2 more steps to stability for blender
How can i use rotations inside the bge? I know i must use matrices, but how can rotate along the z axes by X degrees?
Or how can i set the local rotation of a parented object to a determinate rotation?