@jocamar wrote:
Okay so I'm struggling with a problem and don't quite know what more to do. I've been building a game framework/engine that I can use for myself and it consists of the game engine itself and a separate WinForms editor application (using MonoGame.Forms) that references the engine dll that I can use to generate the data files used by the engine (scene files, entity types, materials, etc). This editor application is completely separate from the game and runs its own Game subclass. When the user selects a project folder and a scene file from the game the Editor attempts to load any assets it needs from that folder.
So far I've been sidestepping the content pipeline by loading textures and sounds via the FromStream methods. The problem is now that I'm including text I have to use the content pipeline since I haven't found any methods to load sprite fonts from streams. And I can't seem to get the spritefont to load in the editor. When I run the game project it all works correctly and the font loads and is drawn to the screen. When I try to open that project in the editor I get a crash when loading the spritefont caused by a NullReferenceException.
Here's the code I use to load assets:
public Resource GetResource<Resource>(string resourceFile, string bundle, bool reload = false) where Resource : Cv_Resource, new() { var resource = new Resource(); resource.File = resourceFile; var isOwnedByResManager = resource.VIsManuallyManaged(); Cv_ResourceData resData; if (/** file has been loaded already**/) { /** return asset in memory **/ } else { Cv_ResourceBundle resBundle; if (!m_ResourceBundles.TryGetValue(bundle, out resBundle)) { /** could not find resource bundle (a subclass of ContentManager) in the list **/ } if (isOwnedByResManager) { /** do stuff with files that don't use the content pipeline **/ } else { /** calls VLoad of the specific resource type, which loads the resource **/ if (!resource.VLoad(resourceFile, null, out size, resBundle)) { /** error loading resource **/ } } } return resource; }
And here's the Font resource VLoad method:
public bool VLoad(string resourceFile, Stream resourceStream, out int size, Cv_ResourceBundle bundle) { try { var resource = Path.GetFileNameWithoutExtension(resourceFile); /** EXCEPTION IN THIS NEXT LINE **/ var font = bundle.Load<SpriteFont>(resource); /** bundle is a subclass of ContentManager so this just loads the content as you would do normally in MonoGame **/ var resData = new Cv_SpriteFontData(); resData.Font = font; ResourceData = resData; /** Do some other stuff here **/ return true; } catch (Exception e) { /** handle the error **/ } }
In the editor I instantiate the resource bundles when I open the project folder and set their RootDirectory to a subdirectory in that folder (in this case a directory called Assets). This works well when running the game engine on the same directory as the Assets folder, but since the editor runs separately it appears to be having trouble loading the sprite font. Inside the Assets folder I have both the xnb and spritefont files generated by the content project and there appears to be no problem with these because they load correctly in the game.
Here's the error I get when I try to load a scene with text in it:
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Xna.Framework.Content.Texture2DReader.Read(ContentReader reader, Texture2D existingInstance) at Microsoft.Xna.Framework.Content.ContentTypeReader`1.Read(ContentReader input, Object existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.InnerReadObject[T](T existingInstance) at Microsoft.Xna.Framework.Content.SpriteFontReader.Read(ContentReader input, SpriteFont existingInstance) at Microsoft.Xna.Framework.Content.ContentTypeReader`1.Read(ContentReader input, Object existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.InnerReadObject[T](T existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.ReadObject[T]() at Microsoft.Xna.Framework.Content.ContentReader.ReadAsset[T]() at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) at Caravel.Core.Resource.Cv_SpriteFontResource.VLoad(String resourceFile, Stream resourceStream, Int32& size, Cv_ResourceBundle bundle) Function: VLoad
I've checked the name of the asset and the RootDirectory before calling Load and they appear to be correct. So I don't know what to do. Is the content manager unable to load files outside the directory where the app is running? Or am I missing something stupid here? Do you have any suggestions on what I should be doing to get the spritefonts to load correctly both in game and when loading scenes in the editor?
Edit: Also, I'm using MonoGame 3.5.
Thank you for reading this wall of text.
Posts: 2
Participants: 2