
-----------------------------------
Zren
Fri Aug 06, 2010 4:53 am

Variable Overwriten?
-----------------------------------
Hokay. So I've got some globalish variables. There a couple of small classes that reference the globals for rendering/default ranges. The thing is, if I try to add code to modify these variables inside main() UnboundLocalError: Local variable CHUNK_W

CHUNK_X = 16
CHUNK_Y = 16
BLOCK = 8
CHUNK_W = CHUNK_X*BLOCK
CHUNK_H = CHUNK_Y*BLOCK
VIEW_W = 5
VIEW_H = 4
VIEW = (VIEW_W*CHUNK_W, VIEW_H*CHUNK_H)

def main():
    while running:
        key=pygame.key.get_pressed()
            if True in key:
                ####################################################
                if key

-----------------------------------
TheGuardian001
Fri Aug 06, 2010 6:27 am

Re: Variable Overwriten?
-----------------------------------
I could be wrong here (been a while since I've used Python), but don't you need to specify "global" if you want to modify a global variable from inside a method?

IE, instead of 
[code]
CHUNK_W = CHUNK_X*BLOCK 
[/code]
You would use
[code]
global CHUNK_W = CHUNK_X*BLOCK 
[/code]

If you don't specify global, I believe Python will create a new local variable by that name, and since you go out of scope immediately afterward, that value is lost.

I'm not sure why that would cause that specific error though, since you should still have the initial values available...
