Hey!

I'm looking to try and understand how I could accurately go about comparing a PackedScene which is in memory (used .pack()) and a PackedScene in user:// which also happens to be the original file of the instantiated PackedScene that the user sees at this time and the PackedScene in memory is a updated version, or at least that is what we are trying to check.

I know I could simply have a bool for an change occurred, but this would be harder to work with (although maybe not too hard) if there are instantiate and queue_free happening in between checks for changes having occurred. So I'm looking for a way to compare.

I'm not looking looking to instantiate the scene in memory either if possible although I'm not 100% how I'd want to go about that, I assume this might be the next most common answer one do. I really want to for now at least (more for the learning experience) want to try and understand how one might do this.

I'd love to hear thoughts! 🙂

  • xyz replied to this.
    Knight_XAura changed the title to Trying to compare PackedScene in memory and a PackedScene on disk .

    Knight_XAura Instantiate both scenes, call get_tree_string() on their respective top nodes and compare the returned strings.

      xyz Hey, I'm aware of this, but as I stated, I'm looking to compare two packed scenes and not two instantiated scenes. I mentioned that I know I could do just as you've said, but is not the goal.

      Thank you for your answer though!

      • xyz replied to this.

        Knight_XAura I'm looking to compare two packed scenes and not two instantiated scenes

        What for? What's to be gained by packed comparison that you can't get from instantiated comparison?

        You also didn't specify what do you mean by "compare". You can compare in lots of ways. What criteria would determine that two scenes are "equal"? Binary representation equal byte by byte? Textual representation equal? Node structure equal? Node structure and naming equal? Node structure, naming and properties state equal? Etc. There's a lot of various criteria that can be applied and comparison method could differ significantly for each criterion.

        Also note that you actually are comparing packed scenes when you compare their instances. It's the simplest way to do it.

        DaveTheCoder
        I don't see in what practical situations the unstructured comparison based on file diffing would be of much use. Any slight change in any node's state would result in "different" scenes.

        PackedScene::get_state() returns SceneState object. It has a set of neat query methods that can be used for easy comparison based on various criteria, without instantiating.

        What's to be gained is not spending more resources then I need to, although I understand that maybe the work to compare would indeed be more resource intensive, but I don't know til I can learn more about ways to do this.

        What I do mean by compare to clarify further though is that I do want to be sure they are equal and if different in any way (variable change, new node in the scene or even removed from the scene). Now if a node was added and then that same node was removed before checked again to compare I would want the scenes to still come back as the same (this could be tough depending on meta data scenes track in Godot so this may not be 100%)

        I 100% agree it's the exact end result I want and simplest way to do it, but sometimes simplest isn't always what we want. That's why I explained I knew I could do that but came here looking for other alternatives. But I definitely still value the vote for that method as it may very well be how I have to do this.

        I made some changes though and I am going to no matter what run into a small difference that I'm kicking myself in the butt for now and that is the visibility of the root node in a few cases will always be different which I could make changes to accomdate that so this situation don't happen, but isn't how I want my game to work, but these maybe unavoidable.

        I will have to check out get_state() I didn't catch that previously maybe that could be of use.

        to further explain the purpose of why as it seems I'm driving you mad here lol - I'm attempting to do read operations to see if a saved packed scene matches the packed version of the running scene (I already have to pack to do the work that comes next if they don't match and so it why I've gone the direction I am) But I could do vice versa, but there are some potential challenges with spawning the scene to check that I'm attempting to try to avoid as well. I basically want to prevent writing to disk every time I check to see if something should be saved, if it doesn't differ from what's on disk already

        • xyz replied to this.

          Knight_XAura Whatever you're doing, comparing scenes is probably not the way to go. Instead you should maintain relevant state and compare that. Regardless, if you insist on packed scene comparison you can do it via SceneState as I mentioned earlier, so that's the answer to your original question.

            xyz

            Looking at SceneState and a bit more at PackedScene now that you point that out, I see some things I think I could use. I believe this to be exactly what I was asking for.

            Thinking even just a bit more on my changes and potential issues there's a very good chance I will like instantiate the scene and check that way, but I wanted to learn more about my options that is all and you have provided another way to look at what I've asked for. I appreciate the feedback and answer/suggestion 🙂

            • xyz replied to this.

              Knight_XAura Comparing scenes may actually be more costly than simply resaving. So just resave and worry about optimizing only if it proves to be a bottleneck in practice.