Filepath issues

Hello, I think I’m having problems setting the path to the folders I want in my BGE project. As moguri did at his bgui sample, I put an always logic brick(with pulse mode on) connected to a .blend internal custom text file which I show below:

import bge
import os

os.chdir(bge.logic.expandPath('//'))

sys.path.append('system/code')

As I understood this code fix the root folder path to any OS’s; I also added the sys.path.append to my game code folder(//system/code)

I can access the .py code there(system/code) allright, but the problem is which my gui.py file inside that folder needs access to another folder(//system/code/bgui/themes); below is the path code line(with the path I tried):

bgui.System.__init__(self, 'bgui/themes/default.cfg')

But that path does not work, and the bgui gui shows incomplete; it seems all the path configs I made at the logic bricks aren’t recognized at my gui file(sys.path.append(system/code) and the // stuff), so I have to type the full path to the file the gui needed(“system/code/bgui/themes/default.cfg”). I tried sys.path.append(“system/code/bgui/themes”) and calling only bgui.System.init(self, ‘default.cfg’) at the gui file, but it doest not work, strangely this code(sys.path.append) works at the first script shown thought.

I wanted which the gui file behaved like the .blend file script regarding paths, where I said to the program to keep an eye at the system/code folder and I could call just “gui.manager” instead of “system/code/gui.manager”.

Thanks for any info regarding this.

Never, never, never execute your first code more than once. You need that only once.

Each time you do that you increase the size of the Python search path. Adding it constantly increases this datastructure without any use. It eats a lot of memory and can slow down the game over time.

Regarding the Path you might want to try // but I’m not sure about

The issue is which if I do not execute it with “pulse mode on”, a module which main.manager call(input.py) does not work(it needs to check the keyboard and mouse often). This is indeed a scary thing thought, as I always wanted which only the needed .py files would be checked more than once, as like you said, the result would be ugly currently.

I wanted to stay loading in BGE only my main.py module(which calls everything else), I do not want to have two always sensors to manage “updated once” and “updated always” modules.
http://img688.imageshack.us/img688/2941/capturadetela20110807s1.png

So what you suggest me to do?

import path_fix from the main module. Then it need no logic bricks at all - and Python imports the same module only once!

Hi Lah, sorry, I typed wrong before; by “first script” I mean “first module/main.py”; I think I cannot merge path_fix with main.py, because to locate main.py(in my project inside //system/code, the startup blend is at //), first I needed to fix in blender the path(sys.append and expand.path) so the main module can be opened correctly in any OS and python know it needs to keep an eye in the system/code folder files. And then comes the issue which I’m loading main.py again and again because of some modules(which need to be sensitive to changes) which main.py imports throughout main.manager.

What do you mean by not needing any logic bricks? As I understood the BGE always needs at least one sensor and one controller(and maybe one actuator too)

You are right, You need to run path_fix before the python controller can load main.py

The cleanest thing is probably to have two always sensors like You have. A always sensor not in pulse mode should really not cost any logic load once the game is started but I’m not sure how it’s implemented.

You could add a dummy function to path_fix and call it as a module. Then the code outside the dummy function is run only once even if the function is called on every tic. But it’s still a waist to call the empty function on every tic so I would not call that an improvement.

What I meant with no logic bricks is that You would not need any logic bricks for path_fix but You sure would need them for the main module… But that did not work anyway.

I think I will have to figure that one out; thanks for the help people, I will let you know if I can come with a creative solution instead of the obvious one(splitting modules into “updated always” and “updated once” files).

I was reading again my thread, and I think I have misunderstood something; monster by first script you was talking about path_fix only? I thought all the time we were talking about main.py too(where the bulk of imports and class module calls are made first). I’m very sorry LaH and Monster if that is the case; but my mistake was a good thing too, it made me think about some improvements over the main.py(which is called with pulse mode on), it was calling the input module always, but I will set it to only be called when a key or mouse button is pressed; This way I do not need to worry about splitting my main.py file into files which are constantly updated and files which are not.

But here is a last question regarding optimization(as the thread is going away from the main question), take a look at main.py code:

#import python stuff
import traceback

#import bge stuff
from bge import logic as logic
from bge import events as events

#import custom code
import updated_variables
import anim
import input	
import data

#Starts necessary loaded modules	
def manager():
	try:
		if data.change_anim == 1: anim.manager() ; data.change_anim = 0
		if logic.KX_INPUT_JUST_ACTIVATED: input.manager()
                updated_variables.manager()

	except:
       		print ("A fatal error has occured, closing Questverse.")
        	traceback.print_exc(file=open("bugs.log","w"))
        	logic.endGame()

Is everything imported again every time the always sensor acts(pulse mode on)? Or python do not import again what was already imported(read about this somewhere)?

Also if someone has any info about the path issue(path_fix already being executed without pulse mode on as monster said) in the first post, I would really like to know

Just keeping the thread active so I may be able to get an answer(or maybe not) for the two last questions in the post above.

Also for the optimization issue above, the solution was really the obvious one: a single .py for updated always variables; the others are updated only when really needed(by request of updated_variables or other much less updated modules). For example, the variables which are related to animation are started at anim module, but the update of these variables is made by updated_variables module, so the always sensor with pulse mode on is really just working effectively on the updated_variables module.

There was no need to add a new module connected to a pulse mode off always sensor and another connected to the pulse mode on sensor: even with main.py connected to an always sensor with pulse mode on I can have portions of main.py which are executed only when needed, and other which executes at every logic tic the always sensor provide. The fix_path script is the only which needs to be executed before main.py, and because that, the only which have a separated always sensor with pulse mode off.

a module is Imported only once. Python cash the import so even if we call import again it is not imported again (checking the cache still takes some time).

But as modules are imported only once also the main module is imported only once so the import statements in it are run only once. When a python controller run a function in a module only the code inside the function (or called from the function) is run - everything ‘outside’ only run once when the module are imported (and are guaranteed to be run before the function is called for the first time - because the function do not exist until the module are imported).

About path, I think sys.path only affect imports, so if bgui/themes/default.cfg is read as a file You probably need to give the full name (system/code/bgui/themes/default.cfg). I don’t know if You in python can access the path the python file is in - but probably somehow…

I was getting crazy with the path issue, after trying every sys.append and path combinations and finding which what works is only typing the full file path I think you may be right Lah. I will mark the thread as solved, but if I manage to find a solution I will let you know.