I’m using Godot 4.2 to make an incremental game. This is my first project in Godot, and I’m a beginner at coding. I’m slowly learning GDscript fundamentals while I’m working on my project. I recently encountered a problem I need help solving.
I wrote all of my code in a single script file attached to a “node2d” node, which is my first node in the game. I used this way because I couldn’t find a way to access variables in other script files and scenes. I know this is very bad practice, but I didn’t encounter many problems initially because the gameplay was simple enough to handle in a single file. However, as the project complexity increased, I began to encounter some problems.
I needed to have a save/load system in the main menu. However, because the main menu scene was different from where node2d is, I couldn’t access any of the variables needed for saving and loading. So, I deleted the main menu scene, moved its content to the node2d scene, and wrote its code on the same script page. It solved that problem.
Now, I’m trying to create a dialogue system in my game, and I want to use a different script file for this. So, I need to learn how to access a script’s content from another script’s. I researched the internet and found it can be done quickly if one understands classes and inheritance.
This is what I gathered. Please correct me where I’m wrong. Every script attached to a node is a class. Classes represent some kind of container. A child of a node inherits its parent’s variables, functions, and methods. So you can use them in the child’s script file. This inheritance is automatic. For example, if I create an entity class with speed and health variables, I can create player and enemy classes under it, and they receive these variables without extra coding. So, from that point, I can code what differs between the player and the enemy in their respective script files. This way, I don’t need to repetitively code speed and health for both of them.
TL;DR
Here are the problems I couldn’t solve:
My dialogue node is a panel, a child of control. My main script file has been attached to node2d. I make the dialogue node a child of node2d by putting it in node2d at the node manager. When I tried extending node2d into the panel, it gave me an error that said:
"Script inherits from native type “Node2D,” so it can’t be assigned to an object of type: “Control.”
I can fix this by changing node2d to a more inclusive node, which includes the control node, but I think there must be another way to do that. If I change node2d, it can break some of the code.
How can I access a function or variable from another scene’s script?
3)How can I properly define and use a class to get information from other classes?
4)What’s the difference between an object and a class?
yeah of course cant, because control and Node2d is a separate inherited class
accessing if you throw the callable and have the other script to save it as a variable, then just .call() the callable variable will call the script, but the usual way to trigger/access function of another script is by Signal
Use signaling
talking about Object in Godot, it is the very basic/base class and all classes inherited from it
you can read what can object do, so basically any class can do/use Object methods because every classes inherited from it Object — Godot Engine (stable) documentation in English
With Godot, you try to “modularize” the parts of your game, and then “stuck them” together.
According to official documentation:
In Godot, a game is a tree of nodes that you group together into scenes. You can then wire these nodes so they can communicate using signals.
Having that in mind, your super big’o script that defines the Node2D of your game can be split, and make each part of your game and make each part handle each specific part that they need, creating the connections those needs in order to work.
Every Script defines a class, but a Script attached to a Node (or any Object) extends their functionality.
Classes are like the blueprint of a house, where the blueprint as is, in paper, is just “the definition of the house”, and the house itself built from that blueprint is “the instance of the class”.
A child of a node is just a child of that node. Nothing more, nothing less. Their behavior may vary according the type of relationship and type of the node, but they, in essence, doesn’t share anything besides their tree relationship.
This is not possible due the previous point. Child nodes and parent nodes doesn’t share anything at core, just a “parent-child” relationship. Maybe you’re confused with the OOP inheritance that you do in script when you extend in script?
A Script and the instance that holds that script (in example, a Node in your scene editor that has that script attached) are two different things.
Having that in mind, your super big’o script that defines the Node2D of your game can be split, and make each part of your game and make each part handle each specific part that they need, creating the connections those needs in order to work.
Thanks you for the answer. I’m going to split the node when I learn how to use signaling.
A child of a node is just a child of that node. Nothing more, nothing less. Their behavior may vary according the type of relationship and type of the node, but they, in essence, doesn’t share anything besides their tree relationship. Child nodes and parent nodes doesn’t share anything at core, just a “parent-child” relationship. Maybe you’re confused with the OOP inheritance that you do in script when you extend in script?
Yes I thought inheritance is automatic. For now I’m going to learn fundamentals more. Also how can I quote your posts like you did?
Select the text and then select “quote” in the popup:
Take a look at your private messages, usually a bot guides you through the forum tutorial… or just send a message to @discobot which is the bot that makes the tutorial.