Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Mapkinasekinase |
Hi everyone,
I am pretty new to Godot, but have previously dabbled a bit with C++.
I feel like I am missing something obvious, or maybe I am using signals for something they are not intended. I’d greatly appreciate some feedback.
In essence, I want to create a small program in which “hunters” walk around randomly until they “see” some (so far) stationary “prey”. Both prey and hunters are created via code and therefore don’t have static names.
I am using Area2Ds and CollisionShapes as the elemental basis for the different objects noting they are in each other’s spheres of influence. In particular, the hunters have two areas associated with them, one larger (their “seeing area”) and one smaller "their “eating area”).
Scene 1:
- Node2D (Hunter)
- Area2D (Seeing Range)
- CollisionShape
- Area2D (Eating Range)
- CollisionShape
-Sprite
- CollisionShape
- Area2D (Seeing Range)
Scene 2
- Area2D (Prey)
- Collision Shape
- Sprite
Then I am using signals, in particular “on_area_entered”, to trigger the intended behaviour. However, I’d need to pass on some additional information stored within the different prey objects as part of the signal, for the hunter to act accordingly. (e.g. if there are different types of prey, but the hunter is only meant to react to some of them, but not others).
I’ve followed various avenues, all unsuccessful, so far:
- Connecting the signal “on_area_entered” from the hunter to itself. The area argument passed by the signal then contains the area of the prey. However, I cannot access additional information from that area (e.g. area.typeofprey), because I can only access the standard variables of area2a (e.g. area.position). I can also not call any functions in the prey, for the same reason (eg. area.foo() )
$SeeingRange.connect("area_entered",self,"_on_hunter_area_entered")
func _on_hunter_area_entered(area):
how to get custom variable area.type?
- As a workaround to the above, I wanted to use the “name” attribute of the area being passed for additional information, but that is autogenerated, e.g. “@Prey@02” or so. Also, even if I could get it working in this instance, it might fail in others, when I’d need to pass on more complex information.
connect("area_entered",self,"_on_hunter_area_entered")
func _on_hunter_area_entered(area):
if area.name == "rabbit":
foo()
- Connecting the signal “on_area_entered” from the prey to the hunter. Then I should be able to pass on information as additional arguments by amending the connect function, though I have struggled to get that working as the arguments being passed are often ‘null’. Even if I did get it working, I’d lose the information which of the two areas in the hunter node are overlapping with the prey area.
connect("area_entered",hunter,"_on_Target_area_entered",[type])
func _on_prey_area_entered
how to get the information on whether prey now overlaps with the Seeing_Range or
the Eating_Range area of the hunter scene?
why is type null? At least when I tried to pass they prey's position? (haven't tried many
more attributes)
So, my question is: Is there any way to get the proper node of the signal emitter, so that I could access its variables, or call functions?
And in case of “No, that’s not how signals should be used, fool!”, then is there a way I can use Area2Ds and collision shapes for my intended purpose but without using signals, but direct function calls between the hunters and the prey?
Many thanks!