Changing the texture (and other attributes) of the last instanced node OR the node I'm about to instance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’ve build a level editor for Breakout/Arkanoid.
I have it saving all the brick placement, colour and other info required as json data.
I have it loading the json data.
I have it drawing bricks on the screen on load.
SCHWEET! Things are looking spivvy! :slight_smile:

The problem is, it loads the original brick scene file, with the same texture.
I now want to have it, that when it loads the json data it changes the texture of the instanced brick file.

So I have a couple of ideas, but I can’t seem to execute either:

Change the texture BEFORE instancing.
I like this idea. I can change the texture of the TSCN file, then instance it. Repeat for each instance. Unfortunately I just can’t figure out how to do this. I can use get_node() to get an already instanced node, but not to one that’s not instanced yet.

Change the texture AFTER instancing.
This should be easy. Instance node. get_node(). Change texture.
My problem is, I don’t know how to get the name of the last added instance so I can do this. If I could work that out I could do something like get_node("path/to/last/instanced/object/").set_texture(etc).

Can anyone offer advice on any of those options, or even suggest something else?

Thanks so much…

:bust_in_silhouette: Reply From: volzhs

when you make an instance, you can get reference of instanced scene.

var instanced = load("res://scene/node.tscn").instance()
instanced.get_node(path).set_texture(texture)
add_child(instanced)

OK that’s looking embarrassingly obvious :slight_smile: Thank you for that.

What about the path that you’re passing? I entered path and (face slap) that didn’t work (embarrassing that I tried it but true), but then I tried to figure it out by replacing it with the /root/ etc but I can’t figure out what the path should be…

Can you offer advice on this?

My tree looks like:
Root

  • Grid Designer
    – Brick 1
    – Brick 2
    – etc

So I would have thought path would be /root/GridDesigner/ but it’s not.

Robster | 2017-04-20 05:13

the path is relative to instanced scene.
if you have node.tscn scene like below, you can simply do instanced.get_node("sprite").set_texture(texture)

- Node2D  // instanced variable refers to this node
    - sprite

volzhs | 2017-04-20 06:53

So it’s the path that’s internal to the instanced node! No wonder I couldn’t figure it out. That’s PERFECT and so well designed.

Thank you so much, that’s a big lesson learned for me.

For anyone else, below is the corrected line. My level editor is now working! Will post it all on the QA site when done and dusted. :slight_smile:

newBrick.get_node("TextureButton").set_normal_texture(textureToUse)

Robster | 2017-04-20 06:58