This is a thread for all of us to share our development experiences with Panda; tips, tricks, and maybe a short story or two. I guess I’ll go first
One thing I notice often in the examples in the Panda manual is the use of direct.directbase.DirectStart. While convenient for small examples, like those in the manual, I recommend staying away from it except for use in one-file, proof-of-concept scripts, for quite a few reasons:
- Code completion in several IDEs mess up when it’s used; often starting up a Panda window
- It breaks the “explicit is better than implicit” rule in Python: It runs code when it is imported - not normal, and implicit
- It is hard to understand for someone not familiar with Panda; when you’re introducing new developers to your project and showing them around your codebase, this can be a problem.
All of the problems associated with direct.directbase.DirectStart can be avoided by use of ShowBase, like so:
# Instead of this: import direct.directbase.DirectStart # Use this: import direct.showbase window = direct.showbase.ShowBase() # The ShowBase instance replaces global "base" # window.render replaces global "render" # window.cam replaces global "cam" # window.render2d replaces global "render2d" # window.loader replaces global "loader" # And so on...
Next, pass your instance of ShowBase around as a parameter wherever it’s needed, completely avoiding the problems associated with usage of direct.directbase.DirectStart.
I know, I know, most (if not all) experienced Panda developers already know this trick, but I see “Is there any alternative to direct.directbase.DirectStart?” asked over and over on the forums and the #panda3d IRC channel, so I thought I’d add this as one of my tips.
Unit testing your game
Unit tests are a good way to automatically make sure your code is working as intended. However, unit testing code that opens and closes a Panda window can get pretty arduous. If you have hundreds of such tests - which is not uncommon - unit testing can become quite a chore. Here are my two solutions to that problem:
- Design your game’s architecture in such a way so that business logic (i.e, things like attack damage, enemy behavior, etc…) is completely independent from the representation of said logic (like attack special effects, so your game’s logic can be tested without Panda. This is good programming practice in general, too.
- Request that Panda open up no window by adding the following line to your Config.prc:
Alternatively, you can use:
This solution is very beneficial for unit testing code that must use Panda - your special effects code, for example. You could possibly run unit tests on a dedicated build server with this solution, since no rendering device is needed.
Those are my tips - I can’t wait to see everyone else’s