(image did not appear in deleted post so I'm trying again)
As show below, I created an instance of a scene that has several child-nodes. It is not loaded into the scenetree yet.
I'm trying to attach a script to one of the scene's children but after attaching to the scenetree (only for troubleshooting here, it is actually added later), it does not show up in the child-node when I look at the Remote scenetree.
(note: I use the word 'node' to describe aspects in my code that have nothing to do with Godot-nodes; it's a legacy issue)
update: I have found that I can't change any properties of an instanced node before calling add_child
----Since I want to setup all of my nodes and THEN attach the base node to the scenetree, this is a problem. If I add my nodes
---- piecemeal to the tree, then the script in each one will fire before I can install other ones.
----(even though; I CAN change properties after add_child, but still can't attach a script. Perhaps this is because it's a packed scene?

  • xyz replied to this.

    xyz

    I don't get an error message, I just get no script attached to 'bod'
    Some notes: global.PanHandler and global.SlideHandler are preloaded elsewhere.
    I've confirmed that nodescript is loaded ok and that bod has also been instanced ok.
    Although I have a more severe problem not being able to attach a script AFTER adding the child, what I really want to do is adjust some properties as well as attaching a script BEFORE adding the child scene. So far I can read properties of the scene but have found I can't write to them until the scene has been added as a child.

    func LoadNode(ID,overlay):
    	global.UpdateNodePaths(ID)
    	var a = load(global.scriptpath)
    	var nodescript = a.new()
    	nodetype = nodescript.type
    	if nodetype == "pano":
    		bod = global.PanoHandler.instance()
    	else:
    		bod = global.SlideHandler.instance()
    	add_child(bod)
    	bod.set_script(nodescript)

    Here is the Remote tree; global.SlideHandler instance is 'NodeHandler', which is the node I'm trying to attach the script.

    • xyz replied to this.

      xyz
      no.
      In my case encapsulation is not the goal. I am essentially recreating a scriptable game-engine within Gogot so the packed scenes are more like templates that get adjusted by the user's code in the attaching script (the user creates the script which then gets attached)
      Worst case I guess I can build the bod scenes from scratch. Speed is not an issue.

      • xyz replied to this.

        xyz
        I think I found the problem.
        I made a simple test from scratch.
        -I load the script I want into variable 'a'
        -I tried to read a variable directly from 'a' (variable name 'type') but that caused a failure.
        -I made a node attached to that script (a.new()?) and could then read the property.
        -But I was trying to attach the node I attached to the script ('nodescript') to my new node 'newnode'. Instead, I can attach 'a' as desired.
        Dont know why I need the a.new() to read from the script.

        func LoadNode(ID,overlay):
        	global.UpdateNodePaths(ID)    #creates a viable 'scriptpath'
        	var a = load(global.scriptpath)
        	var nodescript = a.new()
        	nodetype = nodescript.type
        	var newnode = Node.new()
        	newnode.set_name("kek")
        	newnode.set_script(a)

          Shadowphile Dont know why I need the a.new() to read from the script.

          Because you need to instantiate an object to read its property.

            xyz
            I don't understand what is going on here.
            'a' represents a loaded script.
            a.new instantiates the object but not sure WHAT is being instantiated.
            but I have to attach the loaded script, not the instantiated object.
            Can I instantiate just the script to read it?

            • xyz replied to this.

              Shadowphile Print out every object to see what it is.

              If type is a property defined in your script class, you need to make an object of that class if you want to read it. Not sure why you need that though.

              Calling new() on script resource is equivalent to calling Node.new() and attaching that script resource to new node object. Both will result in creation of a node object of a class defined in the script. Your code basically does the same thing twice.

              Shadowphile that's a lot of steps, not sure why that's different as set_script instantiates the script. Here's the simplest bit of code that should work:

              var player = Node2D.new()
              player.set_script(load("res://player.gd"))

              Do you have an minimal project where you can reproduce this? edit: what is the script that's being loaded, e.g. what does it extend? You mention there are no errors, so is it actually being loaded but the script isn't running as you expect? Stick some print statements in the loaded script, e.g. _enter_tree or _ready.

              I just checked in an MRP and the Remote tab doesn't show the 'script' icon for scenes and/or nodes but it is shown in the Inspector:

              loader has a script but is from a scene, script has a script assigned (godot doesn't show this in the remote tree) and @Node2D@2 is dynamically loaded with a script assigned by loader

              The dynamically loaded node and script.

              OP has different issues perhaps.

                spaceyjase and others...
                yes, there are more lines in my code than are necessary (so I can inspect the intervening steps)
                As for .new() on a script making a new node with the script attached, I can accept. It's one of those things you can only learn organically unless you have a deep knowledge of the engine.
                So for now, I am satisfied.
                thanks all