# How to preload a vast amount of models?

This is more a theoretical question,

during runtime, my game runs smooth without any hickups. But as standard game-objects, my game contains collectibles such as coins (which are rotating Models).

I can (again, during runtime) load 10 of those coins without getting any hickups. But as soon as I load 50 coins, the runtime freezes for some ticks and continues then.

This is how I draw a single Coin (and every of those 50 get drawn like this)

public override void Draw(RenderContext renderContext)
{
var transforms = new Matrix[_model.Bones.Count];
_model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in _model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.View = renderContext.Camera.View;
effect.Projection = renderContext.Camera.Projection;
effect.World = transforms[mesh.ParentBone.Index] * WorldMatrix;
}
mesh.Draw();
}
base.Draw(renderContext);
}


How could I draw one Model in x: 10, x: 20 and x:30 (with the other values like Y,Z or scale untouched) for instance?

• Why are you loading 50 coins? Why are you not loading one coin and reusing that one coin each of the 50 times? May 3, 2013 at 21:20
• How should I do that? I have a row of coins, I need to have 50 coins in different locations
– IMX
May 4, 2013 at 13:50
• Read up on matrices and transforms. May 4, 2013 at 13:52
• @IMX: I see some code, but I don't see why you can't render the same mesh in different places. May 7, 2013 at 13:50
• riemers.net/eng/Tutorials/XNA/Csharp/Series3/… - start with the section beginning "Imagine you want to draw 2 objects from the same mesh" (this should be a familiar page as you seem to have copy/pasted your code from it). May 7, 2013 at 13:58

Upon creation of a coin, do you load the model/texture from disk every time? This would make loading extremely slow. Creating a coin object without disk IO should generally be very, very fast. If that's not the case, try looking at object pools.

Wikipedia:

A pool in computer science is a set of initialised resources that are kept ready to use, rather than allocated and destroyed on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished with an object (or resource), it returns it to the pool, rather than destroying it.

Create a coin pool upon loading a level/the game, then use and put coins back as necessary.

Edit: For a more thorough explanation, see this post.

You don't need 50 or 10 or even 2 copies of a model. You don't need to pool them, either (though pooling the objects they represent may be worthwhile).

You need one model, and you need 50 instances of it. For each mesh in your coin model, you draw that mesh at 50 locations. For simple, repeated models, it makes much more sense than drawing each mesh for each model location.

### The bounty for more details edit:

The reason you aren't getting sufficiently detailed answers is that the solution to your problem is too extensive for a single answer. Your posted code is the out-of-the-box, tutorial method for drawing a model, and it simply doesn't work for drawing a large number of models. As a matter of expert opinion, the Model class itself isn't good for drawing a lot of models (or anything else).

So you want to draw a lot of models anyway. Fine, but you need to drastically change your code. Here's a shortened list:

Easy, right? NOPE! It's a complete overhaul; far beyond the scope of your current Draw() method. No one can give all that code in a detailed answer. But luckily for you, no one has to. You just have to download and study the example project below.