i have a Scene with an attached individual resource, "data". I instantiate the scene each time i press a button from inside my "Main" scene. I want to click that scene i created in run time/load, and have the attached "data" accessed in my global script of that specific selected node. Be able to change that data, Then have that change overwritten in my original "data" attached to the specific scene. I could just set all the global variables to be what that's nodes data is.... but i was hoping a better way to parse the node globally for its attached resource value(s).
As always you are the MAN!

  • xyz replied to this.

    xyz I am probably just being very stupid or simply have not learned the way/best way to go about this. really hoping to take some courses when i can to build up my understanding. until then i'm doggy paddling the Godot ocean and would appreciate a life vest 🙂

    REVBENT How is the resource declared and create in that scene you instantiate? Do you manually create a resource in the editor?

    i created a resource script in filesystem.

    DATA.gd #working

    I then have another scene that used that resource as an :

    @export var data : Resource = DATA.new() #working

    In my "Main" scene....

    var scene = preload("blahblah") #working

    when a button is clicked....:

    var new = scene.instantiate() #working
    add_child(new)

    I want to click on the instanced node.... and have its properties available in my global script without setting each one inside the node when clicked. which is what i currently was doing.... but the whole reason to use a resource is to be able to add to the project with ease and not being broken if i forget to set something. Like i said i may be missing the "way" to do this and reading the docs has gotten me even more confused.
    My project is a test of stuff.... built on a test of stuff.... and without explaining what all the lost features are, it is impossible to navigate intuitively.
    If i am just not explaining myself properly i will try to clean up what i have and post an example.
    I was just hoping my ignorance/aptitude was to blame and simple do "abc" by XYZ may get me out of the proverbial woods.

    • xyz replied to this.

      REVBENT Do new.data. The variable new is a reference to the node that contains the property data. Upon instantiation, pass this reference to anyone who needs to access data.

      Btw you should avoid using the word new to name your variables. It's a name of a built in method in Object class from which all other classes are inherited.

      what if i have multiple of the same instance? but want that data to be individual?

      • xyz replied to this.

        Say i press the button 3 times, but want one red, one blue, etc, save/load, and maintain data integrity to the specific node.

        Am i just trying to do too much, too easily?

        • xyz replied to this.

          REVBENT If the script is creating a resource instance via DATA.new(), the each scene instance will create its own copy of the DATA object.

          REVBENT Here's a simple example to play with:
          Resource class:

          class_name Data extends Resource
          var foo = 1

          Instantiated scene class (scene.gd):

          extends Node
          @export var data = Data.new()

          Script that does scene instantiation:

          extends Node
          func _ready():
          	var s1 = load("res://scene.tscn").instantiate()
          	var s2 = load("res://scene.tscn").instantiate()
          	print(s1.data)
          	print(s2.data)

          You should get a printout of two different Data resource objects:

          <Resource#-9223372008316730102>
          <Resource#-9223372008132180722>

          In the case you don't do Data.new() in the scene script, but instead you create and assign a Data object in the editor (to an exported property) that resource will be shared between all scene instances.
          However if you enable resource_local_to_scene flag for that resource, the engine will automatically duplicate it when instantiating the scene.

          ok i will try to figure this out, i am struggling to visualize this. Maybe some coffee and a break will help it sink in.
          Thanks for the heads up too with naming conventions. I actually do use a better naming scheme for everything, but just for example sake i typed that stuff.
          In my global script how should i reference the created node? I was sending the "self", the getnode(self), getnodepath(self)... as a NodePath in my global script.
          I would then try to say in process:
          if Input.action....(Leftmouse):
          var scene = <path of node i was sending>.data
          scene.Name = globalname
          .....and the above was not working.... so i think i was trying to access the property properly potentially (see what i did there 😛)....
          what i think i am doing wrong is referencing that specific node (created at run time) in the script.

          seriously thanks for taking your time. i know we all got shtuff to do!

          how do i load the:
          <Resource#-9223372008132180722>
          in the global script.....
          i can get my node to reference that and send it. i think i already did.
          so in the global i want to load that resource from reference at runtime, without declaring the variables in _ready()... because the number will change depending how many objects i instantiate or delete.... as in i wont know "S1" to "Sx) at run time/change with load data

          • xyz replied to this.

            REVBENT By "global script" you mean autoload/singleton?

            or do i say something like:

            var object = <Resource#-9223372008132180722>
            object.Name = globalName

            yes.... autoload. I figured i was safest to use that to hold the information and have a separate window to change data... with the singleton acting as the "middleman"

            • xyz replied to this.

              REVBENT Who calls the code in the autoload script that then access the resource? Whoever calls it should also supply a reference to the instantiated scene node or its resource object.

              BTW... i have read you explaining instantiate stuff (maybe the above example???)... as well as many other posts. I really do try to source other places/archives best i can.
              With that said...
              You put some serious time into this site. Its highly impressive and very appreciated.
              I know i say this a lot... but i feel it should be.... THANK YOU!

              The object scene itself is setting a global variable when Input to its <id/path/self>.
              In the autoload i have an export var for <path/id/self>.
              I am trying to create a global func to set a value (data) in another scene (controller scene) that changes the values of the "data"
              The global script have separate set of variables to be set to what the new_data.
              If the player chooses to keep "new_data" it will save the changes to objects "data"...
              If the player chooses to discard i wont have touched the original data.
              I was thinking to have more types of controllers or objects eventually and or access them between scenes.
              I think that is how i need to structure handling data that's created/edited at run time???

              • xyz replied to this.

                REVBENT To complicated for my brain 😃
                Can you explain what are you trying to achieve? Without any technicalities, or ways you tried to do it. Just what needs to happen "on the outside", from the player perspective.

                I want to push a button in game: #MAIN scene
                create object(1)
                push button;
                create object(2) #just for example it may be other objects... they are added with button in game
                click object(1)
                load object DATA into "Menu" #Controller scene
                change object(1) DATA via buttons/sliders in "Menu"
                close the "Menu" saving the DATA(new) ....or... close the "Menu" keeping DATA(old)
                switch scenes back to "MAIN" where the object(1) property has been updated to DATA(new) #MAIN scene again

                I want to load data from object(1) into "controller scene".
                Change the data in the "controller scene". SAVE or NOT
                Switch to Main scene and keep the data.

                I think i am not referencing the created node or its properties correctly in my code.

                • xyz replied to this.

                  i can do all of this if i have the scenes in the scene tree and access them by their path. All that works for me, but when trying to do so "dynamically" in game (scene added to tree in game), i can't get the nodes reference to work in my global scene.