Want to attach a path to a scene instance in another scene instance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By monsterousoperandi

I have set up my game to have a basic scene format that I instance many times over. I want to be able to keep my scene instances organized by attaching them to chapter nodes but the only way I’ve found to be able to export a scene instance path, saved in a variable, is with the NodePath export hint. The problem is, that when I save a scene instance B to another scene instance A, it grabs the relative path to instance B from A. Then the node that calls my change scene function is in another node and I can’t hardcode a way to modify the path if I go from one organizer node to another. Even if I could, I can’t help be feel there should be a better way. Is there a way to specify to the editor that I want to grab an absolute path, rather than a relative one or a better way to save a reference to a scene instance in a scene instance, in the editor?

Something like: export (NodePath, "Absolute" ) var node_path_1

I want to be able to easily save a reference to a scene instance to another scene instance from the editor. If there is a better way to do that I’d really appreciate some pointers.

I’m not sure I understood your setup correctly, but in general, I would avdvise you don’t reference nodes from outside your scene. At least, not withing the scene. Notably because it makes your scene unable to work by itself (if you save a scene separately, it is generally good practice to make it self-contained as far as its job goes). Communication between scenes in such a situation should better be done by whichever scene containing the instances, because the container has the necessary context.

Zylann | 2022-03-22 13:50

:bust_in_silhouette: Reply From: w411-3

Like Zylann kind of referenced, you want to make your scenes as “self-contained” as possible, and I think that’s the key here. This isn’t an engine problem, it’s some programming principles like “encapsulation” that are in your way.

Looking at the concept of a “manager” node might help you out; its whole job would be to keep track of / be the mediator for these instances you are producing. Inside the script for that manager, you can just save direct references to your instanced objects in an array as you create them, or grab them dynamically if you create them in some other way that makes them retrievable (a node-naming scheme of some kind).

Anyway, it’s a programming problem. Chart out what objects are responsible for what, separate the jobs out, THEN program, and the implementation will become clearer.

Sorry If I wasn’t clear. What I’m doing Is part dialog tree part text adventure but with buttons instead of text. What I want to be able to do is attach a reference to an instance of the next page of the dialog/passage of the adventure to the previous passage. This way I can write plan and develop everything from the editor without needing to write any code.

I have it set up so that the passage has a var for button text, something like “open the scary door” then it has a variable that stores a refernce to the next passage so that I can create my story without having to put my developer hat on. The problem is that the scene I’m in will grab a relative path to the scene instance (passage) 'm going to when I use the export (Nodepath) hint. Is there a better way to brab this path from the root node using an export var?

monsterousoperandi | 2022-03-22 21:34

Sorry, My first comment was ment for Zylann. I have a manager node that does the actual controlling of the instancees. Also what I have works almost perfectly… except when I add nodes to try and organize my Instances better. It don’t think I need to overhaul my system for one small thing that seems like it should already be a feature… but if it comes down to it I’ll just have a massive list of scene instances in my node tree… still I feel like there should be a way to store the absolute path to a node in an export var. I cannot be the only one who’s found a need for this. Thanks for you answer and your time!

monsterousoperandi | 2022-03-22 21:51

I think I agree that you won’t have to overhaul your system, but I also still think that this is a programming issue. We can’t know if you don’t show pictures or scripts or something, but somehow you are not managing your objects very nicely. There is an easier way to keep track of what the next scene is and how they connect without all this path stuff.

I think Godot keeps the path relative for a reason, and you’re going to have to do a little extra research to figure out the best way to do this. I know it sounds like you’re really close but this only forces you to make better decisions for later

w411-3 | 2022-03-23 04:45

:bust_in_silhouette: Reply From: monsterousoperandi

So I found a way… Its not as elegant as I’d like but it works. I used the get_node_or_null() function to try out each of the potential parents in an array of parents and only change the scene if it retuned a value other than null. Messy, but still better than having to refactor my whole system.