0 votes

Tree:

Gamemode1 (script attached)

  • BaseGame (instance, script attached)

In BaseGame's script I have the function game_over(). I want to do something when game_over() is called but I want to do this at Gamemode1's script. How?

asked Jun 18 in Engine by MaaaxiKing (292 points)

1 Answer

+1 vote
Best answer

GameMode1.gd

extends ...

func _ready():
    $BaseGame.connect("game_over", self, "_on_BaseGame_game_over")

func _on_BaseGame_game_over():
    # do something

BaseGame.gd

extends ...

signal game_over

func game_over():
    emit_signal("game_over")

If you don't want to use signals, you could also call the parents method directly:

GameMode1.gd

extends ...

func _on_BaseGame_game_over():
    # do something

BaseGame.gd

extends ...

func game_over():
    get_parent()._on_BaseGame_game_over()

Edit: Fixed the code for game_over in the second example.

answered Jun 18 by njamster (9,538 points)
selected Jun 18 by MaaaxiKing

You mean in BaseGame.gd
get_parent()._on_BaseGame_game_over right? Or do I misunderstand?

Yeah, my bad, sorry. I correct my answer accordingly.

What if the parent doesn't have this function? This is also sometimes, in other modes.

second question's answer --> just add if get_parent().has_method("_on_game_over").

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.