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.
What, specifically, are the downsides of globals? Why are they bad?
How do we avoid those pitfalls?
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)