O'Reilly logo

Panda3D 1.6 Game Engine by Dave Mathews

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - reading data from a file

To get a look at file handling in action, we're going to replace the prepLoadGroup method of our Preloader class with a file loading operation.

  1. Open PreloaderClass_01.py. Scroll down to the prepLoadGroup method and copy this line with Ctrl+C:
    self.totalItems = len(self.models)
    
  2. Once you have that line copied, delete the entire method.
  3. Scroll back up to the __init__ method and find the line where we call self.prepLoadGroup. Highlight that line and hit Ctrl+V to paste the line we copied over it, replacing it.
  4. Right above that line we just pasted, add this code:
    file = open("models.txt", "r") self.models = file.readlines() file.close() for N in range(len(self.models)): self.models[N] = self.models[N].replace("\n", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required