Use Global Content
Last updated
Was this helpful?
Last updated
Was this helpful?
The UseGlobalContent property tells Screens or Entities to use the "Global" content manager when loading its content. This means that its content will only be loaded the first time it is accessed and it will never be unloaded.
The UseGlobalContent is not the same thing as . Global Content Files is a collection of files which by default are pre-loaded when the application starts and are never unloaded. Files which are part of Screens and Entities which UseGlobalContent will not be preloaded, but will never be unloaded even if a Screen is destroyed.
The default value for UseGlobalContent is false. Entities (by default) use the ContentManager given to them by their containing Screen. Screens by default use a content manager unique to the given Screen. This means that any time a Screen is unloaded, all content referenced by that given Screen and any contained Entities is automatically be unloaded.
The unloading of content reduces the likelihood of accumulating too much memory through content loading. However, this also means that content may be unnecessarily loaded and unloaded which increases load times.
For example, consider a game which uses the same tileset and associated PNG file for each level. These levels should probably be have UseGlobalContent set to true. Similarly, if an entity (such as Player) appears in multiple screens, the content for that entity should probably never be unloaded.
UseGlobalContent can be set in the Properties tab of a Screen or Entity.
Your Entity/Screen will no longer unload files that it uses.
Files added in "Global Content Files" will be loaded immediately when your game begins to run (or asynchronously if the LoadAsynchronously property is set to true). Files which are a part of an Entity that has UseGlobalContent to true will only be loaded when the Entity is first instantiated, or if the Entity's LoadStaticContent method is manually called.
Files added in "Global Content Files" are accessible through the GlobalContent class, which is a standard way to access global content. Files added to an Entity that has UseGlobalContent set to true will be associated specifically with that Entity. This can improve the organization of your project.
As suggested above, files added through "Global Content Files" can be loaded asynchronously. This means you can avoid long load times and delay in application startup by putting files in "Global Content Files".
Setting the UseGlobalContent tells an Entity to use the "Global" ContentManager which means it won't unload. If you are familiar with the then you are aware that you can also make files global by adding them to Global Content Files. You may be wondering what the difference between the two approaches is. Here are some characteristics which may help you identify when to use UseGlobalContent and when to use "Global Content Files".
There may be situations when a file should both be part of "Global Content Files" as well as part of an Entity which has UseGlobalContent set to true. The most common case is if a piece of content is associated with a particular Entity, but should be asynchronously loaded. For example, you may be working on a game that does not load some in-game content (such as the main character's content). If this is the case, making the main character's content exist in "Global Content Files" allows you to begin loading this content at the very beginning of execution. Depending on the menu flow of your game, you may end up loading your character before the player gets to the game Screen. This could improve load times when going into game. If you set an Entity to UseGlobalContent, and also add its files to the "Global Content Files", then the generated code for the Entity will use the GlobalContent class to get reference to the appropriate files. This means that it will obey the async loading, and even update the if appropriate.