Page 1 of 1

global declaration

Posted: Thu Apr 09, 2020 6:06 pm
by decompile
Hey,

Im having a problem with declaring a global variable across modules.

Example:

globals.py

Syntax: Select all

test_variable = None

def set_test_variable(value):
global test_variable
test_variable = value


test.py

Syntax: Select all

from listeners import OnLevelInit
from .globals import set_test_variable

@OnLevelInit
def on_level_init(map_name):
set_test_variable(123)


And when I debug/print the value of the test_variable (after on_level_init), all im getting is None.

Could you please help me?

Re: global declaration

Posted: Thu Apr 09, 2020 6:51 pm
by Ayuto
Could you please post the original test code of your test.py? The posted one cannot work, because set_test_variable is not defined in that scope (it should be globals.set_test_variable and that should work fine). My guess is that you did a from globals import * previously?

Nevertheless, I would try to avoid global variables. There are many articles that explain why:
https://www.quora.com/How-can-I-avoid-u ... ython-code

Re: global declaration

Posted: Thu Apr 09, 2020 6:56 pm
by decompile
Ayuto wrote:You could please post the original test code of your test.py? The posted one cannot work, because set_test_variable is not defined in that scope (it should be globals.set_test_variable and that should work fine). My guess is that you did a from globals import * previously?


Oh my bad, I just wrote it in forums.

I imported set_test_variable from globals by:

Syntax: Select all

from .globals import set_test_variable


Syntax: Select all

from listeners import OnLevelInit
from .globals import set_test_variable

@OnLevelInit
def on_level_init(map_name):
set_test_variable(123)



And even doing globals.set_test_variable doesnt overwrite test_variable aswell.

Re: global declaration

Posted: Thu Apr 09, 2020 6:59 pm
by Ayuto
And where do you print it?

Re: global declaration

Posted: Thu Apr 09, 2020 7:04 pm
by decompile
in test.py

Syntax: Select all

from events import Event
from .globals import test_variable

@Event('round_start')
def round_start(GameEvent):
print(test_variable)

Re: global declaration

Posted: Thu Apr 09, 2020 7:06 pm
by decompile
If I do the above check in globals.py, it prints the updated value.

Re: global declaration

Posted: Thu Apr 09, 2020 7:22 pm
by Ayuto

Syntax: Select all

from .globals import test_variable
If you do this, you have a copy of the value that will be stored in test.test_variable. But your function updates the value at globals.test_variable. Doing this will work:

Syntax: Select all

from events import Event
import globals

@Event('round_start')
def round_start(game_event):
print(globals.test_variable)
It always retrieves the current value from globals.test_variable.

Nevertheless, you should avoid writing to global variables.

Re: global declaration

Posted: Thu Apr 09, 2020 7:41 pm
by decompile
Works now, thank you.

I actually didnt know it only copies the value, instead of getting the "reloaded" one.

Re: global declaration

Posted: Thu Apr 09, 2020 7:59 pm
by Ayuto
It's exactly the same like accessing attributes of a class instance.

Syntax: Select all

class Player(object):
def __init__(self):
self.health = 100

player = Player()

# Prints 100
print(player.health)

# Store the player health
current_player_health = player.health

# Set player's health to 50
player.health = 50

# Prints 50
print(player.health)

# Print the stored health (100)
print(current_player_health)