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?

in Engine by (381 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.

by (10,608 points)
selected by

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 Frequently asked questions and 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 [email protected] with your username.