Script A needs access to Script B, which needs access to Script C

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

I’ve only recently started Godot and it’s a bit hard to find the information I’m looking for, I would appreciate any help!

I’ve been using preload to access other scripts that are not within the tree the current script is in, except sometimes those scripts also need to preload other scripts that aren’t in the tree either.

A) print B.x → can access B
B) x = var C.y → can’t access C, creates error
C) var y

It doesn’t break if I, say, preload script C directly inside of B’s function as opposed to just writing it out at the top, but it sounds like horrible spaghetti code to do that for every single function that I theoretically need access to.

I’m using the GDQuest Turn Based RPG open source as reference and it doesn’t appear to run into this problem at all, but just looking at the code I can’t tell how it’s being bypassing it. Also I feel like all this preloading stuff is inefficient too, but I don’t know any workarounds.

Do you know about the concepts of object-oriented programming? Have you looking into using inheritance for the code?

Ertain | 2021-08-22 08:37

Besides the fact you could use some kind of global script (Singleton), it could help if you try to reproduce your problem in a smaller scope, so you can also show us concrete code. Without it is extremely hard to tell what your problem is. Especially if you recently started Godot, you should get used to always present some real gd-script code.

Spyrex | 2021-08-22 15:55