Spawning and unspawning objects with MM Inventory System

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Peter Drake
Posts: 37
Joined: Tue May 17, 2022 3:49 pm

Spawning and unspawning objects with MM Inventory System

Post 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.
Peter Drake
Posts: 37
Joined: Tue May 17, 2022 3:49 pm

Re: Spawning and unspawning objects with MM Inventory System

Post 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?
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Spawning and unspawning objects with MM Inventory System

Post 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.
Peter Drake
Posts: 37
Joined: Tue May 17, 2022 3:49 pm

Re: Spawning and unspawning objects with MM Inventory System

Post 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?
Peter Drake
Posts: 37
Joined: Tue May 17, 2022 3:49 pm

Re: Spawning and unspawning objects with MM Inventory System

Post 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.
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Spawning and unspawning objects with MM Inventory System

Post 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.
Post Reply