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.
Open
PreloaderClass_01.py
. Scroll down to theprepLoadGroup
method and copy this line with Ctrl+C:self.totalItems = len(self.models)
Once you have that line copied, delete the entire method.
Scroll back up to the
__init__
method and find the line where we callself.prepLoadGroup
. Highlight that line and hit Ctrl+V to paste the line we copied over it, replacing it.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", "") print(self.models)
Resave the file as
PreloaderClass_02.py
. OpenWorldClass_01.py
, update its import to usePreloaderClass_02
, and then save it asWorldClass_02.py
. Run it and watch the command prompt for the print...