Forum rules - please read before posting.

Loading specific part of the object

edited December 2018 in Technical Q&A

Hi!

I've created a custom Remember Script which works fine. When the object is in the scene, I can use custom Remember Script to save the necessary data which is preserved when you switch the scenes or when you load a save.

The tricky part here is that I want the object to be Instantiated, and once that's done I need to load the previous state of the object with custom Remember Script.

I tested something like this:

GameObject spawnedObject = Instantiate(character, marker, rotation);
spawnedObject.GetComponent<RememberData>().LoadData("Autosave");

However this gives me a nasty looking error: EndOfStreamException: Failed to read past end of stream.

Any ideas how to solve this?

Comments

  • You can use the provided Remember Transform component to save an object's spawn-state within the scene.

    Attach the component and check Save scene presence?. Then follow the steps outlined in the Manual's "Saving asset references" chapter, i.e. give it a unique filename and place it in a Resources asset folder.

  • edited December 2018

    I basically have a character NPC and I added a component to him which is a script with a bunch of variables - his name, description, gold award for the player etc. I later use this variables in custom Actions.

    So the thing is, it's just a script, not really an asset. Would I need to place the script in Resources folder?


    EDIT:
    I've added Remember Transform component and checked Save scene presence?
    I also placed the character prefab in Resource folder and it seems like it works!!!!!!!! I'm going have to test it some more but it seems like it's working!

    Thank you!

  • But you're still spawning the NPC in at runtime, no? To save such a change, you will need Remember Transform.

    Are you making changes to these stats? If not, there's no need for a custom Remember script.

  • Yes, the NPC is spawned at runtime, and I'm making changes to the stats, hence I got custom Remember script.

    Currently, it seems that after adding Remember Transform and checking Save scene presence? the character stays in his place when you change the scene, and when I Instantiate him again later, I get two copies of the same character (one is (Clone) one is normal though).

    I thought I can remove the character just before you switch scene, and when you go back to the same scene, I can instantiate him again and it will be fine. Object: Add or remove action doesn't seem to remove the character though.

  • edited December 2018

    To clarify, what I am after is:

    • Instantiate a character
    • you then talk to the character
    • his stats change depending on your dialogue option (custom script with variables, added to character as a component)
    • stats are saved using custom Remember script
    • character "leaves" the scene and gets deleted
    • at later stage, the character is Instantiated again, you can talk to him, but his stats are loaded

    Basically everything works, except for the last point - when I instantiate character again, his stats are back to default values.

    Attaching Remember Transform and checking Save scene presence? did help, but it makes the character stay in the scene, so later when I Instantiate him again, I get two copies of the same character.

    I also played around with using ScriptableObjects, but that introduces another set of problems. At this stage I'm not sure if this should be done using AC's custom Remember Script or would I need to create something totally different to save my data.

    This type of thing will be repeated continuously throughout the game, for a lot of characters. Another approach I'm considering is to simply have all of the characters in the scene, but somewhere not visible to the player. Instead of Instantiating the characters, I simply teleport them to location in the camera. I am worried about performance in the scene though. If there's ~100 characters, all with their components and conversations, it may get a bit slow. I know this is not your typical adventure game scenario, but I really hope there is a way to achieve this.

  • it seems that after adding Remember Transform and checking Save scene presence? the character stays in his place when you change the scene, and when I Instantiate him again later, I get two copies of the same character

    Of course - saving his presence will retain him in the scene, which allows you to avoid having to re-spawn him each time.

    If you control over his presence in the scene (i.e. spawn him manually), then remove Remember Transform.

    However, AC can only save/restore data for objects that are present in the scene at the time of saving/loading. When you re-enter the scene, the expected custom Remember component is no longer present and is left out of the "data loading" process.

    With 100+ characters, I agree that performance would be a consideration. As opposed to spawning/removing an NPC, one alternative would be to disable their Animator and NPC components, i.e.:

    myNPCGameObject.GetComponent <Animator>().enabled = false;
    myNPCGameObject.GetComponent <NPC>().enabled = false;
    

    Though the GameObjects would still be present, their "footprint" should be reduced significantly.

    If you do need to spawn/remove at runtime, you could consider moving their custom Remember components onto separate GameObjects which are left in the scene and refer to their associated NPC by name.

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.