[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] setting + modifying a variable for use across the entire application



BTW guys, this is working for me. I'm using a module as an
application-level variable store. In my top-level main.py, I'm setting
variables that are referenced by modules down the line.

I had to do a couple things to make it work. One, the app-level
variables can't be already defined when imported, they have to be new
values which are set after the import by main.py. Two, any subsequent
modules which are imported have to import AFTER I'm done creating my
"app variables".

So it looks like this:

-----
main.py:

import acolyte_settings
try:
    import android
except ImportError:
    android = False
if android == True:
    acolyte_settings.ANDROID = True
    acolyte_settings.ASSETS_DIR = './assets'

import gameloop

def (main):
    gameloop.gameloop()

Now gameloop can use the acolyte_settings module to find out the
application settings. I don't have to write to an external file, or
constantly pass 'android=True' to all my classes. It's not all that
practical for multiple changes to the app-level variables, but for my
purposers, I like it!

Thanks for your help on this guys.


-

On Mon, Dec 12, 2011 at 7:00 PM, Russell Jones <russell.jones@xxxxxxxxx> wrote:
> """
> I'm not sure that works as you expect, though perhaps I've misunderstood
> what you're doing. Have a look at this and see what you think.
>
> Note file2.x stays as 1, but config.mutated changes to 2
>
> c.m reset file1
> 1
> 1
> c.m incd file3
> 1
> f1 2
> f2 2
> f3 2
> """
> #__main__
> import file1 as a
> import file2
> print(file2.x)
> import file1 as b
> print(file2.x)
> import file3 as c
> print(file2.x)
> print("f1 "+str(a.config.mutated))
> print("f2 "+str(b.config.mutated))
> print("f3 "+str(c.config.mutated))
>
> #config.py
> blah="blah"
> #file1/3
> import config
> try:
>   if(config.mutated):
>     print ("c.m incd "+ __name__)
>     config.mutated+=1
> except:
>   config.mutated=1
>   print ("c.m reset "+ __name__)
> #file2
> import config
> x=config.mutated
> #file1/3
> import config
> try:
>   if(config.mutated):
>     print ("c.m incd "+ __name__)
>     config.mutated+=1
> except:
>   config.mutated=1
>   print ("c.m reset "+ __name__)



-- 
A musician must make music, an artist must paint, a poet must write,
if he is to be ultimately at peace with himself.
- Abraham Maslow