I've been using Monogame for a awhile now and I was just wondering what is the best way to load my content? Lets say i have an intervals system that constantly creates objects on the screen, so should I load the object's sprite in the game class and put the sprite variable in the constructor or should I put the content variable in the object constructor and load the sprite from within the object?
Btw by best way I mean I try to keep the framerate and use less memory, thx in advance!
Best way for frame rate is to load it before game loop starts. But the best way for less memory is right when you need it and dispose of it after your done. Not necessarily mutually inclusive... pick your poison.
You have 3 different solutions, and each will fit a different type of game.
Load everything in LoadContent
This will cause a longer initial startup time, but after everything is loaded, you wont have to wait for anything anymore. However keep in mind that this is only suited for small games such as Tetris, Arcanoid or Chess. Games that generally doesn't have a lot of content. Keep in mind that the more content you load, the more memory it will consume.
Load everything needed for the current scene
This is what most games does, since you wont load data for scenes that you never use, and you wont load data for scenes you're not yet accessing.
Load everything needed for the current scene (Extended)
Like the previous answer, but with a small twist. If possible, load content while displaying other content. For instance, as soon as the player finished the last step for completing a scene, initiate the loading of the next scene.
Arcanoid example: As soon as the ball hits the last block (or if you're brave, even as soon as you can calculate that the ball WILL hit the final block), initiate the loading of the content for the next scene. Let this load while the ball flies towards the final block, and also while displaying the score for the current scene (Time, deaths, bonuses, etc.)
And should the player actually close the dialogues before the scene has been loaded, show another loading scene while the data finishes loading. That way, there might only be 1-2 seconds of loading time instead of 10.
Remember to perform all this loading in a background thread.
Related
I'm a student using the latest version of Microsoft XNA for learning purposes.
I need to load a whole lot of texture from the Content folder (or more specifically a folder called Tiles inside the Content folder) and I really don't want to type the following code 100 times:
texture = Content.Load<Texture2D>("texture");
I realize I could change the names of the files and run through a loop to load them, but this seems inefficient for finding specific tiles if there's a way I could keep the names. I am just looking for a way to load all of these that saves development time.
Also, the files are in .BMP form, if that matters.
Keep in mind that every texture needs to be loaded at least once.
So using
texture = Content.Load<Texture2D>("texture");
is fine to repeat for different textures, as long as you don't repeat it for the same texture.
I've learned is to load all textures in the LoadContent() of game1.cs, that method is only called once at the moment the game starts. So they're all ready to be used for the rest of the game.
There's also a Lazy Loading practice, where the texture is only loaded for when the object appears. But you should be aware that the texture loading won't repeat itself for each object.
I have a very very Big/Huge scene to load and I can't load it at once.
What I have tried so far is I divided the scene into multiple scenes and loading it into async manner as my character controller move. I am moving my character controller and accordingly loading (its near)scenes by matching its position. But it is not smooth. . .
How do I load big scene smoothly? what will be the strategy? Is this right strategy which I am doing or there is any other feasible and easy to use solution available.?
Try separate your scene into small chunks. When you move your character - just load new chunks and unload old. For example, this strategy uses minecraft.
You can't decrease loading time to zero milliseconds, but loading will be smooth. You can use chunks about 5x5x5 meters. And load from manager with coroutines.
Don't forget to use object pool pattern (because Instantiate it very costly) :)
UPD:
For example, this way use in plugins:
Uniblocks Voxel Terrain
MapMagic World Generator
Also, this plugins uses open world generation from 2d/3d noises (Perlin noise, etc.)
In my Unity game players are able to chop down trees using for example an axe. The tree then gets removed from the terrain and replaced by a falling tree prefab. This works fine in small scale. I am looking for a more effective way to do this though, because:
The entire terrain needs to be reloaded if a tree is cut down. If the terrain is very large and complex this means that the entire game freezes for a few seconds whenever a tree is cut down.
It works very poorly in multiplayer. In order for the terrain to stay in sync, all players terrain needs to be reloaded each time a tree is cut down. This means everyone's game freezes every few minutes.
I have thought about "chunking" the terrain but the entire terrain is already built and I don't know of a way to break it up into parts after it has been made.
Any ideas will be appreciated! I'm about an intermediate level programmer so I don't need anyone to write the code for me here, I just need an idea or suggestion as to the best way to do this?
I've run into a slight problem. When I restart my game (by running my own initialize method), I can successfully reload all my objects/variables. But the initial SoundEffectInstance I used continues to play while a new instance is created. This results in multiple instances of the same music being played simultaneously.
What I'm trying to achieve is a way to get rid of the first instance when making a new one.
instance.Dispose(); can't be used as it means I can't re-initialize the sound.
I tried using a list to solve this (removing instances before restarting game), but it seems I can't CreateInstance() from within the list, restricting me from initializing the sound to begin with.
Arrays don't seem to offer a solution either. Any ideas?
First off, you probably shouldn't use SoundEffectInstance for music - this results in the entire song being loaded into RAM uncompressed - which eats up a ton of space relative to streaming an MP3/WMA from disk using Music.
If you want to dispose all content then you should unload the ContentManager that has all of your content. Then you can recreate the content - it will take some time to re-load it from disk but you will have a clean set of content if that's what you're after. Keep in mind that a SoundEffectInstance contains unmanaged resources, so the only way to "remove" it is to dispose it or unload its ContentManager.
If you don't want to do that then you should be preserving references to your content and then you can manually do things like stop:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.stop.aspx
You should use mediaplayer.play() to play a song instead of SoundEffectInstance and then
you can just have a if statement that you can say if X happens then mediaplayer.play()
else
mediaplayer.stop(); or
mediaplayer.pause()
I'm creating a program that simulates that of the Breakout Game using C#.
I've been learning various techniques on how to create the bricks, paddle and ball for the game but cannot work out on how to add them all into one picture box in Visual Studio.
The main issue I'm facing is that in order to move the ball for example, I have to clear the 'canvas' by using the following section of code:
paper.Clear(Color.White); This basically clears the picture box to the colour white in order for the new coordinate (of the ball for example) to be dawn within the picture box and this is where my issue begins.
Each of the components within the Breakout game (that I have practised) all use the paper.Clear(Color.White); code. This means that if for example I want to move the paddle, display the bricks and bounce the ball simultaneously, the program just decides to do one function at a time. If I remove paper.Clear(Color.White); from one of my assets then the program just won't function in the way I want it to.
Is there a way for all these components to run simultaneously within the game without missing any of them out completely?
At its simplest you need to change your approach to have the 'layouting' or 'painting' be centrally controlled, presumably on a timer or similar, and do a single 'clear' operation and then redraw all your components. In other words, do not have each component clear the canvas, they should just be concerned with their own rendering.
The above is the simplest approach. Beyond that you could take an approach of only redrawing what has changed from one frame to another. This can make for much more optimized performance, especially if your game canvas is large or has many components. However it requires a completely different, and in some ways more complex design. You would need to determine the rectangle / rectangles that have had 'movement' or other modifications to them from the prior frame, clear only those rectangles and ask those components that are wholly or partially in those rectangles to re-draw themselves.