How to pick a random sprite to show up when entering area2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EitherRock

Hey, how’s it going? I’m making like and hide and seek mini game and I need help!
I have an area2d node set up with two sprite as its children. One is a kid and the other is a scary monkey face and I used the hide() to keep both of them hidden. When the player enters the area2d I want to be able to randomly select which sprite shows up but im having trouble trying to figure it out.

:bust_in_silhouette: Reply From: Zylann

I think you can connect the body_enter signal to a function like this:

func player_entered_area(body):
	var kid = get_node("Kid")
	var scary = get_node("Scary")

	var show_kid = (randi() % 2 == 0)

	kid.visible = show_kid
	scary.visible = not show_kid

Does it matter where I connect the signal to?
like do i need to connect it in my player script or the main scene script?

EitherRock | 2018-06-23 18:54

I think you can connect that signal from your area to the node where you’ve put your script. It can be the node itself :slight_smile:

Zylann | 2018-06-23 18:56

im getting an error “Invalid set index ‘visible’ (on base: ‘null instance’) with value of type ‘bool’.”

Sorry, im still fairly new to this.

EitherRock | 2018-06-23 19:09

Maybe because the node “Kid” wasnt found. Don’t just copy/paste my code, you should adapt it to the way your scene is setup and named (also I assume you use Godot 3).

Zylann | 2018-06-23 19:11

yeah its 3 and okay i also had debugging problems with getting nodes “kid1” and “monkey”

I did:
get_node(“kid1”)
get_node(“monkey”)

didn’t work and i also tried:

get_node(“Lost Jungle/kids/area2d/kid1”)

>Lost jungle = main node
     >kids
       > area2d
            >kid1
            >monkey
   

kid1 and monkey are children of area2d

EitherRock | 2018-06-23 19:21

theres supposed to be “_” in between get node. ignore the mistake lol

EitherRock | 2018-06-23 19:22

![ Imgur: The magic of the Internet ][comment6-1]

EitherRock | 2018-06-23 19:52

Where did you connect the body_enter signal?

Zylann | 2018-06-23 19:53

i connected it to Lost Jungle

EitherRock | 2018-06-23 19:57

So the paths should be:

get_node("kids/area2d/kid1")
get_node("kids/area2d/monkey")

Zylann | 2018-06-23 20:00

i also tried connecting it to the area2d and the player script.

nothing happens with area2d or Lost Jungle, but when I connect it to player it give the error i mentioned

EitherRock | 2018-06-23 20:00

would the path be the same if i have it connected to player?

EitherRock | 2018-06-23 20:02

![ Imgur: The magic of the Internet ][comment12-1]

[1]:

EitherRock | 2018-06-23 20:06

The signal is connected to player in this one since i wasn’t getting any response from Lost Jungle

EitherRock | 2018-06-23 20:07

You must connect to the node on which you put the script receiving that signal. Depending on where that node is in the hierarchy, the path to get the kid or monkey will change (it is relative).
When you connect the signal, the function name in the dialog must match with the one in your script.

Zylann | 2018-06-23 20:20