0 votes

I am trying to get character to hit a sprite (area2d), and as soon as that happens it should change to different sprite. I cannot find that button that looks like a plug, which i see was in older version of Godot. Please help.

Oh and also wanted to ask, i see and understand how to vanish a sprite once it comes in contact with character, but what if i want to change the sprite as it collides? Thank you.

in Engine by (74 points)

1 Answer

0 votes

Connecting nodes from the editor is now done in the Node dock. Select the node you want to connect a signal from, and then see what you can connect in the Node dock.

Changing the texture of a sprite can be done in several ways depending on your setup.
If you have two images in two files, you can set another texture in script this way:

func on_touched_by_thing():
    var texture = preload("res://path/to/your/image.png")

I'm not sure how your scene is setup so I can't really tell where to put the code above in context. What I usually do is that I make my characters have a physics node as root with a script having a touched_by_thing function. The sprite node is a child of it, and then I do this in the area2d:

func _on_area_body_enter(body):
    if body.has_method("touched_by_thing"):

(It's just one way of doing it, among others)

by (28,731 points)
edited by

Cool i will try it out and give feedback. Appreciate it

Hey Zylann. So to explain the set up i have, have a scene with character running around, and as soon as character jumps and catches the light, it changes the light (sprite).

So there is a scene that holds the environment (root node). A player (character) is child of this root. And the light (saved separately as xml file) is also child of this scene. Now within the light file itself i added this code to have a little animation and also do area2d.
So as soon as the character touches area2d it should substitue with a different sprite. Was wondering if i should keep the sprite(new texture that should pop up in place of old one) in light.xml or scene.xml?

Preview of code from light.xml :

extends Node2D

#for small lights, get 1 point
export var light_value = 1

func on_touched_by_thing():
    var texture = preload("res://InsideHouse_lvl01/c2_lightLED.png")

func _ready():
    print (get_owner().get_name())
    if get_owner() != null:
        get_owner().lights_total += light_value
    #connect the area2d with node using code, connect function. Say 'self' cos its referring to light node
    get_node("Area2D").connect("body_enter", self,"_change_lights")

func _change_lights( body ):

Will be appreciated if you can help. I have managed to make the light disappear, where instead of body.ontouchedbything() i simply put "queuefree()"

Your code looks fine, I don't see why you would put the new sprite in another scene, because that's still a light, right?

Note: I edited your post to format the code. To do so in further code samples, add a newline before and after it, then indent the block by 1 tab (you can see a preview when you edit the post). Or if in doubt, use the format buttons.

Yes, the sprite will also be a light. So your saying i should just have the new light sprite within the old light.xml. hmmm i tried that. An error pops up saying : Parser Error: Identifier not found: sprite. Thought Sprite was inherited from Node2d

I don't think you understood the code I gave you, it was just an example^^
Is your script on a Sprite node? If it is, then you can simply write set_texture(texture).
If your sprite is a child of the node, get it first and then set the texture:

var sprite = get_node("ChildSprite")

Or simply put:


Also, another note: the .xml format has been deprecated in favor of .tscn, just so you know ;)

Hey Zylann Thanks a lot. I am still confused and it has not worked. Can you provide me an email so i can send u a screen shot of how my scenes are set. I am new to this so yeah some help would be appreciated.


You can try this https://imgur.com/
So anyone will be able to see and help


Hey I have attached url link to where i put pics up. They are how the scene is set.
One is room scene, where the character is as well as the separately created light.
Other is the light scene set up.
I don't know if that makes sense. would really appreciate if you can take a look.


Well... you have two sprites in your light scene. Which one do you want to change?
Whatever it is, the code is just the same as before, and as I explained above you need to get the sprite node by its name:

func on_touched_by_thing():
    var texture = preload("res://InsideHouse_lvl01/c2_lightLED.png")
    var sprite = get_node("incandescant_sprite")

If you want to change the "ledsprite" instead, then... write "ledsprite" instead of "incandescant_sprite". I see it's also hidden in your scene, so if you want to show or hide it, use sprite.show() or sprite.hide().

Hey ZYlann i cant thank you enough. Really appreciate it. It works.

Thanks a lot.

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 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 webmaster@godotengine.org with your username.