githubEdit

Load

Introduction

The Load method is a method which can be used to load content either from a XNB (if using content pipelines) or from raw file if FlatRedBall supports the file format. For more information on the Load method and how FlatRedBall caches content, see the FlatRedBall Content Managerarrow-up-right wiki entry.

File locations

The Load method supports either absolute or relative file names. In other words, both are valid (assuming the files exist):

Texture2D texture = FlatRedBallServices.Load<Texture2D>("redball.bmp");

--or--

Texture2D texture = FlatRedBallServices.Load<Texture2D>("C:\MyGame\bin\x86\debug\content\redball.bmp");

The Load method will prepend FileManager.RelativeDirectoryarrow-up-right if the argument file is relative. In other words, the following two are equivalent:

Texture2D texture = FlatRedBallServices.Load<Texture2D>("redball.bmp");

--and--

Texture2D texture = FlatRedBallServices.Load<Texture2D>(FileManager.RelativeDirectory + "redball.bmp");

FileManager.RelativeDirectoryarrow-up-right defaults to the project's Content directory (the location where the Content project copies/builds files if using XNA). Therefore, if redball.bmp were present at the root of the content folder, then "content\redball.bmp" would be used to load the file.

Files and Folders

If using an XNA project, most files are added to a Content project. Any file added to the Content project must be loaded with the "content/" prefix. For example, if a file "redball.bmp" is added to the root of the Content project, then the call to load it would be:

// If added as a file:
FlatRedBallServices.Load<Texture2D>("content/redball.bmp");
// If added through the content pipeline:
FlatRedBallServices.Load<Texture2D>("content/redball");

Load and Caching

The Load method supports caching and returning cached content to improve speed and reduce memory usage. To understand this, consider the following

The Load call performs the following logic:

  1. Look for a ContentManagerarrow-up-right with a name matching the contentManager argument.

    1. If no contentManager is specified, the "global" ContentManagerarrow-up-right is used.

    2. If no matching ContentManagerarrow-up-right is found, create one.

  2. Search the ContentManager for an instance that has a matching name.

    1. If the content is found, return the existing instance. This saves time loading content and reduces RAM usage.

    2. If the content is not found, attempt to load it.

For more information on how file names are stored, see the ContentManager.IsAssetLoadedByName page.

Load can return IDisposables added through AddDisposable

The Load method can give you disposables that have been added through AddDisposable. This means that you can use FlatRedBallServices as a cache for content that comes from disk as well as content created dynamically. For example, the following method would work:

Last updated

Was this helpful?