I want to further discuss some of the points raised in is there another way to access render attribute in sub object but it seemed better to make a new topic than to hijack that thread.
In summary:
What, specifically, are the downsides of globals? Why are they bad?
How do we avoid those pitfalls?
Longer:
I want to make sure I understand what makes globals bad and how to structure code so I dont have to rely on them. Especially when Panda3D might move away from them in the future.
The service locator pattern is a good read. My current project uses an âassetBookâ, which is similar to the pattern. Itâs much like a phonebook, but for asset file names. That way all the filepaths are in one place and easy to change. If I need to change a texture thatâs used by several classes, I just change itâs address in the assetBook and tadaa. So at first glance, it looks like I am on the right track.
But if you google âwhy globals are badâ, itâs things like âanyone can change it from anywhereâ, âcomplicated coupling and dependenciesâ. Thatâs still true for my assetBook, itâs quite convenient to use, but I havent really avoided any of the globals pitfalls.
Anything that can access the assetBook can fiddle with it. I need to make changes to how the access to the assetBook is managed. The service locator pattern page explains that. I can make those changes.
The code still needs references to the assetBook in lots of places, so thereâs coupling and dependencies. Thaumaturge his code examples show how to better manage those. I am storing and accessing the assetBook class wrong.
So thatâs kinda my concern. At a glance, I could feel like Iâm cleverly avoiding globals, but when i take a step back and think about it, I havent. I just created the same downsides as globals, but I took a different route to getting to those downsides. My assetBook class isnt a global, but that doesnt mean much if it has all the same downsides.
I can improve my assetBook code, thatâs not the issue. The issue is that there is a difference between avoiding globals and avoiding the downsides of globals.
(I do have that book by Nystrom as well, but when I read it my skills werent good enough yet to fully understand it all, I should reread it)