Page 1 of 1

Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 12:37 pm
by Peter Drake
I'm using the Dialogue System with More Mountains' TopDown Engine and Inventory System. Previously, thanks to an enormous amount of help from Tony Li, I was able to get inventories to persist between scenes:

https://www.pixelcrushers.com/phpbb/vie ... f=3&t=5849

My next task is to get picked up and dropped objects to persist between scenes. In other words, if I pick something up, I want it to be gone (although of course present in my inventory) if I leave the scene and come back. If I drop something, I want it to be there if I leave the scene and come back.

I started by following the instructions here:

https://www.pixelcrushers.com/dialogue_ ... nedObjects

This worked halfway. All of the objects that I drop are still there when I leave the scene and come back. The problem is that objects that I've picked up are also still there! Consequently, the player can make many clones of an object by picking it up and dropping it repeatedly, then leaving and returning to the scene.

I tried changing the Despawn Mode in the Spawned Object script attached to the object prefab to "On Disable". This doesn't seem to help.

I've noticed that the Inventory Engine can disable objects when they are picked up, but not destroy them. This leaves a lot of disabled objects lying around the scene.

Maybe this is an Inventory Engine problem -- I'll ask on their Discord -- but any help here would be appreciated.

Re: Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 1:03 pm
by Peter Drake
Update: reuno of More Mountains says: "destroying objects at runtime comes at a cost, disabling is free"

This is backed up here:

https://answers.unity.com/questions/133 ... chang.html

So: how do I get disabled object to not be saved?

Re: Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 1:27 pm
by Tony Li
Hi,

Try adding a Multi Active Saver component to an active GameObject in your scene, and set a unique key. Then assign all design-time item pickers to it.

Renaud's essentially correct about destroying vs disabling, but disabling isn't entirely free. It's free in the sense that destroying an object can create free garbage memory that will need to be collected at some point, whereas disabling usually doesn't create much garbage. (Frequent or large garbage collection jobs can cause stutter.) However, disabling an object makes it run all of its components' OnDisable() methods. I only mention this in case you write scripts with OnDisable() methods. Those methods should ideally be very lightweight.

Re: Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 1:42 pm
by Peter Drake
Is this in addition to or instead of the SpawnedObjectManager? If it's both, should they be attached to the same GameObject?

Re: Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 1:53 pm
by Peter Drake
I got it working. The answer is:

Both savers are needed.

They can be on the same component.

They need to have different keys.

Re: Spawning and unspawning objects with MM Inventory System

Posted: Wed May 25, 2022 2:02 pm
by Tony Li
That's good to point out. For item pickers that get spawned at runtime, the prefab needs a SpawnedObject component. You only need set up a (Multi)ActiveSaver component to track the instances that you've placed in the scene at design time. And the keys do need to be unique.