0 votes

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.

in Engine by (14 points)

1 Answer

+1 vote

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
by (28,791 points)

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?

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

im getting an error "Invalid set index 'visible' (on base: 'null instance') with value of type 'bool'."

Sorry, im still fairly new to this.

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

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

I did:
getnode("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

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

Where did you connect the body_enter signal?

i connected it to Lost Jungle

So the paths should be:

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

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

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

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

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.

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.