Setting variables mystery (Content.Load)

This is a weird one and I have never encountered this before, maybe someone with more knowledge in Monogame can help me here.

I am using Spine for the animations in my game, the object that controls these animations is called SpineAsset. I am using the pipeline to load the animation, in the image you can see that I have an array of SpineAsset (particleSpineAnimation), in this object I have a BasePosition variable. The problems comes when I try to update them individually. All the SpineAsset’s BasePosition in the array will set when only one in particular is set.

So when I do a loop to set a new BasePosition, all the SpineAssets in array will get their BasePosition value set in the last iteration of the loop since it is the last set it goes through.

My only theory, is that content.Load is using the same object under the hood in order to prevent Monogame from loading the same content ‘n’ amount of times? What are my options?

Thank you very much.

Does SpineAsset have a Clone() method? If so, use that.

Content.Load does exactly as you say, it has an internal cache. If you need multiple instances of the same asset, clone it. I do this all the time when I want copies of the same Effect with different variables or textures.

Worth noting that asset classes are normally IDisposable. Generally this isn’t important as the content manager cleans up loaded assets itself, but I think if you’ve cloned them this doesn’t happen as it won’t know about these. So good practice to have your class implement IDisposable to clean up any cloned assets itself.

There is no Clone() method in SpineAsset, it is custom made. It is a IDisposable class though. How does one clone the same asset? Sorry, I am still fairly new to Monogame.

Thank you.

Sorry not really sure in that case, I’m not familiar with Spine. Content Manager is really designed for loading and caching single instances of external assets, not for copies like this, so if possible I would have a single instance of SpineAsset for the animation definition and put dynamic data like positions somewhere else, except if this is a third party component maybe you can’t? In which case maybe this is a question for whoever created this Spine implementation, how to correctly use it with MonoGame.

I’m pretty sure you are right about content.Load only loading distinct objects once.

Instead of using the SpineAsset to store position I would store it separately then in the draw call I would update the position just in time so that it will be copied over before each draw call.

Sorry I can’t post sample code because I am doing my spine drawing in a very convoluted way, you may have to call something like Skeleton.UpdateWorldTransform() after setting the position but before asking it to draw.

You may also need to store a separate AnimationState for each particle.

Sorry for the late reply. I was experimenting with what you told me and it works. Thank you very much.