Jump to content
  • Advertisement
Sign in to follow this  

Separating loading/saving functions into a separate class.

This topic is 1749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game has a few objects that load their data from a script file, mainly the level and the game objects. When running the game, I don't mind including the loading functions into the actual object itself because I don't include any way to instantiate them otherwise. In my level editor though, I want to spawn a default level, as well as editing a file where it will be stored. 

 

Here is the two ways I could do this:

[spoiler]

public class Level {
	public Level() {}
	
	public void loadLevel(ScriptObject scriptObject) {	}
	public void saveLevel() {}
	
	// Other Functions
} 

or

public class LevelLoader { // LevelFactory perhaps?
	public static Level loadLevel(ScriptObject scriptObject) {}
	public static void saveLevel(Level level, ScriptObject scriptObject) {}
}

public class Level {
	public Level() {}
	// Other Functions
} 

[/spoiler]

 

As per Code Complete 2, it seems to me that I ought to avoid classes that don't have any data to work on. Well that's fine, but adding the LevelLoader functions into Level seems to violate SRP. Also, Clean Code seems to recognize the fact that not everything should be a class, which made me realize the simple fact that LevelLoader is a class only by name really. It should really be just two separate routines. 

 

Is there any preference for any of those approaches?

 

Share this post


Link to post
Share on other sites
Advertisement
If you want real classes, define an abstract LevelFactory, with an implementation that loads a level file into a Level object and other implementations that produce fixed or randomly generated Level instances (like the mentioned editor default). Saving levels happens only in the level editor; you should be able to share code properly between loading and saving levels if you base both functions on low-level serialization and deserialization of a Level object and everything it contains.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!