Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | BakouKai |
Hi every one,
I built a map on which various NPC are traveling.
The tree looks like :
MainNode (script)
|_ TileMap
|_NPC (scene instance)
NPC movements are handled in the script of the npc Scene. My main node creates them, instantiate the childrens, and keep track of them in a dictionary (NPC_name: Instance)
When a NPC reaches its destination, it is supposed to emit a signal, that send his own instance so that the main script can remove it from the traveling table (another dictionary)
it goes like this :
elif path_2D.size()==1 and move_distance > distance_to_next:
position = path_2D[0]
print("set NPC moving process off")
set_process(false)
animation_mode.travel("Idle")
self.hide()
print("Emit signal for end of travel")
emit_signal("end_of_NPC_travel",instance_ww,position)
break
Nothing special actually.
The problem is that my main script never reacts when my codes reaches the “emit_signal”.
The codes looks like this and is never triggered :
func _on_NPC_end_of_NPC_travel(instance_ww,position):
print("checking who has arrived")
for i in npc_dic:
if npc_dic[i]==instance_ww:
print("Npc ",i," has arrived at ", position)
Global.pop_dic[i]["Position"]=position
instance_ww.hide()
npc_dic[i].remove()
instance_ww.queue_free()
Global.npc_arrival(i)
The signal has been connected through the editor/inspector. I see the green arrow next to my code in the MainScript.
Any idea regarding what I did wrong ?
Many thanks for your help !
Little bit more on this.
I added a fake signal (just printing if it is received in the main script) like this :
Main script :
func _on_NPC_end_travel():
print("End travel short signal")
If I call emit_signal("end_travel")
in the _ready() of my NPC Instance, it works and the signal is received in the main script.
It is not working from the segment of code shown before.
I also tried to extract it in a small function like so :
func end_of_road():
print("emmitting short signal")
emit_signal("end_travel")
that I would call in my movement (in particular end of movement section) :
func move_along_path(move_distance):
var start_position = position
traveling(path_2D[0])
for _i in range (path_2D.size()):
var distance_to_next=start_position.distance_to(path_2D[0])
if move_distance <= distance_to_next and move_distance >=0.0:
position = start_position.linear_interpolate(path_2D[0], move_distance / distance_to_next)
break
elif path_2D.size()==1 and move_distance > distance_to_next:
position = path_2D[0]
print("set NPC moving process off")
set_process(false)
animation_mode.travel("Idle")
self.hide()
print("Emit signal for end of travel")
end_of_road()
emit_signal("end_of_NPC_travel",instance_ww,position)
break
move_distance -= distance_to_next
start_position=path_2D[0]
path_2D.remove(0)
Strangely it does not work this way.
Any idea why the signal works from the _ready() but not in my other func ?
Many thanks for your help
BakouKai | 2021-05-13 14:32
Try putting this in the NPC’s _ready()
function and see if it works:
func _ready():
self.connect("end_of_NPC_travel", get_parent(), "_on_NPC_end_of_NPC_travel")
timothybrentwood | 2021-05-13 14:32
Hi Thimothy,
Well, I tried to connect manually the signal … and it seems to work better than links in the editor… for some reasons… Dunno if its a kind of a bug but whatever. Thank you for your help
BakouKai | 2021-05-13 15:11
Can you put your answer as an answer so that I flag it ?
BakouKai | 2021-05-13 15:11