Skip to content
Advertisement

Loading an object across multiple files

I’m making a game in Python, and I have an object to store all of the user’s preferences in, referred to as gameSettings.

When running functions in a seperate file (functions.py) I have to create a new local gameSettings var in every single function in the file.

JavaScript

This also means I have to run the gameSettings object through almost every function I’ve written so far.

I was wondering if there was a better way to do this, as I doubt this is very efficient. I can’t just import the gameSettings object at launch (as that would cause a circular import), but even if I could the object’s variables can be modified by the user and my guess is that changing the gameSettings object in main.py will not change its functions.py counterpart.

Advertisement

Answer

How do you see this solution? Using a shared dictionary. From my experience, I’d store that settings as a .json file and load it when you start the game.

main.py

JavaScript

func1.py

JavaScript

func2.py

JavaScript

Here’s the output of executing this code

JavaScript
Advertisement