I want to know how i can connect the Area2D? I cant find that button by area2d which looks like a plug to do connection

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

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.

:bust_in_silhouette: Reply From: Zylann

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")
    sprite.set_texture(texture)

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"):
        body.touched_by_thing()

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

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

yeeshu06 | 2017-03-06 13:30

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")
    sprite.set_texture(texture)

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 ):
    body.on_touched_by_thing()

Will be appreciated if you can help. I have managed to make the light disappear, where instead of body.on_touched_by_thing() i simply put “queue_free()”

yeeshu06 | 2017-03-06 19:50

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.

Zylann | 2017-03-06 20:18

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

yeeshu06 | 2017-03-06 20:21

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")
sprite.set_texture(texture)

Or simply put:

get_node("ChildSprite").set_texture(texture)

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

Zylann | 2017-03-06 20:38

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.

Thanks.

yeeshu06 | 2017-03-07 16:08

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

Zylann | 2017-03-07 20:17

Imgur: The magic of the Internet

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.

Thanks.

yeeshu06 | 2017-03-08 07:02

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")
    sprite.set_texture(texture)

If you want to change the “led_sprite” instead, then… write “led_sprite” 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().

Zylann | 2017-03-08 20:00

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

Thanks a lot.

yeeshu06 | 2017-03-09 06:18