Global window

29 views
Skip to first unread message

Hello3171

unread,
Mar 26, 2011, 8:47:33 AM3/26/11
to Grease Users
Hi,

I tried to follow the tutorial (Part 1) roughly but changed a couple
of things to make it neater. I put GameWorld into its own file but now
I am having trouble accessing the window object.

From my understanding the global keyword only makes the variable
globally accessible within a single file. I have tried a couple of
things. Note: def main() is in the file main.py and def configure() is
in the class GameWorld which is in the file worlds.py

1
--
import worlds
def main():
global window
window = pyglet.window.Window()
world = worlds.GameWorld()

def configure()
from main import window
print window #WINDOW IS NOT DEFINED ERROR

2
--

import worlds
window = 2
def main():
global window
window = pyglet.window.Window()
world = worlds.GameWorld()

def configure()
from main import window
print window #WINDOW = 2


The second approach implies that the problem with the first approach
is that the main module is imported "from scratch". I really don't
know what to do about this.

Casey Duncan

unread,
Mar 28, 2011, 12:46:00 AM3/28/11
to grease...@googlegroups.com
The problem is likely that the import is happening before main() is
executed, thus window has not been created yet. If you are refering to
non-constant members of a module, it is not usually a good idea to
import them directly (i.e., using "import from"). Instead just import
the module and refer to the members as attributes:

import main

def configure():
print main.window

Obviously you still need to ensure that the main() function gets
called before configure(), or you'll get an error.

hth,

-Casey

Hello3171

unread,
Mar 28, 2011, 1:49:58 PM3/28/11
to Grease Users
I'm not sure what you mean by "that the import is happening before
main() is executed".

The order in which relevant things are happening:

#MAIN.PY
1) window = 2
2) def main() #Definition
3) if __name__ == "__main__": main()

function main() does this:
a) global window
b) window = pyglet.window.Window(800,600)
*c) import worlds
**d) world = worlds.BaseWorld()

#WORLDS.PY
*@2)c)
i) import run_game
ii) class GameWorld(grease.world.World) #Class Definition

**@2)d)
ii) def configure(self): #GameWorld.configure
print run_game.window #Outputs '2' instead of '<window
object>'

That is the exact order of things as far as I can work out. The import
is happening after main is executed - I believe.

Also the original two blocks of code were just a couple of variations
of everything I tried. I hadn't originally used "import ... from" but
I was running out of ideas quite badly. The only explanation that I
can come up with is still my first one but that wasn't the way that I
thought import worked!

Casey Duncan

unread,
Mar 28, 2011, 4:45:45 PM3/28/11
to grease...@googlegroups.com
Actually we are running into a subtly different problem here. When you
run the main.py file as a script, it creates an implicit module for it
called __main__, but that is not the same module that is created by
doing "import main" from somewhere else. When you import main from
worlds.py, it runs the code in main.py all over again, except this
time the name of it is not "__main__" (it is "main", but the
similarity is only a coincidence due to your file name choice), so the
if block at the bottom does not execute.

The solution is that you should not actually run the main.py module as
a script. Instead create a separate script called "run_game.py" (it
can be called anything, but that is commonplace). That script will
contain only the following:

import main
main.main()

I would probably rename the main module to "game" or something to make
it less confusing, but that's optional. You can then remove the if
__main__ conditional from the bottom of main.py entirely.

Note there is a nice python game skeleton for pyglet that uses this
pattern. It was made for pyweek, but is useful as a starter template.
I intend to add a skeleton templating system to grease so that you can
run a script to generate the modules, scripts, and basic configuration
for some different games. That will hopefully help prevent these sorts
of problems.

You can find the skeleton here, I recommend it:

http://media.pyweek.org/static/skellington-1.9.zip

-Casey

Hello3171

unread,
Aug 28, 2012, 6:34:20 AM8/28/12
to grease...@googlegroups.com
Well over a year later, but I came across this and saw that I never gave a reply! This was indeed the problem and I wanted to say thank you. I'm not entirely sure why I never replied in the first place.
Reply all
Reply to author
Forward
0 new messages