Can I add lines to a function from another script and function?

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

In BaseGame.gd there should be the do_something() function. I want to write lines in this function from another script.

:bust_in_silhouette: Reply From: blockchan

Yes it’s possible and called “inheritance”

create do do_something in SecondScript.gd, then at the top of BaseGame.gd:

extends SecondScript

I’d have to to extends “res://Scripts/Second.gd”, however this isn’t what I wanted to have. I’ll give you an example:
BaseGame.tscn

Control

Container

Second.tscn

Node

Control (inherited from BaseGame.tscn)

BaseGame.gd

func do_something():
pass

func _ready():
print ($Control.rect_size)

Second.gd

$Control.do_something.add_lines("print("Hello")")

(this is just an example how it could work)
Of course, the added code wouldn’t be seeable for us, it would be there like if you did $Control.add_child(Sprite.new)
If Second.gd extended BaseGame.gd, $Control wouldn’t be found.

MaaaxiKing | 2020-05-13 15:44