How to check for tyes of overlapping areas

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


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?

:bust_in_silhouette: Reply From: p7f

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

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

BigBackPack | 2020-07-28 15:43

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.

p7f | 2020-07-28 16:32