Forum rules - please read before posting.

How to save the presence of GameObjects spawned via script

edited May 2023 in Technical Q&A

I'm new to using Adventure creator and also new to C#. I am using Unity 2021.3.23f1 for Windows, and I coded some functionality without realizing that I also needed to do stuff to make sure that what I wanted saved stays saved.

This is the C# script I wrote:

    GameObject MessageObject = Instantiate(ReceivedMessageObject, new Vector3(0, 0, 0), Quaternion.identity);
    MessageObject.transform.SetParent(ConversationBox.transform);
    MessageObject.transform.SetLocalPositionAndRotation(new Vector3(0, 0, 0), Quaternion.identity);
    MessageObject.transform.localScale = new Vector3(1, 1, 1);

I also delete some MessageObjects after a certain amount of them are spawned in with this script:

    GameObject ChildToDelete = ConversationBox.transform.GetChild(0).gameObject;
    Destroy(ChildToDelete);

I tried to save the presence of each MessageObject by attaching the RememberTransform script to the prefab of the ReceivedMessageObject as so:

And then I put the prefab in Assets/AdventureCreator/Resources

When I tried it with the code I have, it didn't save the presence of the MessageObjects.

I read in the Manual that the RememberTransform script is supposed to save objects specifically added using the "Add or Remove Object" action in an action list, but it would be cumbersome for me to make an action list and try to link to that action list via my script instead of just calling the action to be performed from within the script, but I couldn't find any examples online on how to do that.

I tried doing it with the stuff I found in the manual, but that didn't spawn the MessageObject. This is what I wrote:

    ActionInstantiate Instantiate = AC.ActionInstantiate.CreateNew_Add(ReceivedMessageObject);
    GameObject MessageObject = Instantiate.gameObject;
    ActionParent Parent = AC.ActionParent.CreateNew_SetParent(MessageObject, ConversationBox.transform);
    Parent.setPosition = true;
    Parent.newPosition = new Vector3(0, 0, 0);

I also tried using Instantiate.Run(); and Parent.Run(); afterward, and it did nothing.

Can anyone help me figure out how to get my objects' presence to be saved considering all this? Please do bear in mind that I'm new to C#, and I'm actually learning it because Adventure Creator couldn't accomplish parts of the concept I had in mind. So I might need some guidance on basic things.

Comments

  • Welcome to the community, @Erallie.

    So long as the prefab is set up with AC's requirements (naming convention, placed in Resources, etc)m it should just be a case of updating your own spawning code to have AC recognise it.

    This just involves calling the Remember Transform component's OnSpawn function.

    To do this, add the following after your first code-snippet:

    MessageObject.GetComponent<AC.RememberTransform> ().OnSpawn ();
    

    If that doesn't work, it'd be worth putting the custom code aside for a moment, and trying it with the regular Object: Add or remove Action as normal - i.e. in the ActionList Editor.

  • edited May 2023

    What are all of AC's requirements @ChrisIceBox ? What's the naming convention and the other requirements?

  • Thank you @ChrisIceBox . My game is now spawning all the MessageObjects, but it's not saving its parentage, even though its direct parent has a constantID.

    The scene loads with their parent not active in the hierarchy, though. Would that have anything to do with it?

  • What's the naming convention and the other requirements?

    For an object to be re-spawnable via the save system, it'll need to have a unique filename and be placed in a Resources folder (or made Addressable). More on this can be found in the Manual's "Saving asset references" chapter.

    The scene loads with their parent not active in the hierarchy, though. Would that have anything to do with it?

    Is the parent also a spawned object?

    This may cause an issue if the objects are re-spawned in the wrong order - i.e. the child before the parent. It may be possible to rely on a custom Remember component that separately re-attaches an object to its correct parent.

  • edited June 2023

    @ChrisIceBox The parent is not a spawned object. But the parent doesn't start as active in the hierarchy. Does the parent need to be active in the hierarchy in order for it to parent it correctly?

    Actually, I just checked right now, and it's because the parent wasn't active in the hierarchy that it didn't parent it to the right object.

    I also checked, and it seems like it doesn't remember the objects unless they are active in hierarchy when the game is saved either. Is there a way to work around this, so that it remembers the spawned objects when they're not active in hierarchy too?

    But now I have another question: how do I make sure the objects are spawned in the right order? Because there are multiple objects spawned that need to be parented and because of the way I set it up, the order is important for this project. Would I need a custom remember component script for that? And what exactly would I need to put in the script to make it work? I can try to reference those tutorials of custom remember scripts on the website, but it's still a bit difficult to learn.

    Another question: I haven't tried it out yet, but if the object to be spawned contains variables and a remember variables script, will each spawned object's unique variables be remembered when they are spawned? And if not, how would I save those variables when they are spawned?

  • I also checked, and it seems like it doesn't remember the objects unless they are active in hierarchy when the game is saved either. Is there a way to work around this, so that it remembers the spawned objects when they're not active in hierarchy too?

    An object must be enabled to be automatically "visible" to the save-system, as it will register itself with AC when the component itself is enabled.

    You should, however, be able to register an object manually, through script, if you refer to its Constant ID component:

    AC.KickStarter.stateHandler.Register (myConstantIDComponent);
    

    how do I make sure the objects are spawned in the right order? Because there are multiple objects spawned that need to be parented and because of the way I set it up, the order is important for this project

    As in, a chain of objects parented to one another? If you can share details on the situation, it'll give me a better overview to advise from.

    The first thing I'd say is to check that the parenting is necessary - since if it's just to do with relative positioning, a script could feasibly be used to instead have objects separated but position them relative to one another each frame.

    When it comes to loading Remember data, Remember Transforms are loaded first - and then other Remember components. Currently, there's no way to control the order in which components within these two groups are loaded - but you raise a good point, and I'll look into this possibility. Again though, any detail you can share on the situation will help my approach.

    I haven't tried it out yet, but if the object to be spawned contains variables and a remember variables script, will each spawned object's unique variables be remembered when they are spawned?

    Yes. You can attach additional Remember components to those with Remember Transforms, and they will be restored once re-spawned in the scene.

  • @ChrisIceBox So, what I have is that a bunch of different UI objects (specifically chat messages in a simulated desktop experience) are parented under a single game object (the conversation box that holds the chat messages), which has the "Vertical Layout Group" element. So the sibling index of each spawned game object is really important, otherwise the chat messages would be out of order.

    I fixed the issue of things not being active in hierarchy by starting the scene with everything enabled, and then using a script to disable the appropriate game objects once everything is loaded in. So that's sorted.

    Also, I tried the remember component thing, and it works! Thank you!

  • Thanks for the elaboration.

    The next update will introduce a means to set the relative order that Remember Transform components are loaded in - it might help to simplify the process a bit.

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.