Forum rules - please read before posting.

Global Containers

edited August 2018 in Technical Q&A
Unity 2018.2.2f1 AC 1.64.1

In my current game I've got multiple potential Players. They can exchange inventory items by using one of the many scene-specific local containers scattered around the game world (which consists of numerous linked screens)

I've got two extra requirements though:

1) I'd like a "Global" container, which could be used by any Player in any Scene. In other words, if Player A puts Item X into it in Scene Y, then if Player B opens it in Scene Z, then it will still contain Item X

2) On the same lines, I'd like for Players to be able to exchange items between themselves, probably by means of a similar "Global Container" mechanism. I think if I can crack the first option, then this would just be a variation of it

So, what I've tried so far is to set up a Container Prefab Object. The Prefab contains a Container script, and a RememberContainer script, with "Retain In Prefab" ticked. It is not limited by Category

As a test I've got two different scenes, each of which has a Hotspot instigated ActionList which simply runs an "OpenContainer" Action, specifying the Prefab Container, which allows different Players to add and remove items from the Prefab Container

That works as expected. Put an item in the Container in Scene 1, and it will then be available in the Container when opened in Scene 2, etc

But, it's working directly on the Prefab, so, on exiting from the game the Container Prefab is left in its last state. Well, that's not too terrible, because I want this particular Container to be empty at the start of the game, so I simply run a "Container/Remove All" when the game starts, and everything is fine...

...except (and there's always an "except")... except that if the Player makes some Inventory changes to the Container and then does a Save and Reload, the Container Prefab doesn't reload its state from when it was saved, it is simply empty

So, really the questions are:

1) Am I going about this in the right way?
2) If so, do I need to write something extra to save and reload the Prefab Container data?
3) If so, do you have any pointers on how to approach it?
4) Or, um, something else...?

Comments

  • The issue will be that Remember components only work on scene objects - placing one on a prefab itself will have no effect because prefabs aren't (and shouldn't be) modified at runtime.

    I'll have to look into this further, but I expect the way to do this would be to instead have a local instance of such a prefab present in each scene - and then transfer the open scene's Container contents into the next scene's upon changing scene (except when loading a save).
  • As I hoped, this can be done with a custom script (though the latest version of AC, v1.64.2, is required).

    SyncContainers.cs

    I'll make a wiki post later, but I'm posting it here to put it through some testing first.  Essentially:
    1. Drop a new Container in the scene and add the script above
    2. Check Retain in prefab? in the Remember Container Inspector and make a new prefab from it
    3. Place an instance of this prefab in each scene.  Checking the box in 2) should ensure each instance shares the same Constant ID number
    Upon loading a new scene, the script causes the Container to retrieve the contents of its associated Container in the previously-open scene (provided the scene change did not occur from loading a save game).
  • edited August 2018
    Cool. Thanks for that Chris

    I've currently got a problem with reloading saved games (completely unrelated to this issue, and totally down to something I've done), so I've not been able to fully test this feature yet, but as far as I've been able to tell it's doing exactly what I wanted

    Once I've fixed the other thing I'll run some further tests
  • Well, this has proved quite a useful exercise for me in more ways than one, because I'd not really given the Save/Load system a vigorous workout with my current AC configuration

    However, I've now discovered quite a few little "quirks" (all on my side, relating to scripts I'd added to allow me to process arrays in the game, and to give my NPCs some extra AI), one of which was a show-stopper that initially prevented me from fully testing whether the Global Container persisted correctly across Saves

    Once I'd fixed that, I was able to sit down and properly test it, and I can confirm that as far as I can tell, all is good with the new feature. The Global Container retains its state across scenes, no matter which Player is accessing it or where it's being accessed from, and, crucially, after a Save and Reload it's in the state it was last left in

    So, thanks once again Chris for adding this



    (Now, all I gotta do is fix those other pesky little bugs, but that's another story...)
  • Hi guys. Thank you again for this script, I thought I had answered back then, it worked!! for a while... Because after a few updates, the SyncContainers.cs stopped working. Is there a new way of doing this that I have missed, or could somebody help me with the script? It gives a few different errors type like CS1061: 'SceneChanger' does not contain.... and for now I've been ignoring it, but its time for me to face the music :)

    thank you again!

  • ok, for now my workaround is this:

    • Found this script: https://adventure-creator.fandom.com/wiki/Creating_a_global_Container
    • In the first scene, I have it add the container then "check" it.
    • after it had been interacted, the container keeps the objects correctly when checking from any scene. (not sure why it needed to be opened or checked on the first scene, but im fine with that)
      So, as long as these works too, I guess we are golden, but let me know if I'm missing or way off.

    thanks again :)

  • By "checking", you're referring to the use of the "Container: Check" Action?

    There shouldn't be a requirement to use that for it to work - can you explain the issue that you're trying to solve with it?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.