About the scenes
You can instantiate scenes. To do that, you need to click on that chain icon, beside the plus icon on the top of the scene tree.
About the scripts
- If you want the exact same script in multiple scenes, you can use a
- If you wan't a base script for multiple scenes, you can inherit
scripts from others.
Doing that, the inherited script will have access to all methods and variables of the parent but it also can have it own methods and variables, without sharing them with the parent. To do that, in the very top of the script, change the
extends Object to
You can also easily do that when creating a new script by browsing the parent script using the folder icon:
It's a good idea to use at the same time singletons and inherited scripts, so you won't need to copy and paste your code that is comon in a bunch of different scripts and, by using singletons, you won't deppend on your scene tree.
What I said above may be a litte confusing, but come with me:
Let's suppose that you have your
Player.tscn scene and you are instantiating it at you
Level1.tscn scene has the following tree:
- Level1 (root node)
And in your
Player.tscn root node you have the following code:
onready var enemy = get_parent().get_node("Enemy")
It will work fine BUT, imagine if you renamed the "Enemy" node or changed your scene tree to someting like:
- Level1 (root node)
- Enemies (container)
It would not work anymore because you were getting the node "Enemy" for that scene tree.
To avoid that, you can use global scripts (singletons).
Let's suppose that you created the singleton
Global.gd and added the var
enemy but did not set it to any value.
What you could do was, in the Enemy script, add the following code:
Global.enemy = self
Doing that, you can access this node from ANYWHERE in your game just by typing
Read the singleton docs for more info on how to create one.
As you are very new to Godot, I would recommend the course made by GameDev available on Udemy.
I saw it myself and it's really great. There are also great tutorials on Youtube for free, such as the channel KidsCanCode.
There are also great tutorials on the Godot Docs.
Take a look at them and you will improve your GDScript skills very fast.
If you have more doubts, please ask!
If this question solved your problem, please set it as the best answer and remember to help others to keep the community running.