0

I am working on a game and I have a List of available characters each player chooses from, each character contains a (3D)Model and a Texture2D Icon that has been loaded in through the Content Pipeline.

The issue is, I want more than one player to select the same character, but player1Char = charDatabase[choosenChar]; is by reference, so any repeat selections would refer to the same Model.

Here's the constructor of the Character class I am trying to copy, CModel class is just a model with with position, rotation and scale values for that model.

public Character(CModel model, string name, int HP, Vector2 iconPositionOnSheet)

What I think I want to do is make a deep copy of that class, but the Model class is not serializeable. The only other way I can think of doing it is loading up the same model from its file with the Content.Load if a repeat is selected, but I am wondering if there is a way to simply copy the character class and therefore the model, because that would work easier for me.

  • Unless you are altering the geometry then dont copy the model, reference it. Assets eat up a huge amount of memory and should never be deep copied except in very limited situations. – ClassicThunder Jul 25 '13 at 18:05

1 Answers1

0

Instead of passing the Model object to the constructor, pass the ContentManager object (content) and the filename string of the model. Then in the body of the constructor, call Content.Load<Model>(filename). Then, if you want to give it an alternate color or texture, clone the basicEffect and reset it with the texture/color you want that character to have and you're set.

Now a new Model object will be allocated elsewhere on the heap (instantiated) from the other similar model so both players can have a unique object.

One of the smart things that the content manager does is reuse GPU resources if they've already been loaded. So if you load the model a second time, although it gives you that unique reference on the heap for your c#, both Model objects will use the same vertex buffers and effects so they don't have to be instantiated twice.

Steve H
  • 5,479
  • 4
  • 20
  • 26
  • Thanks Steve, that's better than what I would have done, but it's not quite what I'm looking for because at that stage I have already created all my characters, I create each character, put them into a list because they are all displayed in a character selection screen prior to playing. So, it would be easier just to do something like `player1Char = characterList[selected]` as a deep copy, but I'm not sure if such a thing is even possible with a Model. – user1693188 Jul 24 '13 at 14:36
  • I don't think position, scale, rotation belong in the Cmodel class. those attributes are unique to the player, not the model. For instance, here is an example of two players using the same model but with different textures, scale, position, & rotations. Game class: http://pastebin.com/phNbCELH and here is the player class: http://pastebin.com/FSb09tp1 Although both bill & joe use the same model reference, it gets drawn in different places in the scene with different textures and avoids taking up anymore resources than necessary. – Steve H Jul 25 '13 at 13:36
  • Ok, i didn't know that, I assumed it was like Textures, which don't work like that, I don't think. Knowing that I can handle this in a way that feels a lot cleaner now, thanks a lot! – user1693188 Jul 25 '13 at 23:59
  • In my understaning of the Load function (and my first hand experience), this doesn't work, as loading a model that has been loaded before will return a reference to the first loaded model. – Stef Nov 04 '19 at 09:41
  • @stef I don't understand what you think doesn't work. Yes, it will reference the same vertex buffer on the GPU every time that model is loaded as stated in the answer. But the effect(s) and orientation(s) can/should be unique to each new game entity that the model reference is used for. – Steve H Nov 04 '19 at 21:35