0 votes

Hey,

i am working on a game with an player character (area2D) which is stepping on different fields (area2D). Depending on the field he is staying different events should be triggered. In order to do so i need to check what type of object he is currently overlapping with. But i have no idea how to do so. Can anybody give me an example how i could write this in GD script?

in Engine by (27 points)

1 Answer

0 votes

There are some ways. One could be adding each a group to each field, and when checking the overlapping area, ask:

if area.is_in_group("my_field_type"):
     #do stuff

(obviously, changing "my_field_type" with the names of the groups. Another way, if you have scripts for each different field, you could add class_name MyFieldType in the script, and then just ask, inside the function where you detected the overlapping area:

if area is MyFieldType:
    #do stuff
by (3,408 points)

thx but somehow this does not work for me. Because the script is not on my player character but on a different object in the same hierarchy i added a $player. in front of the line. But if i move the player i only get the error: "Invalid get index 'area' (on base: 'Area2D').

here is the code:

if ($player.area.is_in_group("symbols_1")):
        print("something")

Hi! ok, sorry, i missunderstood i think, but would you please add more info to the question then? Because, the question implies that when your Character (Area2D) steps on a field (also Area2D) you want to trigger an effect. Then, either your character or the filed should be able to detect the other and trigger the effect. What is your other object for? what kind of node? should that other object trigger the effect? It would be useful also if you provide your hierarchy.

Anyways, what would be the problem to connect the area_entered signal of the player, to the script of that other object, and doing what i told? it really doesn't matter where the script is, as long as its on the same tree.

And the snipeet you wrote wont work, because your field area is not child of $player, so you cannot retrieve it like that.

For me the solution is, connect the signal area_entered to a function in the script where you trigger the effects, and then check the group in that funciton.

If you dont want to connect signal and function, then you should retrieve the area that is overlaping with player with the function $player.get_overlaping_areas() (keep the 0 index if you only care for one), and then check the group to the area retrieved that way.

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.