0 votes

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! :-)

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...

in Engine by (838 points)

1 Answer

+1 vote
Best answer

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)
by (9,796 points)
selected by

OK that's looking embarrassingly obvious :-) 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.

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

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. :-)

newBrick.get_node("TextureButton").set_normal_texture(textureToUse)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.